test set deletion
This commit is contained in:
parent
53d4120e83
commit
ae48bdf93b
2 changed files with 205 additions and 0 deletions
140
backend/tests/Unit/Set/UseCases/DeleteSetTest.php
Normal file
140
backend/tests/Unit/Set/UseCases/DeleteSetTest.php
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Set\UseCases;
|
||||
|
||||
use App\Element\CreateElementDto;
|
||||
use App\Element\Element;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\NotFoundException;
|
||||
use App\Set\CreateSetDto;
|
||||
use App\Set\Set as DomainSet;
|
||||
use App\Set\UseCases\DeleteSet\DeleteSet;
|
||||
use App\Set\UseCases\DeleteSet\DeleteSetRequest;
|
||||
use Tests\Fakes\FakeElementRepository;
|
||||
use Tests\Fakes\FakeFilesystem;
|
||||
use Tests\Fakes\FakeSetRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DeleteSetTest extends TestCase
|
||||
{
|
||||
private FakeSetRepository $setRepository;
|
||||
|
||||
private FakeElementRepository $elementRepository;
|
||||
|
||||
private FakeFilesystem $filesystem;
|
||||
|
||||
private DeleteSet $deleteSet;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->setRepository = new FakeSetRepository();
|
||||
$this->elementRepository = new FakeElementRepository();
|
||||
$this->filesystem = new FakeFilesystem();
|
||||
$this->deleteSet = new DeleteSet(
|
||||
$this->setRepository,
|
||||
$this->elementRepository,
|
||||
$this->filesystem,
|
||||
);
|
||||
}
|
||||
|
||||
public function testDeletesSetElementTreeAndManagedFiles(): void
|
||||
{
|
||||
$set = $this->createSet('set-icons/set.png');
|
||||
$rootElement = $this->createElement(
|
||||
set: $set,
|
||||
parentElement: null,
|
||||
title: 'Root',
|
||||
iconImageUrl: 'set-icons/set.png',
|
||||
shortPdfPath: null,
|
||||
longPdfPath: null,
|
||||
);
|
||||
$childElement = $this->createElement(
|
||||
set: $set,
|
||||
parentElement: $rootElement,
|
||||
title: 'Child',
|
||||
iconImageUrl: 'element-icons/child.png',
|
||||
shortPdfPath: 'element-pdfs/short/child.pdf',
|
||||
longPdfPath: 'element-pdfs/long/child.pdf',
|
||||
);
|
||||
$grandchildElement = $this->createElement(
|
||||
set: $set,
|
||||
parentElement: $childElement,
|
||||
title: 'Grandchild',
|
||||
iconImageUrl: 'element-icons/grandchild.png',
|
||||
shortPdfPath: 'element-pdfs/short/grandchild.pdf',
|
||||
longPdfPath: 'element-pdfs/long/grandchild.pdf',
|
||||
);
|
||||
|
||||
$this->deleteSet->execute(new DeleteSetRequest(id: $set->getId()));
|
||||
|
||||
$this->assertNull($this->setRepository->find($set->getId()));
|
||||
$this->assertNull(
|
||||
$this->elementRepository->find($rootElement->getId()),
|
||||
);
|
||||
$this->assertNull(
|
||||
$this->elementRepository->find($childElement->getId()),
|
||||
);
|
||||
$this->assertNull(
|
||||
$this->elementRepository->find($grandchildElement->getId()),
|
||||
);
|
||||
$this->assertEqualsCanonicalizing([
|
||||
'set-icons/set.png',
|
||||
'element-icons/child.png',
|
||||
'element-pdfs/short/child.pdf',
|
||||
'element-pdfs/long/child.pdf',
|
||||
'element-icons/grandchild.png',
|
||||
'element-pdfs/short/grandchild.pdf',
|
||||
'element-pdfs/long/grandchild.pdf',
|
||||
], $this->filesystem->deletedPaths);
|
||||
$this->assertSame(
|
||||
count($this->filesystem->deletedPaths),
|
||||
count(array_unique($this->filesystem->deletedPaths)),
|
||||
);
|
||||
}
|
||||
|
||||
public function testRequiresSetId(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->expectExceptionMessage('setId is required');
|
||||
|
||||
$this->deleteSet->execute(new DeleteSetRequest(id: null));
|
||||
}
|
||||
|
||||
public function testThrowsWhenSetIsMissing(): void
|
||||
{
|
||||
$this->expectException(NotFoundException::class);
|
||||
$this->expectExceptionMessage('Set not found');
|
||||
|
||||
$this->deleteSet->execute(new DeleteSetRequest(id: 999));
|
||||
}
|
||||
|
||||
private function createSet(string $iconImageUrl): DomainSet
|
||||
{
|
||||
return $this->setRepository->create(new CreateSetDto(
|
||||
name: 'Set',
|
||||
description: 'Set description',
|
||||
iconImageUrl: $iconImageUrl,
|
||||
));
|
||||
}
|
||||
|
||||
private function createElement(
|
||||
DomainSet $set,
|
||||
?Element $parentElement,
|
||||
string $title,
|
||||
?string $iconImageUrl,
|
||||
?string $shortPdfPath,
|
||||
?string $longPdfPath,
|
||||
): Element {
|
||||
return $this->elementRepository->create(new CreateElementDto(
|
||||
set: $set,
|
||||
title: $title,
|
||||
description: "$title description",
|
||||
iconImageUrl: $iconImageUrl,
|
||||
richText: '',
|
||||
shortPdfPath: $shortPdfPath,
|
||||
longPdfPath: $longPdfPath,
|
||||
youtubeUrl: null,
|
||||
parentElement: $parentElement,
|
||||
));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue