<?php

declare(strict_types=1);

namespace Drupal\Tests\flowdrop\Unit\Traits;

/**
 * Trait for DTO contract testing.
 *
 * Verifies DTO serialization round-trips and immutability.
 */
trait DtoContractTestTrait {

  /**
   * Asserts that a DTO survives toArray -> fromArray round-trip.
   *
   * @param object $dto
   *   The DTO instance to test.
   */
  protected function assertRoundTripSerialization(object $dto): void {
    $this->assertTrue(method_exists($dto, 'toArray'), get_class($dto) . ' must have toArray()');
    $this->assertTrue(method_exists($dto, 'fromArray'), get_class($dto) . ' must have fromArray()');

    $array = $dto->toArray();
    $restored = $dto::fromArray($array);
    $this->assertEquals($array, $restored->toArray(), get_class($dto) . ' round-trip serialization failed');
  }

  /**
   * Asserts that a method returns a new instance (immutability).
   *
   * @param object $dto
   *   The DTO instance.
   * @param string $method
   *   The method to test.
   * @param array<mixed> $args
   *   Arguments to pass to the method.
   */
  protected function assertImmutable(object $dto, string $method, array $args = []): void {
    $result = $dto->$method(...$args);
    $this->assertNotSame($dto, $result, get_class($dto) . "::{$method}() must return a new instance");
  }

}
