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() ); } }