test persisted set ordering

This commit is contained in:
Yisroel Baum 2026-07-31 10:58:32 +03:00
parent 7579c3c1b3
commit fde12ad561
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 227 additions and 0 deletions

View file

@ -0,0 +1,135 @@
<?php
namespace Tests\Unit\Set\UseCases;
use App\Exceptions\BadRequestException;
use App\Set\CreateSetDto;
use App\Set\Set as DomainSet;
use App\Set\UseCases\ReorderSets\ReorderSets;
use App\Set\UseCases\ReorderSets\ReorderSetsRequest;
use Tests\Fakes\FakeSetRepository;
use Tests\TestCase;
class ReorderSetsTest extends TestCase
{
private FakeSetRepository $setRepository;
private ReorderSets $reorderSets;
protected function setUp(): void
{
$this->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;
}
}