Rabbi_Gerzi/backend/tests/Unit/Set/UseCases/DeleteSetTest.php
2026-07-02 17:11:50 +03:00

140 lines
4.4 KiB
PHP

<?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,
));
}
}