test persisted set ordering
This commit is contained in:
parent
7579c3c1b3
commit
fde12ad561
2 changed files with 227 additions and 0 deletions
|
|
@ -73,6 +73,98 @@ class SetsEndpointTest extends TestCase
|
|||
]);
|
||||
}
|
||||
|
||||
public function testReorderSetsRequiresAuthentication(): void
|
||||
{
|
||||
$response = $this->putJson('/api/sets/order', [
|
||||
'setIds' => [1, 2],
|
||||
]);
|
||||
|
||||
$response->assertUnauthorized();
|
||||
$response->assertExactJson([
|
||||
'error' => 'unauthenticated',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testAuthenticatedReorderSetsPersistsPublicOrder(): void
|
||||
{
|
||||
$setRepository = app(SetRepository::class);
|
||||
$firstSet = $setRepository->create(new CreateSetDto(
|
||||
name: 'First Set',
|
||||
description: 'First set description',
|
||||
iconImageUrl: '/assets/first.png',
|
||||
));
|
||||
$secondSet = $setRepository->create(new CreateSetDto(
|
||||
name: 'Second Set',
|
||||
description: 'Second set description',
|
||||
iconImageUrl: '/assets/second.png',
|
||||
));
|
||||
$thirdSet = $setRepository->create(new CreateSetDto(
|
||||
name: 'Third Set',
|
||||
description: 'Third set description',
|
||||
iconImageUrl: '/assets/third.png',
|
||||
));
|
||||
$this->createSession('valid-token');
|
||||
|
||||
$response = $this->withCredentials()
|
||||
->withUnencryptedCookie('auth_token', 'valid-token')
|
||||
->putJson('/api/sets/order', [
|
||||
'setIds' => [
|
||||
$thirdSet->getId(),
|
||||
$firstSet->getId(),
|
||||
$secondSet->getId(),
|
||||
],
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonCount(3, 'sets');
|
||||
$response->assertJsonPath('sets.0.id', $thirdSet->getId());
|
||||
$response->assertJsonPath('sets.1.id', $firstSet->getId());
|
||||
$response->assertJsonPath('sets.2.id', $secondSet->getId());
|
||||
|
||||
$publicResponse = $this->getJson('/api/sets');
|
||||
$publicResponse->assertOk();
|
||||
$publicResponse->assertJsonPath('sets.0.id', $thirdSet->getId());
|
||||
$publicResponse->assertJsonPath('sets.1.id', $firstSet->getId());
|
||||
$publicResponse->assertJsonPath('sets.2.id', $secondSet->getId());
|
||||
}
|
||||
|
||||
public function testCreatedSetAppendsToSavedOrder(): void
|
||||
{
|
||||
$setRepository = app(SetRepository::class);
|
||||
$firstSet = $setRepository->create(new CreateSetDto(
|
||||
name: 'First Set',
|
||||
description: 'First set description',
|
||||
iconImageUrl: '/assets/first.png',
|
||||
));
|
||||
$secondSet = $setRepository->create(new CreateSetDto(
|
||||
name: 'Second Set',
|
||||
description: 'Second set description',
|
||||
iconImageUrl: '/assets/second.png',
|
||||
));
|
||||
$this->createSession('valid-token');
|
||||
$this->withCredentials()
|
||||
->withUnencryptedCookie('auth_token', 'valid-token')
|
||||
->putJson('/api/sets/order', [
|
||||
'setIds' => [
|
||||
$secondSet->getId(),
|
||||
$firstSet->getId(),
|
||||
],
|
||||
])
|
||||
->assertOk();
|
||||
|
||||
$thirdSet = $setRepository->create(new CreateSetDto(
|
||||
name: 'Third Set',
|
||||
description: 'Third set description',
|
||||
iconImageUrl: '/assets/third.png',
|
||||
));
|
||||
|
||||
$publicResponse = $this->getJson('/api/sets');
|
||||
$publicResponse->assertOk();
|
||||
$publicResponse->assertJsonPath('sets.0.id', $secondSet->getId());
|
||||
$publicResponse->assertJsonPath('sets.1.id', $firstSet->getId());
|
||||
$publicResponse->assertJsonPath('sets.2.id', $thirdSet->getId());
|
||||
}
|
||||
|
||||
public function testCreateSetRequiresAuthentication(): void
|
||||
{
|
||||
$response = $this->postJson('/api/sets', [
|
||||
|
|
|
|||
135
backend/tests/Unit/Set/UseCases/ReorderSetsTest.php
Normal file
135
backend/tests/Unit/Set/UseCases/ReorderSetsTest.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue