<?php

declare(strict_types=1);

namespace Drupal\Tests\flowdrop\Unit\Plugin\FlowDropNodeProcessor;

use Drupal\Tests\UnitTestCase;
use Drupal\flowdrop\DTO\Tool\ToolBinding;
use Drupal\flowdrop\DTO\Tool\ToolCollection;
use Drupal\flowdrop\DTO\Tool\ToolCollectionInterface;
use Drupal\flowdrop\Plugin\FlowDropNodeProcessor\ToolsAwareTrait;
use Drupal\flowdrop\Service\Tool\ToolInvokerInterface;

/**
 * Tests the ToolsAwareTrait.
 *
 * @coversDefaultClass \Drupal\flowdrop\Plugin\FlowDropNodeProcessor\ToolsAwareTrait
 * @group flowdrop
 */
class ToolsAwareTraitTest extends UnitTestCase {

  /**
   * A processor-like object exposing the trait's protected accessors.
   */
  private function subject(): object {
    return new class() {
      use ToolsAwareTrait;

      /**
       * Exposes the protected tool collection accessor.
       */
      public function tools(): ToolCollectionInterface {
        return $this->getTools();
      }

      /**
       * Exposes the protected connected-tools accessor.
       */
      public function connected(): array {
        return $this->getConnectedTools();
      }

    };
  }

  /**
   * Without injection the trait yields an empty collection, not an error.
   *
   * @covers ::getTools
   * @covers ::getConnectedTools
   */
  public function testDefaultsToEmpty(): void {
    $subject = $this->subject();
    $this->assertCount(0, $subject->tools());
    $this->assertSame([], $subject->connected());
  }

  /**
   * Injected tools are exposed via the accessors.
   *
   * @covers ::setTools
   * @covers ::getTools
   * @covers ::getConnectedTools
   */
  public function testSetToolsIsExposed(): void {
    $invoker = $this->prophesize(ToolInvokerInterface::class)->reveal();
    $binding = new ToolBinding("alpha", "node_a", "d", [], "alpha (node_a)", $invoker);
    $collection = new ToolCollection([$binding]);

    $subject = $this->subject();
    $subject->setTools($collection);

    $this->assertCount(1, $subject->tools());
    $this->assertSame([$binding], $subject->connected());
  }

}
