108 lines
3.1 KiB
PHP
108 lines
3.1 KiB
PHP
<?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\UpdateIconImage;
|
|
use App\Set\UseCases\UpdateSet\UpdateIconImageRequest;
|
|
use Tests\Fakes\FakeFilesystem;
|
|
use Tests\Fakes\FakeSetRepository;
|
|
use Tests\TestCase;
|
|
|
|
class UpdateIconImageTest extends TestCase
|
|
{
|
|
private FakeSetRepository $setRepository;
|
|
|
|
private FakeFilesystem $filesystem;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->setRepository = new FakeSetRepository();
|
|
$this->filesystem = new FakeFilesystem();
|
|
}
|
|
|
|
public function testUpdatesOnlyIconAndDeletesOldIcon(): 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 testRejectsMissingFile(): 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 testRejectsInvalidMimeType(): 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',
|
|
));
|
|
}
|
|
}
|