181 lines
5.6 KiB
PHP
181 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Set\UseCases;
|
|
|
|
use App\Element\CreateElementDto;
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\NotFoundException;
|
|
use App\Set\CreateSetDto;
|
|
use App\Set\Set as DomainSet;
|
|
use App\Set\UseCases\UpdateSet\UpdateSet;
|
|
use App\Set\UseCases\UpdateSet\UpdateSetRequest;
|
|
use Tests\Fakes\FakeElementRepository;
|
|
use Tests\Fakes\FakeFilesystem;
|
|
use Tests\Fakes\FakeSetRepository;
|
|
use Tests\TestCase;
|
|
|
|
class UpdateSetTest extends TestCase
|
|
{
|
|
private FakeSetRepository $setRepository;
|
|
|
|
private FakeElementRepository $elementRepository;
|
|
|
|
private FakeFilesystem $filesystem;
|
|
|
|
private UpdateSet $updateSet;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->setRepository = new FakeSetRepository();
|
|
$this->elementRepository = new FakeElementRepository();
|
|
$this->filesystem = new FakeFilesystem();
|
|
$this->updateSet = new UpdateSet(
|
|
$this->setRepository,
|
|
$this->filesystem,
|
|
);
|
|
}
|
|
|
|
public function testUpdatesSetWithoutChangingRootElement(): void
|
|
{
|
|
$set = $this->setRepository->create(new CreateSetDto(
|
|
name: 'Original Set',
|
|
description: 'Original set description',
|
|
iconImageUrl: 'set-icons/original.png',
|
|
));
|
|
$rootElement = $this->elementRepository->create(new CreateElementDto(
|
|
set: $set,
|
|
title: 'Original Root',
|
|
description: 'Original root description',
|
|
iconImageUrl: 'set-icons/original.png',
|
|
richText: '',
|
|
shortPdfPath: null,
|
|
longPdfPath: null,
|
|
youtubeUrl: null,
|
|
parentElement: null,
|
|
));
|
|
|
|
$updatedSet = $this->updateSet->execute(new UpdateSetRequest(
|
|
id: $set->getId(),
|
|
name: 'Updated Set',
|
|
description: 'Updated set description',
|
|
iconImageContents: null,
|
|
iconImageOriginalName: null,
|
|
iconImageMimeType: null,
|
|
iconImageSizeBytes: null,
|
|
));
|
|
|
|
$this->assertSame('Updated Set', $updatedSet->getName());
|
|
$this->assertSame(
|
|
'Updated set description',
|
|
$updatedSet->getDescription(),
|
|
);
|
|
$this->assertSame(
|
|
'set-icons/original.png',
|
|
$updatedSet->getIconImageUrl(),
|
|
);
|
|
|
|
$unchangedRootElement = $this->elementRepository->find(
|
|
$rootElement->getId(),
|
|
);
|
|
$this->assertNotNull($unchangedRootElement);
|
|
$this->assertSame('Original Root', $unchangedRootElement->getTitle());
|
|
$this->assertSame(
|
|
'Original root description',
|
|
$unchangedRootElement->getDescription(),
|
|
);
|
|
$this->assertSame(
|
|
'set-icons/original.png',
|
|
$unchangedRootElement->getIconImageUrl(),
|
|
);
|
|
}
|
|
|
|
public function testReplacesSetIconAndDeletesOldManagedIcon(): 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: 'Updated set description',
|
|
iconImageContents: 'image contents',
|
|
iconImageOriginalName: 'updated.png',
|
|
iconImageMimeType: 'image/png',
|
|
iconImageSizeBytes: 13,
|
|
));
|
|
|
|
$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 testRequiresName(): void
|
|
{
|
|
$set = $this->createSet();
|
|
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('name is required');
|
|
|
|
$this->updateSet->execute(new UpdateSetRequest(
|
|
id: $set->getId(),
|
|
name: '',
|
|
description: 'Updated set description',
|
|
iconImageContents: null,
|
|
iconImageOriginalName: null,
|
|
iconImageMimeType: null,
|
|
iconImageSizeBytes: null,
|
|
));
|
|
}
|
|
|
|
public function testRequiresDescription(): void
|
|
{
|
|
$set = $this->createSet();
|
|
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('description is required');
|
|
|
|
$this->updateSet->execute(new UpdateSetRequest(
|
|
id: $set->getId(),
|
|
name: 'Updated Set',
|
|
description: '',
|
|
iconImageContents: null,
|
|
iconImageOriginalName: null,
|
|
iconImageMimeType: null,
|
|
iconImageSizeBytes: null,
|
|
));
|
|
}
|
|
|
|
public function testThrowsWhenSetIsMissing(): void
|
|
{
|
|
$this->expectException(NotFoundException::class);
|
|
$this->expectExceptionMessage('Set not found');
|
|
|
|
$this->updateSet->execute(new UpdateSetRequest(
|
|
id: 999,
|
|
name: 'Updated Set',
|
|
description: 'Updated set description',
|
|
iconImageContents: null,
|
|
iconImageOriginalName: null,
|
|
iconImageMimeType: null,
|
|
iconImageSizeBytes: null,
|
|
));
|
|
}
|
|
|
|
private function createSet(): DomainSet
|
|
{
|
|
return $this->setRepository->create(new CreateSetDto(
|
|
name: 'Original Set',
|
|
description: 'Original set description',
|
|
iconImageUrl: 'set-icons/original.png',
|
|
));
|
|
}
|
|
}
|