From b2e39a98e49bf8b72f9a19e480f1ba1cb7ad0685 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Fri, 3 Jul 2026 13:59:17 +0300 Subject: [PATCH] test set update routing --- .../Unit/Set/UseCases/UpdateSetFieldsTest.php | 184 ++++++++++++++++++ .../tests/Unit/Set/UseCases/UpdateSetTest.php | 61 +++++- 2 files changed, 242 insertions(+), 3 deletions(-) create mode 100644 backend/tests/Unit/Set/UseCases/UpdateSetFieldsTest.php diff --git a/backend/tests/Unit/Set/UseCases/UpdateSetFieldsTest.php b/backend/tests/Unit/Set/UseCases/UpdateSetFieldsTest.php new file mode 100644 index 0000000..7ef3321 --- /dev/null +++ b/backend/tests/Unit/Set/UseCases/UpdateSetFieldsTest.php @@ -0,0 +1,184 @@ +setRepository = new FakeSetRepository(); + $this->filesystem = new FakeFilesystem(); + } + + public function testUpdateNameUpdatesOnlyName(): void + { + $set = $this->createSet(); + $updateName = new UpdateName($this->setRepository); + + $updatedSet = $updateName->execute(new UpdateNameRequest( + id: $set->getId(), + name: 'Updated Set', + )); + + $this->assertSame('Updated Set', $updatedSet->getName()); + $this->assertSame( + $set->getDescription(), + $updatedSet->getDescription(), + ); + $this->assertSame( + $set->getIconImageUrl(), + $updatedSet->getIconImageUrl(), + ); + } + + public function testUpdateNameRejectsBlankName(): void + { + $set = $this->createSet(); + $updateName = new UpdateName($this->setRepository); + + $this->expectException(BadRequestException::class); + $this->expectExceptionMessage('name is required'); + + $updateName->execute(new UpdateNameRequest( + id: $set->getId(), + name: '', + )); + } + + public function testUpdateDescriptionUpdatesOnlyDescription(): void + { + $set = $this->createSet(); + $updateDescription = new UpdateDescription($this->setRepository); + + $updatedSet = $updateDescription->execute( + new UpdateDescriptionRequest( + id: $set->getId(), + description: 'Updated set description', + ) + ); + + $this->assertSame( + 'Updated set description', + $updatedSet->getDescription(), + ); + $this->assertSame($set->getName(), $updatedSet->getName()); + $this->assertSame( + $set->getIconImageUrl(), + $updatedSet->getIconImageUrl(), + ); + } + + public function testUpdateDescriptionRejectsBlankDescription(): void + { + $set = $this->createSet(); + $updateDescription = new UpdateDescription($this->setRepository); + + $this->expectException(BadRequestException::class); + $this->expectExceptionMessage('description is required'); + + $updateDescription->execute(new UpdateDescriptionRequest( + id: $set->getId(), + description: '', + )); + } + + public function testUpdateIconImageUpdatesOnlyIconAndDeletesOldIcon(): void + { + $set = $this->createSet(); + $updateIconImage = new UpdateIconImage( + $this->setRepository, + $this->filesystem, + ); + + $updatedSet = $updateIconImage->execute(new UpdateIconImageRequest( + id: $set->getId(), + fileContents: 'image contents', + fileOriginalName: 'updated.png', + fileMimeType: 'image/png', + fileSizeBytes: 13, + )); + + $this->assertSame($set->getName(), $updatedSet->getName()); + $this->assertSame( + $set->getDescription(), + $updatedSet->getDescription(), + ); + $this->assertSame( + 'set-icons/fake-updated.png', + $updatedSet->getIconImageUrl(), + ); + $this->assertCount(1, $this->filesystem->uploads); + $this->assertSame('set-icons', $this->filesystem->uploads[0]['folder']); + $this->assertSame([ + 'set-icons/original.png', + ], $this->filesystem->deletedPaths); + } + + public function testUpdateIconImageRejectsMissingFile(): void + { + $set = $this->createSet(); + $updateIconImage = new UpdateIconImage( + $this->setRepository, + $this->filesystem, + ); + + $this->expectException(BadRequestException::class); + $this->expectExceptionMessage('icon image is required'); + + $updateIconImage->execute(new UpdateIconImageRequest( + id: $set->getId(), + fileContents: null, + fileOriginalName: null, + fileMimeType: null, + fileSizeBytes: null, + )); + } + + public function testUpdateIconImageRejectsInvalidMimeType(): void + { + $set = $this->createSet(); + $updateIconImage = new UpdateIconImage( + $this->setRepository, + $this->filesystem, + ); + + $this->expectException(BadRequestException::class); + $this->expectExceptionMessage( + 'icon image must be a jpeg, png or webp image', + ); + + $updateIconImage->execute(new UpdateIconImageRequest( + id: $set->getId(), + fileContents: 'image contents', + fileOriginalName: 'updated.gif', + fileMimeType: 'image/gif', + fileSizeBytes: 13, + )); + } + + private function createSet(): DomainSet + { + return $this->setRepository->create(new CreateSetDto( + name: 'Original Set', + description: 'Original set description', + iconImageUrl: 'set-icons/original.png', + )); + } +} diff --git a/backend/tests/Unit/Set/UseCases/UpdateSetTest.php b/backend/tests/Unit/Set/UseCases/UpdateSetTest.php index a1bba41..0104c61 100644 --- a/backend/tests/Unit/Set/UseCases/UpdateSetTest.php +++ b/backend/tests/Unit/Set/UseCases/UpdateSetTest.php @@ -7,6 +7,9 @@ use App\Exceptions\BadRequestException; use App\Exceptions\NotFoundException; use App\Set\CreateSetDto; use App\Set\Set as DomainSet; +use App\Set\UseCases\UpdateSet\UpdateDescription; +use App\Set\UseCases\UpdateSet\UpdateIconImage; +use App\Set\UseCases\UpdateSet\UpdateName; use App\Set\UseCases\UpdateSet\UpdateSet; use App\Set\UseCases\UpdateSet\UpdateSetRequest; use Tests\Fakes\FakeElementRepository; @@ -30,8 +33,13 @@ class UpdateSetTest extends TestCase $this->elementRepository = new FakeElementRepository(); $this->filesystem = new FakeFilesystem(); $this->updateSet = new UpdateSet( + new UpdateName($this->setRepository), + new UpdateDescription($this->setRepository), + new UpdateIconImage( + $this->setRepository, + $this->filesystem, + ), $this->setRepository, - $this->filesystem, ); } @@ -89,6 +97,35 @@ class UpdateSetTest extends TestCase ); } + public function testUpdatesOnlySuppliedFields(): void + { + $set = $this->setRepository->create(new CreateSetDto( + name: 'Original Set', + description: 'Original set description', + iconImageUrl: 'set-icons/original.png', + )); + + $updatedSet = $this->updateSet->execute(new UpdateSetRequest( + id: $set->getId(), + name: 'Updated Set', + description: null, + iconImageContents: null, + iconImageOriginalName: null, + iconImageMimeType: null, + iconImageSizeBytes: null, + )); + + $this->assertSame('Updated Set', $updatedSet->getName()); + $this->assertSame( + 'Original set description', + $updatedSet->getDescription(), + ); + $this->assertSame( + 'set-icons/original.png', + $updatedSet->getIconImageUrl(), + ); + } + public function testReplacesSetIconAndDeletesOldManagedIcon(): void { $set = $this->setRepository->create(new CreateSetDto( @@ -118,6 +155,24 @@ class UpdateSetTest extends TestCase ], $this->filesystem->deletedPaths); } + public function testRejectsNothingToUpdate(): void + { + $set = $this->createSet(); + + $this->expectException(BadRequestException::class); + $this->expectExceptionMessage('nothing to update'); + + $this->updateSet->execute(new UpdateSetRequest( + id: $set->getId(), + name: null, + description: null, + iconImageContents: null, + iconImageOriginalName: null, + iconImageMimeType: null, + iconImageSizeBytes: null, + )); + } + public function testRequiresName(): void { $set = $this->createSet(); @@ -128,7 +183,7 @@ class UpdateSetTest extends TestCase $this->updateSet->execute(new UpdateSetRequest( id: $set->getId(), name: '', - description: 'Updated set description', + description: null, iconImageContents: null, iconImageOriginalName: null, iconImageMimeType: null, @@ -145,7 +200,7 @@ class UpdateSetTest extends TestCase $this->updateSet->execute(new UpdateSetRequest( id: $set->getId(), - name: 'Updated Set', + name: null, description: '', iconImageContents: null, iconImageOriginalName: null,