setRepository = new FakeSetRepository(); $this->reorderSets = new ReorderSets($this->setRepository); } public function testReordersEverySet(): void { $firstSet = $this->createSet('First Set'); $secondSet = $this->createSet('Second Set'); $thirdSet = $this->createSet('Third Set'); $sets = $this->reorderSets->execute(new ReorderSetsRequest( setIds: [ $thirdSet->getId(), $firstSet->getId(), $secondSet->getId(), ], )); $expectedSetIds = [ $thirdSet->getId(), $firstSet->getId(), $secondSet->getId(), ]; $this->assertSame($expectedSetIds, $this->setIds($sets)); $this->assertSame( $expectedSetIds, $this->setIds($this->setRepository->getAll()), ); } public function testThrowsWhenSetIdsAreMissing(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('setIds is required'); $this->reorderSets->execute(new ReorderSetsRequest(setIds: null)); } public function testThrowsWhenSetIdsAreNotIntegers(): void { $firstSet = $this->createSet('First Set'); $this->expectException(BadRequestException::class); $this->expectExceptionMessage('setIds must contain integers'); $this->reorderSets->execute(new ReorderSetsRequest( setIds: [$firstSet->getId(), 'invalid'], )); } public function testThrowsWhenSetIdsContainDuplicates(): void { $firstSet = $this->createSet('First Set'); $secondSet = $this->createSet('Second Set'); $this->expectException(BadRequestException::class); $this->expectExceptionMessage('Set order contains duplicate ids'); $this->reorderSets->execute(new ReorderSetsRequest( setIds: [ $firstSet->getId(), $firstSet->getId(), $secondSet->getId(), ], )); } public function testThrowsWhenSetOrderContainsUnknownSet(): void { $firstSet = $this->createSet('First Set'); $this->expectException(BadRequestException::class); $this->expectExceptionMessage('Set order contains invalid set'); $this->reorderSets->execute(new ReorderSetsRequest( setIds: [$firstSet->getId(), 999], )); } public function testThrowsWhenSetOrderOmitsSet(): void { $firstSet = $this->createSet('First Set'); $this->createSet('Second Set'); $this->expectException(BadRequestException::class); $this->expectExceptionMessage('Set order must include every set'); $this->reorderSets->execute(new ReorderSetsRequest( setIds: [$firstSet->getId()], )); } private function createSet(string $name): DomainSet { return $this->setRepository->create(new CreateSetDto( name: $name, description: "$name description", iconImageUrl: "/assets/$name.png", )); } /** * @param DomainSet[] $sets * @return int[] */ private function setIds(array $sets): array { $setIds = []; foreach ($sets as $set) { $setIds[] = $set->getId(); } return $setIds; } }