test set elements
This commit is contained in:
parent
104909bcf5
commit
db35a97910
6 changed files with 409 additions and 0 deletions
171
backend/tests/Unit/Element/UseCases/CreateElementTest.php
Normal file
171
backend/tests/Unit/Element/UseCases/CreateElementTest.php
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Element\UseCases;
|
||||
|
||||
use App\Element\Element;
|
||||
use App\Element\UseCases\CreateElement\CreateElement;
|
||||
use App\Element\UseCases\CreateElement\CreateElementRequest;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Set\CreateSetDto;
|
||||
use DomainException;
|
||||
use Tests\Fakes\FakeElementRepository;
|
||||
use Tests\Fakes\FakeSetRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CreateElementTest extends TestCase
|
||||
{
|
||||
private FakeSetRepository $setRepo;
|
||||
|
||||
private FakeElementRepository $elementRepo;
|
||||
|
||||
private CreateElement $createElement;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->setRepo = new FakeSetRepository();
|
||||
$this->elementRepo = new FakeElementRepository();
|
||||
$this->createElement = new CreateElement(
|
||||
$this->elementRepo,
|
||||
$this->setRepo,
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreatesRootElement(): void
|
||||
{
|
||||
$set = $this->setRepo->create(new CreateSetDto('Daily learning'));
|
||||
|
||||
$element = $this->createElement->execute(new CreateElementRequest(
|
||||
setId: $set->getId(),
|
||||
title: 'Root',
|
||||
parentElementId: null,
|
||||
));
|
||||
|
||||
$this->assertInstanceOf(Element::class, $element);
|
||||
$this->assertSame('Root', $element->getTitle());
|
||||
$this->assertSame($set->getId(), $element->getSet()->getId());
|
||||
$this->assertNull($element->getParentElement());
|
||||
}
|
||||
|
||||
public function testCreatesChildElement(): void
|
||||
{
|
||||
$set = $this->setRepo->create(new CreateSetDto('Daily learning'));
|
||||
$rootElement = $this->createElement->execute(
|
||||
new CreateElementRequest(
|
||||
setId: $set->getId(),
|
||||
title: 'Root',
|
||||
parentElementId: null,
|
||||
)
|
||||
);
|
||||
|
||||
$childElement = $this->createElement->execute(
|
||||
new CreateElementRequest(
|
||||
setId: $set->getId(),
|
||||
title: 'Child',
|
||||
parentElementId: $rootElement->getId(),
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertSame('Child', $childElement->getTitle());
|
||||
$this->assertSame(
|
||||
$rootElement->getId(),
|
||||
$childElement->getParentElement()->getId(),
|
||||
);
|
||||
}
|
||||
|
||||
public function testThrowsWhenSetIdMissing(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->expectExceptionMessage('setId is required');
|
||||
|
||||
$this->createElement->execute(new CreateElementRequest(
|
||||
setId: null,
|
||||
title: 'Root',
|
||||
parentElementId: null,
|
||||
));
|
||||
}
|
||||
|
||||
public function testThrowsWhenTitleMissing(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->expectExceptionMessage('title is required');
|
||||
|
||||
$this->createElement->execute(new CreateElementRequest(
|
||||
setId: 1,
|
||||
title: null,
|
||||
parentElementId: null,
|
||||
));
|
||||
}
|
||||
|
||||
public function testThrowsWhenSetDoesNotExist(): void
|
||||
{
|
||||
$this->expectException(DomainException::class);
|
||||
$this->expectExceptionMessage('Set with id: 99 doesnt exist');
|
||||
|
||||
$this->createElement->execute(new CreateElementRequest(
|
||||
setId: 99,
|
||||
title: 'Root',
|
||||
parentElementId: null,
|
||||
));
|
||||
}
|
||||
|
||||
public function testThrowsWhenParentElementDoesNotExist(): void
|
||||
{
|
||||
$set = $this->setRepo->create(new CreateSetDto('Daily learning'));
|
||||
|
||||
$this->expectException(DomainException::class);
|
||||
$this->expectExceptionMessage(
|
||||
'Element with id: 99 doesnt exist'
|
||||
);
|
||||
|
||||
$this->createElement->execute(new CreateElementRequest(
|
||||
setId: $set->getId(),
|
||||
title: 'Child',
|
||||
parentElementId: 99,
|
||||
));
|
||||
}
|
||||
|
||||
public function testThrowsWhenRootElementAlreadyExists(): void
|
||||
{
|
||||
$set = $this->setRepo->create(new CreateSetDto('Daily learning'));
|
||||
$this->createElement->execute(new CreateElementRequest(
|
||||
setId: $set->getId(),
|
||||
title: 'Root',
|
||||
parentElementId: null,
|
||||
));
|
||||
|
||||
$this->expectException(DomainException::class);
|
||||
$this->expectExceptionMessage(
|
||||
'A root element already exists for this set'
|
||||
);
|
||||
|
||||
$this->createElement->execute(new CreateElementRequest(
|
||||
setId: $set->getId(),
|
||||
title: 'Another root',
|
||||
parentElementId: null,
|
||||
));
|
||||
}
|
||||
|
||||
public function testThrowsWhenParentBelongsToAnotherSet(): void
|
||||
{
|
||||
$parentSet = $this->setRepo->create(new CreateSetDto('Parent set'));
|
||||
$childSet = $this->setRepo->create(new CreateSetDto('Child set'));
|
||||
$parentElement = $this->createElement->execute(
|
||||
new CreateElementRequest(
|
||||
setId: $parentSet->getId(),
|
||||
title: 'Parent root',
|
||||
parentElementId: null,
|
||||
)
|
||||
);
|
||||
|
||||
$this->expectException(DomainException::class);
|
||||
$this->expectExceptionMessage(
|
||||
'Parent element must belong to the same set'
|
||||
);
|
||||
|
||||
$this->createElement->execute(new CreateElementRequest(
|
||||
setId: $childSet->getId(),
|
||||
title: 'Invalid child',
|
||||
parentElementId: $parentElement->getId(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue