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

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

declare(strict_types=1);

namespace Drupal\Tests\reviewer\Kernel\Review;

use Drupal\KernelTests\KernelTestBase;
use Drupal\reviewer\Plugin\reviewer\Review\ReviewPluginInterface;
use Drupal\reviewer\Plugin\ReviewManagerInterface;
use Drupal\reviewer\Reviewer\Action;
use Drupal\reviewer\Reviewer\Review\ReviewBuilderInterface;
use Drupal\reviewer\Reviewer\Review\ReviewInterface;
use Drupal\reviewer\Reviewer\Review\ReviewRunner;
use Drupal\reviewer\Reviewer\Review\ReviewRunnerInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Test runner methods.
 */
#[Group('reviewer')]
#[CoversClass(ReviewRunner::class)]
class ReviewRunnerTest extends KernelTestBase {

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

  private readonly ReviewRunnerInterface $runner;

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

    $review_builder = $this->createMock(ReviewBuilderInterface::class);
    $review_builder->method('fromIds')->willReturnCallback($this->testReviewsFromIds(...));
    $review_builder->method('fromPlugins')->willReturnCallback($this->testReviewsFromPlugins(...));

    $review_manager = $this->createMock(ReviewManagerInterface::class);
    $review_manager->method('getDefinitions')->willReturn([
      'review' => [],
      'entity_review' => [],
    ]);

    $this->runner = new ReviewRunner(
      $review_builder,
      $this->container->get('reviewer.evaluator.bundle'),
      $review_manager,
    );
  }

  /**
   * Test running review plugins from review plugin IDs.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\reviewer\Exception\NoChecklistsException
   */
  public function testRunIds(): void {
    self::assertSame(
      ['entity_review.one', 'entity_review.two'],
      array_keys($this->runner->runIds(Action::Check, ['entity_review'])),
    );
    self::assertSame(
      ['entity_review.one'],
      array_keys($this->runner->runIds(Action::Check, ['entity_review'], ['one'])),
    );
    self::assertSame(
      ['entity_review.one', 'entity_review.two'],
      array_keys($this->runner->runIds(Action::Check, ['entity_review'], ['one', 'two'])),
    );
    self::assertSame(
      ['review'],
      array_keys($this->runner->runIds(Action::Check, ['review'])),
    );

    $reviews = $this->runner->runIds(Action::Check);
    self::assertCount(3, $reviews);
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $reviews);
  }

  /**
   * Test running review plugins.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\reviewer\Exception\NoChecklistsException
   * @throws \PHPUnit\Framework\MockObject\Exception
   */
  public function testRunPlugins(): void {
    $review = $this->createMock(ReviewPluginInterface::class);
    $review->method('getPluginId')->willReturn('review');
    $entity_review = $this->createMock(ReviewPluginInterface::class);
    $entity_review->method('getPluginId')->willReturn('entity_review');

    self::assertSame(
      ['entity_review.one', 'entity_review.two'],
      array_keys($this->runner->runPlugins(Action::Check, [$entity_review])),
    );
    self::assertSame(
      ['entity_review.one', 'entity_review.two'],
      array_keys($this->runner->runPlugins(Action::Check, [$entity_review], ['one', 'two'])),
    );
    self::assertSame(
      ['entity_review.one'],
      array_keys($this->runner->runPlugins(Action::Check, [$entity_review], ['one'])),
    );
    self::assertSame(
      ['review'],
      array_keys($this->runner->runPlugins(Action::Check, [$review])),
    );

    $reviews = $this->runner->runPlugins(Action::Check);
    self::assertCount(3, $reviews);
    self::assertContainsOnlyInstancesOf(ReviewInterface::class, $reviews);
  }

  /**
   * Get mock reviews from test IDs.
   *
   * @param string[] $ids
   *
   * @return array<string, \Drupal\reviewer\Reviewer\Review\ReviewInterface>
   *
   * @throws \PHPUnit\Framework\MockObject\Exception
   */
  private function testReviewsFromIds(array $ids): array {
    $reviews = [];
    foreach ($ids as $id) {
      $review = $this->createMock(ReviewInterface::class);

      if ($id === 'review') {
        $review->method('getId')->willReturn($id);
        $reviews[$id] = $review;
        continue;
      }

      $review->method('getId')->willReturn("$id.one");
      $reviews["$id.one"] = $review;

      $review = $this->createMock(ReviewInterface::class);
      $review->method('getId')->willReturn("$id.two");
      $reviews["$id.two"] = $review;
    }

    return $reviews;
  }

  /**
   * Get mock reviews from test plugins.
   *
   * @param \Drupal\reviewer\Plugin\reviewer\Review\ReviewPluginInterface[] $plugins
   *
   * @return array<string, \Drupal\reviewer\Reviewer\Review\ReviewInterface>
   *
   * @throws \PHPUnit\Framework\MockObject\Exception
   */
  private function testReviewsFromPlugins(array $plugins): array {
    return $this->testReviewsFromIds(array_map(fn(ReviewPluginInterface $review) => $review->getPluginId(), $plugins));
  }

}

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

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