reviewer-1.2.x-dev/tests/src/Kernel/Review/ReviewBuilderTest.php

tests/src/Kernel/Review/ReviewBuilderTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\reviewer\Kernel\Review;

use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\reviewer\Exception\AttributeMissingException;
use Drupal\reviewer\Plugin\reviewer\Review\ReviewBase;
use Drupal\reviewer\Plugin\reviewer\Review\ReviewPluginInterface;
use Drupal\reviewer\Plugin\ReviewManagerInterface;
use Drupal\reviewer\Reviewer\Review\ReviewBuilder;
use Drupal\reviewer\Reviewer\Review\ReviewBuilderInterface;
use Drupal\reviewer\Reviewer\Review\ReviewInterface;
use Drupal\reviewer_test\Plugin\reviewer\Checklist\TestChecklist;
use Drupal\reviewer_test\Plugin\reviewer\Review\TestReview;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests the review builder.
 */
#[Group('reviewer')]
#[CoversClass(ReviewBuilder::class)]
final class ReviewBuilderTest extends KernelTestBase {

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

  private readonly ReviewBuilderInterface $builder;

  /**
   * Test review plugins.
   *
   * @var array<string, \Drupal\reviewer\Plugin\reviewer\Review\ReviewPluginInterface>
   */
  private readonly array $reviews;

  /**
   * Review builder constructor arguments.
   *
   * @var array{
   *   \Drupal\reviewer\Reviewer\BundleEvaluatorInterface,
   *   \Drupal\reviewer\Reviewer\Checklist\ChecklistFactoryInterface,
   *   \Drupal\Core\Entity\EntityTypeManagerInterface,
   *   \Drupal\reviewer\Reviewer\Review\ReviewFactoryInterface,
   *   \Drupal\reviewer\Plugin\ReviewManagerInterface,
   * }
   */
  private readonly array $constructorArguments;

  /**
   * {@inheritdoc}
   *
   * @throws \Exception
   * @throws \PHPUnit\Framework\MockObject\Exception
   */
  protected function setUp(): void {
    parent::setUp();

    $this->constructorArguments = [
      $this->container->get('reviewer.evaluator.bundle'),
      $this->container->get('reviewer.factory.checklist'),
      $this->container->get('entity_type.manager'),
      $this->container->get('reviewer.factory.review'),
      $this->container->get('plugin.manager.reviewer.review'),
    ];

    $review_one = $this->createMock(ReviewPluginInterface::class);
    $review_one->method('getPluginId')->willReturn('one');
    $review_one->method('getLabel')->willReturn('One');
    $review_one->method('getChecklists')->willReturn([TestChecklist::class]);
    $review_two = $this->createMock(ReviewPluginInterface::class);
    $review_two->method('getPluginId')->willReturn('two');
    $review_two->method('getLabel')->willReturn('Two');
    $review_two->method('getChecklists')->willReturn([TestChecklist::class]);
    $review_three = $this->createMock(ReviewPluginInterface::class);
    $review_three->method('getPluginId')->willReturn('three');
    $review_three->method('getLabel')->willReturn('Three');
    $review_three->method('getChecklists')->willReturn([TestChecklist::class]);
    $review_three->method('getConfigEntityId')->willReturn('type');
    $review_three->method('getBundles')->willReturn(['a', 'b']);

    $this->reviews = [
      $review_one->getPluginId() => $review_one,
      $review_two->getPluginId() => $review_two,
      $review_three->getPluginId() => $review_three,
    ];

    $config_entity = $this->createMock(ConfigEntityInterface::class);
    $config_entity->method('label')->willReturn('Entity Label');
    $config_entity_type = $this->createMock(EntityTypeInterface::class);
    $config_entity_type->method('getKey')->willReturn('bundle');
    $entity_storage = $this->createMock(EntityStorageInterface::class);
    $entity_storage->method('load')->willReturn($config_entity);
    $entity_storage->method('loadByProperties')->willReturn([$config_entity]);
    $entity_storage->method('getEntityType')->willReturn($config_entity_type);
    $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
    $entity_type_manager->method('getStorage')->willReturn($entity_storage);

    $definitions = [];
    foreach ($this->reviews as $review) {
      $definitions[$review->getPluginId()] = [
        'id' => $review->getPluginId(),
        'label' => $review->getLabel(),
        'bundles' => $review->getBundles(),
      ];
    }
    $review_manager = $this->createMock(ReviewManagerInterface::class);
    $review_manager->method('getDefinition')->willReturnOnConsecutiveCalls(...$definitions);
    $review_manager->method('createInstance')->willReturnOnConsecutiveCalls(...$this->reviews);

    $this->builder = new ReviewBuilder(
      $this->container->get('reviewer.evaluator.bundle'),
      $this->container->get('reviewer.factory.checklist'),
      $entity_type_manager,
      $this->container->get('reviewer.factory.review'),
      $review_manager,
    );
  }

  /**
   * Test building a review from a class.
   *
   * @throws \ReflectionException
   */
  public function testFromClass(): void {
    $builder = new ReviewBuilder(...$this->constructorArguments);

    $review = $builder->fromClass(TestReview::class);
    self::assertSame(['review_test'], array_keys($review));
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $review);

    self::expectException(AttributeMissingException::class);
    $builder->fromClass(ReviewBase::class);
  }

  /**
   * Test building a review from an array of classes.
   *
   * @throws \ReflectionException
   */
  public function testFromClasses(): void {
    $builder = new ReviewBuilder(...$this->constructorArguments);

    $reviews = $builder->fromClasses([
      TestReview::class,
    ]);
    self::assertSame(['review_test'], array_keys($reviews));
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $reviews);

    self::expectException(AttributeMissingException::class);
    $builder->fromClasses([ReviewBase::class]);
  }

  /**
   * Test building a review from a plugin ID.
   */
  public function testFromId(): void {
    /** @var \Drupal\reviewer\Reviewer\Review\ReviewInterface[] $review */
    $review = $this->builder->fromId($this->reviews['one']->getPluginId());
    self::assertSame(['one'], array_keys($review));
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $review);
    self::assertSame('One', $review['one']->getLabel());
    self::assertCount(1, $review['one']->getChecklists());

    $review = $this->builder->fromId($this->reviews['two']->getPluginId());
    self::assertSame(['two'], array_keys($review));
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $review);
    self::assertCount(1, $review['two']->getChecklists());

    /** @var \Drupal\reviewer\Reviewer\Review\ReviewInterface[] $reviews */
    $reviews = $this->builder->fromId($this->reviews['three']->getPluginId());
    self::assertSame(['three.a', 'three.b'], array_keys($reviews));
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $review);
    self::assertSame('Three: Entity Label', $reviews['three.a']->getLabel());
    self::assertSame('Three: Entity Label', $reviews['three.b']->getLabel());
    self::assertCount(1, $reviews['three.a']->getChecklists());
    self::assertCount(1, $reviews['three.b']->getChecklists());
  }

  /**
   * Test building a review from an array of plugin IDs.
   */
  public function testFromIds(): void {
    $reviews = $this->builder->fromIds(array_keys($this->reviews));
    self::assertSame(['one', 'two', 'three.a', 'three.b'], array_keys($reviews));
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $reviews);
  }

  /**
   * Test building a review from a plugin.
   */
  public function testFromPlugin(): void {
    $review = $this->builder->fromPlugin($this->reviews['one']);
    self::assertSame(['one'], array_keys($review));
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $review);

    $reviews = $this->builder->fromPlugin($this->reviews['three']);
    self::assertSame(['three.a', 'three.b'], array_keys($reviews));
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $reviews);
  }

  /**
   * Test building a review from an array of plugins.
   */
  public function testFromPlugins(): void {
    $reviews = $this->builder->fromPlugins($this->reviews);
    self::assertSame(['one', 'two', 'three.a', 'three.b'], array_keys($reviews));
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $reviews);
  }

}

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

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