test set update routing

This commit is contained in:
Yisroel Baum 2026-07-03 13:59:17 +03:00
parent f8666454c3
commit b2e39a98e4
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 242 additions and 3 deletions

View file

@ -0,0 +1,184 @@
<?php
namespace Tests\Unit\Set\UseCases;
use App\Exceptions\BadRequestException;
use App\Set\CreateSetDto;
use App\Set\Set as DomainSet;
use App\Set\UseCases\UpdateSet\UpdateDescription;
use App\Set\UseCases\UpdateSet\UpdateDescriptionRequest;
use App\Set\UseCases\UpdateSet\UpdateIconImage;
use App\Set\UseCases\UpdateSet\UpdateIconImageRequest;
use App\Set\UseCases\UpdateSet\UpdateName;
use App\Set\UseCases\UpdateSet\UpdateNameRequest;
use Tests\Fakes\FakeFilesystem;
use Tests\Fakes\FakeSetRepository;
use Tests\TestCase;
class UpdateSetFieldsTest extends TestCase
{
private FakeSetRepository $setRepository;
private FakeFilesystem $filesystem;
protected function setUp(): void
{
$this->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',
));
}
}