103 lines
3.5 KiB
PHP
103 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Service\Season;
|
|
|
|
use App\Config\SeasonTask;
|
|
use App\Entity\SeasonAssignment;
|
|
use App\Entity\SeasonAssignmentTask;
|
|
use App\Service\Season\SeasonTaskSynchronizer;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* What makes the enum extensible is that this fills in whatever an assignment is missing, so
|
|
* that is what is asserted: new entries appear on assignments that already exist, and rows of
|
|
* an entry that was removed are left alone rather than deleted.
|
|
*/
|
|
class SeasonTaskSynchronizerTest extends TestCase
|
|
{
|
|
private EntityManagerInterface&MockObject $entityManager;
|
|
private SeasonTaskSynchronizer $synchronizer;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->entityManager = $this->createMock(EntityManagerInterface::class);
|
|
$this->synchronizer = new SeasonTaskSynchronizer($this->entityManager);
|
|
}
|
|
|
|
public function testItCreatesOneRowPerEntryInEnumOrder(): void
|
|
{
|
|
$seasonAssignment = new SeasonAssignment();
|
|
|
|
$this->entityManager->expects($this->exactly(count(SeasonTask::cases())))->method('persist');
|
|
$this->entityManager->expects($this->once())->method('flush');
|
|
|
|
$this->synchronizer->sync($seasonAssignment);
|
|
|
|
$this->assertSame(
|
|
array_map(static fn (SeasonTask $task): string => $task->value, SeasonTask::cases()),
|
|
$this->keysOf($seasonAssignment)
|
|
);
|
|
}
|
|
|
|
public function testItOnlyCreatesWhatIsMissing(): void
|
|
{
|
|
$seasonAssignment = new SeasonAssignment();
|
|
$seasonAssignment->addTask((new SeasonAssignmentTask())->setTaskKey(SeasonTask::CONTRACT->value));
|
|
|
|
$this->entityManager->expects($this->exactly(count(SeasonTask::cases()) - 1))->method('persist');
|
|
|
|
$this->synchronizer->sync($seasonAssignment);
|
|
|
|
$this->assertCount(count(SeasonTask::cases()), $seasonAssignment->getTasks());
|
|
$this->assertSame(
|
|
1,
|
|
count(array_filter(
|
|
$this->keysOf($seasonAssignment),
|
|
static fn (string $key): bool => SeasonTask::CONTRACT->value === $key
|
|
))
|
|
);
|
|
}
|
|
|
|
public function testASyncedAssignmentIsNotFlushedAgain(): void
|
|
{
|
|
$seasonAssignment = new SeasonAssignment();
|
|
|
|
foreach (SeasonTask::cases() as $case) {
|
|
$seasonAssignment->addTask((new SeasonAssignmentTask())->setTaskKey($case->value));
|
|
}
|
|
|
|
$this->entityManager->expects($this->never())->method('persist');
|
|
$this->entityManager->expects($this->never())->method('flush');
|
|
|
|
$this->synchronizer->sync($seasonAssignment);
|
|
}
|
|
|
|
public function testARowOfAnUnknownEntryIsKept(): void
|
|
{
|
|
$seasonAssignment = new SeasonAssignment();
|
|
$seasonAssignment->addTask((new SeasonAssignmentTask())->setTaskKey('entry_removed_from_the_enum'));
|
|
|
|
$this->entityManager->expects($this->never())->method('remove');
|
|
|
|
$this->synchronizer->sync($seasonAssignment);
|
|
|
|
$this->assertCount(count(SeasonTask::cases()) + 1, $seasonAssignment->getTasks());
|
|
$this->assertContains('entry_removed_from_the_enum', $this->keysOf($seasonAssignment));
|
|
}
|
|
|
|
/**
|
|
* @return string[]
|
|
*/
|
|
private function keysOf(SeasonAssignment $seasonAssignment): array
|
|
{
|
|
return array_map(
|
|
static fn (SeasonAssignmentTask $task): string => $task->getTaskKey(),
|
|
$seasonAssignment->getTasks()->toArray()
|
|
);
|
|
}
|
|
}
|