test set elements

This commit is contained in:
Yisroel Baum 2026-05-24 22:28:44 +03:00
parent 104909bcf5
commit db35a97910
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
6 changed files with 409 additions and 0 deletions

View file

@ -0,0 +1,68 @@
<?php
namespace Tests\Fakes;
use App\Element\CreateElementDto;
use App\Element\Element;
use App\Element\ElementRepository;
class FakeElementRepository implements ElementRepository
{
/**
* @var Element[]
*/
private array $elementsById = [];
public function create(CreateElementDto $dto): Element
{
$id = count($this->elementsById) + 1;
$element = new Element(
id: $id,
title: $dto->title,
set: $dto->set,
parentElement: $dto->parentElement,
);
$this->elementsById[$id] = $element;
return $element;
}
public function find(int $id): ?Element
{
if (! isset($this->elementsById[$id])) {
return null;
}
return $this->cloneElement($this->elementsById[$id]);
}
/**
* @return Element[]
*/
public function findBySetId(int $id): array
{
$elements = [];
foreach ($this->elementsById as $element) {
if ($element->getSet()->getId() === $id) {
$elements[] = $this->cloneElement($element);
}
}
return $elements;
}
private function cloneElement(Element $element): Element
{
$parentElement = $element->getParentElement();
if ($parentElement !== null) {
$parentElement = $this->cloneElement($parentElement);
}
return new Element(
id: $element->getId(),
title: $element->getTitle(),
set: $element->getSet(),
parentElement: $parentElement,
);
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace Tests\Fakes;
use App\Set\CreateSetDto;
use App\Set\Set as DomainSet;
use App\Set\SetRepository;
class FakeSetRepository implements SetRepository
{
/**
* @var DomainSet[]
*/
private array $setsById = [];
public function create(CreateSetDto $dto): DomainSet
{
$id = count($this->setsById) + 1;
$set = new DomainSet(
id: $id,
name: $dto->name,
);
$this->setsById[$id] = $set;
return $set;
}
public function find(int $id): ?DomainSet
{
if (! isset($this->setsById[$id])) {
return null;
}
return $this->cloneSet($this->setsById[$id]);
}
/**
* @return DomainSet[]
*/
public function getAll(): array
{
$sets = [];
foreach ($this->setsById as $set) {
$sets[] = $this->cloneSet($set);
}
return $sets;
}
private function cloneSet(DomainSet $set): DomainSet
{
return new DomainSet(
id: $set->getId(),
name: $set->getName(),
);
}
}

View file

@ -0,0 +1,33 @@
<?php
namespace Tests\Unit\Element;
use App\Element\Element;
use App\Set\Set as DomainSet;
use Tests\TestCase;
class ElementTest extends TestCase
{
public function testCreatesElementWithNullableParent(): void
{
$set = new DomainSet(1, 'Daily learning');
$rootElement = new Element(
id: 1,
title: 'Root',
set: $set,
parentElement: null,
);
$childElement = new Element(
id: 2,
title: 'Child',
set: $set,
parentElement: $rootElement,
);
$this->assertSame(2, $childElement->getId());
$this->assertSame('Child', $childElement->getTitle());
$this->assertSame($set, $childElement->getSet());
$this->assertSame($rootElement, $childElement->getParentElement());
$this->assertNull($rootElement->getParentElement());
}
}

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

View file

@ -0,0 +1,17 @@
<?php
namespace Tests\Unit\Set;
use App\Set\Set as DomainSet;
use Tests\TestCase;
class SetTest extends TestCase
{
public function testCreatesSetWithName(): void
{
$set = new DomainSet(1, 'Daily learning');
$this->assertSame(1, $set->getId());
$this->assertSame('Daily learning', $set->getName());
}
}

View file

@ -0,0 +1,63 @@
<?php
namespace Tests\Unit\Set\UseCases;
use App\Exceptions\BadRequestException;
use App\Set\Set as DomainSet;
use App\Set\UseCases\CreateSet\CreateSet;
use App\Set\UseCases\CreateSet\CreateSetRequest;
use Tests\Fakes\FakeElementRepository;
use Tests\Fakes\FakeSetRepository;
use Tests\TestCase;
class CreateSetTest extends TestCase
{
private FakeSetRepository $setRepo;
private FakeElementRepository $elementRepo;
private CreateSet $createSet;
protected function setUp(): void
{
$this->setRepo = new FakeSetRepository();
$this->elementRepo = new FakeElementRepository();
$this->createSet = new CreateSet(
$this->setRepo,
$this->elementRepo,
);
}
public function testCreatesSet(): void
{
$set = $this->createSet->execute(
new CreateSetRequest('Daily learning')
);
$this->assertInstanceOf(DomainSet::class, $set);
$this->assertSame(1, $set->getId());
$this->assertSame('Daily learning', $set->getName());
}
public function testCreatesRootElementForSet(): void
{
$set = $this->createSet->execute(
new CreateSetRequest('Daily learning')
);
$elements = $this->elementRepo->findBySetId($set->getId());
$this->assertCount(1, $elements);
$this->assertSame('Daily learning', $elements[0]->getTitle());
$this->assertSame($set->getId(), $elements[0]->getSet()->getId());
$this->assertNull($elements[0]->getParentElement());
}
public function testThrowsWhenNameMissing(): void
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('name is required');
$this->createSet->execute(new CreateSetRequest(null));
}
}