reviewer-1.2.x-dev/tests/src/Kernel/IgnorerTest.php
tests/src/Kernel/IgnorerTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\reviewer\Kernel;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\reviewer\Exception\NotIgnoredException;
use Drupal\reviewer\Plugin\reviewer\Review\ReviewPluginInterface;
use Drupal\reviewer\Plugin\ReviewManagerInterface;
use Drupal\reviewer\Reviewer\Ignorer;
use Drupal\reviewer\Reviewer\IgnorerInterface;
use Drupal\reviewer\Reviewer\Result\ResultInterface;
use Drupal\reviewer\Reviewer\Status\Status;
use Drupal\reviewer\Reviewer\Status\StatusEvaluatorInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
/**
* Tests the ignorer.
*/
#[Group('reviewer')]
#[CoversClass(Ignorer::class)]
final class IgnorerTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'reviewer',
];
/**
* Ignored test data.
*
* @var array{id: string, reason: string}[]
*/
private readonly array $ignored;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->ignored = [
'five' => ['id' => 'five', 'reason' => ''],
'four' => ['id' => 'four', 'reason' => ''],
'one' => ['id' => 'one', 'reason' => 'reason one'],
'three' => ['id' => 'three', 'reason' => 'reason three'],
'two' => ['id' => 'two', 'reason' => 'reason two'],
];
}
/**
* Test getting ignored items.
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
public function testGetIgnored(): void {
$ignorer = $this->mockIgnorer(
[$this->ignored['one'], $this->ignored['three'], $this->ignored['two']],
[$this->ignored['five']],
);
self::assertSame(
[$this->ignored['five'], $this->ignored['one'], $this->ignored['three'], $this->ignored['two']],
$ignorer->getIgnored(),
);
}
/**
* Test getting ignored items.
*
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testGetConfigIgnored(): void {
$config = $this->createMock(ImmutableConfig::class);
$config->method('get')->willReturn([$this->ignored['one'], $this->ignored['two']]);
$config_factory = $this->createMock(ConfigFactoryInterface::class);
$config_factory->method('get')->willReturn($config);
$ignorer = new Ignorer(
$config_factory,
$this->createMock(ReviewManagerInterface::class),
$this->createMock(StatusEvaluatorInterface::class),
);
self::assertSame([$this->ignored['one'], $this->ignored['two']], $ignorer->getConfigIgnored());
}
/**
* Test getting ignored items.
*
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testGetPluginIgnored(): void {
$review = $this->createMock(ReviewPluginInterface::class);
$review->method('getIgnored')->willReturn([$this->ignored['one'], $this->ignored['two']]);
$review_manager = $this->createMock(ReviewManagerInterface::class);
$review_manager->method('createAllInstances')->willReturn([$review]);
$ignorer = new Ignorer(
$this->createMock(ConfigFactoryInterface::class),
$review_manager,
$this->createMock(StatusEvaluatorInterface::class),
);
self::assertSame([$this->ignored['one'], $this->ignored['two']], $ignorer->getCodeIgnored());
}
/**
* Test checking a result is ignored.
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
public function testIsIgnored(): void {
$ignorer = $this->mockIgnorer(
[$this->ignored['one'], $this->ignored['two']],
[$this->ignored['three'], $this->ignored['four']],
);
$config_ignored = $this->createMock(ResultInterface::class);
$config_ignored->method('getId')->willReturn('one');
$plugin_ignored = $this->createMock(ResultInterface::class);
$plugin_ignored->method('getId')->willReturn('three');
$not_ignored = $this->createMock(ResultInterface::class);
$not_ignored->method('getId')->willReturn('five');
self::assertTrue($ignorer->isIgnored($config_ignored));
self::assertTrue($ignorer->isIgnored($config_ignored->getId()));
self::assertTrue($ignorer->isIgnored($plugin_ignored));
self::assertTrue($ignorer->isIgnored($plugin_ignored->getId()));
self::assertFalse($ignorer->isIgnored($not_ignored));
self::assertFalse($ignorer->isIgnored($not_ignored->getId()));
}
/**
* Test checking a result is ignored in configuration.
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
public function testIsConfigIgnored(): void {
$ignorer = $this->mockIgnorer(
config_ignored: [$this->ignored['one'], $this->ignored['two']],
plugin_ignored: [$this->ignored['three'], $this->ignored['four']],
);
$ignored = $this->createMock(ResultInterface::class);
$ignored->method('getId')->willReturn('one');
$not_ignored = $this->createMock(ResultInterface::class);
$not_ignored->method('getId')->willReturn('three');
self::assertTrue($ignorer->isConfigIgnored($ignored));
self::assertTrue($ignorer->isConfigIgnored($ignored->getId()));
self::assertFalse($ignorer->isConfigIgnored($not_ignored));
self::assertFalse($ignorer->isConfigIgnored($not_ignored->getId()));
}
/**
* Test checking a result is ignored in the plugin definition.
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
public function testIsPluginIgnored(): void {
$ignorer = $this->mockIgnorer(
config_ignored: [$this->ignored['one'], $this->ignored['two']],
plugin_ignored: [$this->ignored['three'], $this->ignored['four']],
);
$ignored = $this->createMock(ResultInterface::class);
$ignored->method('getId')->willReturn('three');
$not_ignored = $this->createMock(ResultInterface::class);
$not_ignored->method('getId')->willReturn('one');
self::assertTrue($ignorer->isCodeIgnored($ignored));
self::assertTrue($ignorer->isCodeIgnored($ignored->getId()));
self::assertFalse($ignorer->isCodeIgnored($not_ignored));
self::assertFalse($ignorer->isCodeIgnored($not_ignored->getId()));
}
/**
* Test retrieving the ignored reason for results.
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
public function testIgnoredReason(): void {
$ignorer = $this->mockIgnorer([$this->ignored['one']]);
$ignored = $this->createMock(ResultInterface::class);
$ignored->method('getId')->willReturn('one');
$not_ignored = $this->createMock(ResultInterface::class);
$not_ignored->method('getId')->willReturn('not_ignored');
self::assertSame($this->ignored['one']['reason'], $ignorer->ignoredReason($ignored));
self::assertSame($this->ignored['one']['reason'], $ignorer->ignoredReason($ignored->getId()));
self::expectException(NotIgnoredException::class);
$ignorer->ignoredReason($not_ignored);
}
/**
* Test retrieving ignored results from a set of results.
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
public function testIgnoredResults(): void {
$ignorer = $this->mockIgnorer();
$ignored_failure = $this->createMock(ResultInterface::class);
$ignored_failure->method('getStatus')->willReturn(Status::IgnoredFailure);
$ignored_error = $this->createMock(ResultInterface::class);
$ignored_error->method('getStatus')->willReturn(Status::IgnoredError);
$not_ignored = $this->createMock(ResultInterface::class);
$not_ignored->method('getStatus')->willReturn(Status::Fail);
self::assertSame([$ignored_failure], $ignorer->ignoredResults([$ignored_failure]));
self::assertSame(
[$ignored_failure, $ignored_error],
$ignorer->ignoredResults([$ignored_failure, $ignored_error, $not_ignored]),
);
self::assertSame([], $ignorer->ignoredResults([$not_ignored]));
self::assertSame([], $ignorer->ignoredResults([]));
}
/**
* Test retrieving unignored results from a set of results.
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
public function testUnignoredResults(): void {
$ignorer = $this->mockIgnorer();
$ignored_failure = $this->createMock(ResultInterface::class);
$ignored_failure->method('getStatus')->willReturn(Status::IgnoredFailure);
$ignored_error = $this->createMock(ResultInterface::class);
$ignored_error->method('getStatus')->willReturn(Status::IgnoredError);
$not_ignored = $this->createMock(ResultInterface::class);
$not_ignored->method('getStatus')->willReturn(Status::Fail);
self::assertSame([$not_ignored], $ignorer->unignoredResults([$not_ignored]));
self::assertSame([], $ignorer->unignoredResults([$ignored_failure, $ignored_error]));
self::assertSame([], $ignorer->unignoredResults([]));
}
/**
* Test ignoring results.
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
public function testIgnore(): void {
$ignored_one = $this->createMock(ResultInterface::class);
$ignored_one->method('getId')->willReturn('one');
$ignored_two = $this->createMock(ResultInterface::class);
$ignored_two->method('getId')->willReturn('two');
$plugin_ignored = $this->createMock(ResultInterface::class);
$plugin_ignored->method('getId')->willReturn('five');
$review = $this->createMock(ReviewPluginInterface::class);
$review->method('getIgnored')->willReturn([$this->ignored['five']]);
$review_manager = $this->createMock(ReviewManagerInterface::class);
$review_manager->method('createAllInstances')->willReturn([$review]);
$ignorer = new Ignorer(
$this->container->get('config.factory'),
$review_manager,
$this->createMock(StatusEvaluatorInterface::class),
);
self::assertSame([$this->ignored['five']], $ignorer->getIgnored());
$ignorer->ignore($plugin_ignored);
self::assertSame([$this->ignored['five']], $ignorer->getIgnored());
$ignorer->ignore($ignored_two, 'Two');
self::assertSame([$this->ignored['five'], ['id' => 'two', 'reason' => 'Two']], $ignorer->getIgnored());
$ignorer->ignore($ignored_one, 'One');
self::assertSame(
[$this->ignored['five'], ['id' => 'one', 'reason' => 'One'], ['id' => 'two', 'reason' => 'Two']],
$ignorer->getIgnored(),
);
$ignorer->ignore('one', 'Reason');
self::assertSame(
[$this->ignored['five'], ['id' => 'one', 'reason' => 'Reason'], ['id' => 'two', 'reason' => 'Two']],
$ignorer->getIgnored(),
);
}
/**
* Test unignoring results.
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
public function testUnignore(): void {
$ignored = $this->ignored;
$config = $this->container->get('config.factory');
$config
->getEditable('reviewer.settings')
->set('ignored', $ignored)
->save();
$ignorer = new Ignorer(
$config,
$this->createMock(ReviewManagerInterface::class),
$this->createMock(StatusEvaluatorInterface::class),
);
self::assertSame($ignored, $ignorer->getIgnored());
$ignored_one = $this->createMock(ResultInterface::class);
$ignored_one->method('getId')->willReturn('one');
$ignorer->unignore($ignored_one);
unset($ignored['one']);
self::assertSame(array_values($ignored), $ignorer->getIgnored());
$ignorer->unignore('four');
unset($ignored['four']);
self::assertSame(array_values($ignored), $ignorer->getIgnored());
$ignored_three = $this->createMock(ResultInterface::class);
$ignored_three->method('getId')->willReturn('three');
$ignorer->unignore([$ignored_three, 'two']);
unset($ignored['three'], $ignored['two']);
self::assertSame(array_values($ignored), $ignorer->getIgnored());
}
/**
* Test unignoring all results.
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
public function testUnignoreAll(): void {
$ignored = $this->ignored;
$config = $this->container->get('config.factory');
$config
->getEditable('reviewer.settings')
->set('ignored', $ignored)
->save();
$ignorer = new Ignorer(
$config,
$this->createMock(ReviewManagerInterface::class),
$this->createMock(StatusEvaluatorInterface::class),
);
self::assertSame($ignored, $ignorer->getIgnored());
$ignorer->unignoreAll();
self::assertSame([], $ignorer->getIgnored());
}
/**
* Get the ignorer with specific ignored configuration IDs.
*
* @param array{id: string, reason: string}[] $config_ignored
* @param array{id: string, reason: string}[] $plugin_ignored
*
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Exception
*/
private function mockIgnorer(
array $config_ignored = [],
array $plugin_ignored = [],
): IgnorerInterface {
$config = $this->createMock(Config::class);
$config->method('get')->willReturn($config_ignored);
$config_factory = $this->createMock(ConfigFactoryInterface::class);
$config_factory->method('get')->willReturn($config);
$review = $this->createMock(ReviewPluginInterface::class);
$review->method('getIgnored')->willReturn($plugin_ignored);
$review_manager = $this->createMock(ReviewManagerInterface::class);
$review_manager->method('createAllInstances')->willReturn([$review]);
return new Ignorer(
$config_factory,
$review_manager,
$this->container->get('reviewer.evaluator.status'),
);
}
}
