reviewer-1.2.x-dev/tests/src/Kernel/Checklist/ChecklistTest.php

tests/src/Kernel/Checklist/ChecklistTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\reviewer\Kernel\Checklist;

use Drupal\KernelTests\KernelTestBase;
use Drupal\reviewer\Reviewer\Action;
use Drupal\reviewer\Reviewer\Result\CollectionResultInterface;
use Drupal\reviewer\Reviewer\Result\IndividualResultInterface;
use Drupal\reviewer\Reviewer\Review\ReviewInterface;
use Drupal\reviewer\Reviewer\Task\TaskInterface;
use Drupal\reviewer_test\Plugin\reviewer\Checklist\TestChecklist as TestChecklist;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Test checklist methods.
 */
#[Group('reviewer')]
#[CoversClass(ChecklistTest::class)]
final class ChecklistTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'reviewer',
  ];

  /**
   * Checklist constructor arguments.
   *
   * @var array{
   *   \Drupal\reviewer\Reviewer\Result\ResultFactoryInterface,
   *   \Drupal\reviewer\Reviewer\Task\TaskFactoryInterface,
   * }
   */
  private readonly array $checklistConstructorArguments;

  /**
   * {@inheritdoc}
   *
   * @throws \Exception
   * @throws \InvalidArgumentException
   * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
   * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
   */
  protected function setUp(): void {
    parent::setUp();
    $this->checklistConstructorArguments = [
      $this->container->get('reviewer.factory.result'),
      $this->container->get('reviewer.factory.task'),
    ];
  }

  /**
   * Test running the checklist.
   *
   * @throws \PHPUnit\Framework\MockObject\Exception
   * @throws \ReflectionException
   */
  public function testRun(): void {
    $review = $this->createMock(ReviewInterface::class);
    $review->method('resultId')->willReturn('review');

    $result_one = $this->createMock(IndividualResultInterface::class);
    $result_one->method('getId')->willReturn('result_one');
    $collection_one = $this->createMock(CollectionResultInterface::class);
    $collection_one->method('getId')->willReturn('collection_one');
    $collection_one->method('getIndividualResults')->willReturn([$result_one]);
    $task_one = $this->createMock(TaskInterface::class);
    $task_one->method('getId')->willReturn('task_one');
    $task_one->method('getResults')->willReturn($collection_one);

    $result_two = $this->createMock(IndividualResultInterface::class);
    $result_two->method('getId')->willReturn('result_two');
    $collection_two = $this->createMock(CollectionResultInterface::class);
    $collection_two->method('getId')->willReturn('collection_two');
    $collection_two->method('getIndividualResults')->willReturn([$result_two]);
    $task_two = $this->createMock(TaskInterface::class);
    $task_two->method('getId')->willReturn('task_two');
    $task_two->method('getResults')->willReturn($collection_two);

    $reflection = (new \ReflectionClass(TestChecklist::class))->getParentClass();
    if (!$reflection) {
      self::fail('Cannot set tasks.');
    }
    $tasks = $reflection->getProperty('tasks');

    $check = new TestChecklist(...$this->checklistConstructorArguments);
    $check->setReview($review);
    $tasks->setValue($check, [$task_one, $task_two]);
    $check->run(Action::Check);
    self::assertCount(2, $check->getResults()->getAll());
    self::assertSame(['collection_one', 'collection_two'], array_keys($check->getResults()->getAll()));

    $fix = new TestChecklist(...$this->checklistConstructorArguments);
    $fix->setReview($review);
    $tasks->setValue($fix, [$task_one, $task_two]);
    $fix->run(Action::Fix);
    self::assertCount(2, $fix->getResults()->getAll());
    self::assertSame(['collection_one', 'collection_two'], array_keys($fix->getResults()->getAll()));
  }

  /**
   * Test getting the checklist ID.
   */
  public function testGetId(): void {
    $checklist = new TestChecklist(...$this->checklistConstructorArguments);
    self::assertSame('checklist_test', $checklist->getId());
  }

  /**
   * Test getting the result ID.
   *
   * @throws \PHPUnit\Framework\MockObject\Exception
   */
  public function testResultId(): void {
    $review = $this->createMock(ReviewInterface::class);
    $review->method('resultId')->willReturn('review');
    $checklist = new TestChecklist(...$this->checklistConstructorArguments);
    $checklist->setReview($review);

    self::assertSame('review.checklist_test', $checklist->resultId());
  }

  /**
   * Test that getting tasks return task objects.
   */
  public function testGetTasks(): void {
    $checklist = new TestChecklist(...$this->checklistConstructorArguments);
    self::assertCount(3, $checklist->getTasks());
    self::assertContainsOnlyInstancesOf(TaskInterface::class, $checklist->getTasks());
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc