<?php

declare(strict_types=1);

namespace Drupal\Tests\flowdrop\Unit\DTO;

use Drupal\Tests\UnitTestCase;
use Drupal\flowdrop\DTO\ValidationError;
use Drupal\Tests\flowdrop\Unit\Traits\DtoContractTestTrait;

/**
 * Tests the ValidationError DTO.
 *
 * @coversDefaultClass \Drupal\flowdrop\DTO\ValidationError
 * @group flowdrop
 */
class ValidationErrorTest extends UnitTestCase {

  use DtoContractTestTrait;

  /**
   * @covers ::__construct
   * @covers ::getParameter
   * @covers ::getMessage
   * @covers ::getCode
   */
  public function testConstructorAndGetters(): void {
    $error = new ValidationError("email", "Invalid format", "ERR_FMT");
    $this->assertEquals("email", $error->getParameter());
    $this->assertEquals("Invalid format", $error->getMessage());
    $this->assertEquals("ERR_FMT", $error->getCode());
  }

  /**
   * @covers ::__construct
   * @covers ::getCode
   */
  public function testConstructorWithoutCode(): void {
    $error = new ValidationError("field", "Required");
    $this->assertNull($error->getCode());
  }

  /**
   * @covers ::toArray
   */
  public function testToArray(): void {
    $error = new ValidationError("name", "Too long", "MAX_LEN");
    $this->assertEquals([
      "parameter" => "name",
      "message" => "Too long",
      "code" => "MAX_LEN",
    ], $error->toArray());
  }

  /**
   * @covers ::fromArray
   * @covers ::toArray
   */
  public function testRoundTrip(): void {
    $error = new ValidationError("url", "Invalid URL", "BAD_URL");
    $this->assertRoundTripSerialization($error);
  }

}
