reviewer-1.2.x-dev/tests/src/Kernel/Result/CollectionResultTest.php
tests/src/Kernel/Result/CollectionResultTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\reviewer\Kernel\Result;
use Drupal\KernelTests\KernelTestBase;
use Drupal\reviewer\Reviewer\Result\CollectionResult;
use Drupal\reviewer\Reviewer\Result\CollectionResultInterface;
use Drupal\reviewer\Reviewer\Result\IndividualResultInterface;
use Drupal\reviewer\Reviewer\Status\Status;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
/**
* Test result methods.
*/
#[Group('reviewer')]
#[CoversClass(CollectionResult::class)]
final class CollectionResultTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'reviewer',
];
private readonly IndividualResultInterface $pass;
private readonly IndividualResultInterface $fail;
private readonly IndividualResultInterface $error;
private readonly CollectionResultInterface $collection;
/**
* Checklist constructor arguments.
*
* @var array{
* \Drupal\reviewer\Reviewer\Status\StatusFactoryInterface,
* string,
* array<string, \Drupal\reviewer\Reviewer\Result\ResultInterface>
* }
*/
private readonly array $collectionConstructorArguments;
/**
* {@inheritdoc}
*
* @throws \Exception
* @throws \PHPUnit\Framework\MockObject\Exception
*/
protected function setUp(): void {
parent::setUp();
$this->collectionConstructorArguments = [
$this->container->get('reviewer.factory.status'),
'collection',
[],
];
$this->collection = new CollectionResult(...$this->collectionConstructorArguments);
$this->pass = $this->createMock(IndividualResultInterface::class);
$this->pass->method('getId')->willReturn('pass');
$this->pass->method('getStatus')->willReturn(Status::Pass);
$this->fail = $this->createMock(IndividualResultInterface::class);
$this->fail->method('getId')->willReturn('fail');
$this->fail->method('getStatus')->willReturn(Status::Fail);
$this->error = $this->createMock(IndividualResultInterface::class);
$this->error->method('getId')->willReturn('error');
$this->error->method('getStatus')->willReturn(Status::Error);
}
/**
* Test getting the result status.
*/
public function testGetStatus(): void {
$this->collection->add($this->pass);
$this->collection->add($this->fail);
$this->collection->add($this->error);
self::assertSame(Status::Error, $this->collection->getStatus());
}
/**
* Test adding a collection item.
*/
public function testAdd(): void {
$this->collection->add($this->pass);
self::assertSame(['pass' => $this->pass], $this->collection->getAll());
$this->collection->add($this->fail);
self::assertSame(['pass' => $this->pass, 'fail' => $this->fail], $this->collection->getAll());
}
/**
* Test remove a collection item.
*/
public function testRemove(): void {
$this->collection->add($this->pass);
$this->collection->add($this->fail);
$this->collection->add($this->error);
$this->collection->remove($this->fail);
self::assertSame(['pass' => $this->pass, 'error' => $this->error], $this->collection->getAll());
}
/**
* Test getting all individual results.
*/
public function testGetIndividualResults(): void {
$grandchild_collection = new CollectionResult(...$this->collectionConstructorArguments);
$grandchild_collection->add($this->pass);
$child_collection = new CollectionResult(...$this->collectionConstructorArguments);
$child_collection->add($this->fail);
$child_collection->add($grandchild_collection);
$this->collection->add($this->error);
$this->collection->add($child_collection);
self::assertSame(
['error' => $this->error, 'fail' => $this->fail, 'pass' => $this->pass],
$this->collection->getIndividualResults(),
);
}
}
