<?php

declare(strict_types=1);

namespace Drupal\Tests\flowdrop\Unit\DTO\JsApi;

use Drupal\Tests\UnitTestCase;
use Drupal\flowdrop\DTO\JsApi\Edge;

/**
 * Tests the JsApi transport Edge value object.
 *
 * @coversDefaultClass \Drupal\flowdrop\DTO\JsApi\Edge
 * @group flowdrop
 */
class EdgeTest extends UnitTestCase {

  /**
   * @covers ::fromArray
   * @covers ::getId
   * @covers ::getSource
   * @covers ::getTarget
   * @covers ::getSourceHandle
   * @covers ::getTargetHandle
   * @covers ::getData
   */
  public function testReadsCamelCaseTransportKeys(): void {
    $edge = Edge::fromArray([
      'id' => 'e1',
      'source' => 'a.1',
      'target' => 'b.1',
      'sourceHandle' => 'a.1-output-text',
      'targetHandle' => 'b.1-input-message',
      'data' => ['edgeType' => 'data'],
    ]);

    $this->assertSame('e1', $edge->getId());
    $this->assertSame('a.1', $edge->getSource());
    $this->assertSame('b.1', $edge->getTarget());
    $this->assertSame('a.1-output-text', $edge->getSourceHandle());
    $this->assertSame('b.1-input-message', $edge->getTargetHandle());
    $this->assertSame(['edgeType' => 'data'], $edge->getData());
  }

  /**
   * @covers ::fromArray
   */
  public function testDefaultsForMissingKeys(): void {
    $edge = Edge::fromArray([]);

    $this->assertSame('', $edge->getId());
    $this->assertSame('', $edge->getSource());
    $this->assertSame('', $edge->getTarget());
    $this->assertSame('', $edge->getSourceHandle());
    $this->assertSame('', $edge->getTargetHandle());
    $this->assertSame([], $edge->getData());
  }

  /**
   * @covers ::fromArray
   */
  public function testNonArrayDataCoercesToEmptyArray(): void {
    $edge = Edge::fromArray(['data' => 'not-an-array']);
    $this->assertSame([], $edge->getData());
  }

  /**
   * @covers ::toArray
   * @covers ::fromArray
   */
  public function testRoundTripsThroughCamelCase(): void {
    $camel = [
      'id' => 'e1',
      'source' => 'a.1',
      'target' => 'b.1',
      'sourceHandle' => 'a.1-output-text',
      'targetHandle' => 'b.1-input-message',
      'data' => ['edgeType' => 'data'],
    ];

    $this->assertSame($camel, Edge::fromArray($camel)->toArray());
  }

}
