test seeded sets

This commit is contained in:
Yisroel Baum 2026-05-25 08:24:31 +03:00
parent 3bfcdfd0cc
commit f62b05bc37
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
3 changed files with 20 additions and 80 deletions

View file

@ -2,7 +2,6 @@
namespace Tests\Fakes; namespace Tests\Fakes;
use App\Set\CreateSetDto;
use App\Set\Set as DomainSet; use App\Set\Set as DomainSet;
use App\Set\SetRepository; use App\Set\SetRepository;
@ -13,16 +12,9 @@ class FakeSetRepository implements SetRepository
*/ */
private array $setsById = []; private array $setsById = [];
public function create(CreateSetDto $dto): DomainSet public function store(DomainSet $set): void
{ {
$id = count($this->setsById) + 1; $this->setsById[$set->getId()] = $set;
$set = new DomainSet(
id: $id,
name: $dto->name,
);
$this->setsById[$id] = $set;
return $set;
} }
public function find(int $id): ?DomainSet public function find(int $id): ?DomainSet

View file

@ -6,7 +6,7 @@ use App\Element\Element;
use App\Element\UseCases\CreateElement\CreateElement; use App\Element\UseCases\CreateElement\CreateElement;
use App\Element\UseCases\CreateElement\CreateElementRequest; use App\Element\UseCases\CreateElement\CreateElementRequest;
use App\Exceptions\BadRequestException; use App\Exceptions\BadRequestException;
use App\Set\CreateSetDto; use App\Set\Set as DomainSet;
use DomainException; use DomainException;
use Tests\Fakes\FakeElementRepository; use Tests\Fakes\FakeElementRepository;
use Tests\Fakes\FakeSetRepository; use Tests\Fakes\FakeSetRepository;
@ -30,9 +30,20 @@ class CreateElementTest extends TestCase
); );
} }
private function seedSet(int $id, string $name): DomainSet
{
$set = new DomainSet(
id: $id,
name: $name,
);
$this->setRepo->store($set);
return $set;
}
public function testCreatesRootElement(): void public function testCreatesRootElement(): void
{ {
$set = $this->setRepo->create(new CreateSetDto('Daily learning')); $set = $this->seedSet(1, 'Daily learning');
$element = $this->createElement->execute(new CreateElementRequest( $element = $this->createElement->execute(new CreateElementRequest(
setId: $set->getId(), setId: $set->getId(),
@ -48,7 +59,7 @@ class CreateElementTest extends TestCase
public function testCreatesChildElement(): void public function testCreatesChildElement(): void
{ {
$set = $this->setRepo->create(new CreateSetDto('Daily learning')); $set = $this->seedSet(1, 'Daily learning');
$rootElement = $this->createElement->execute( $rootElement = $this->createElement->execute(
new CreateElementRequest( new CreateElementRequest(
setId: $set->getId(), setId: $set->getId(),
@ -110,7 +121,7 @@ class CreateElementTest extends TestCase
public function testThrowsWhenParentElementDoesNotExist(): void public function testThrowsWhenParentElementDoesNotExist(): void
{ {
$set = $this->setRepo->create(new CreateSetDto('Daily learning')); $set = $this->seedSet(1, 'Daily learning');
$this->expectException(DomainException::class); $this->expectException(DomainException::class);
$this->expectExceptionMessage( $this->expectExceptionMessage(
@ -126,7 +137,7 @@ class CreateElementTest extends TestCase
public function testThrowsWhenRootElementAlreadyExists(): void public function testThrowsWhenRootElementAlreadyExists(): void
{ {
$set = $this->setRepo->create(new CreateSetDto('Daily learning')); $set = $this->seedSet(1, 'Daily learning');
$this->createElement->execute(new CreateElementRequest( $this->createElement->execute(new CreateElementRequest(
setId: $set->getId(), setId: $set->getId(),
title: 'Root', title: 'Root',
@ -147,8 +158,8 @@ class CreateElementTest extends TestCase
public function testThrowsWhenParentBelongsToAnotherSet(): void public function testThrowsWhenParentBelongsToAnotherSet(): void
{ {
$parentSet = $this->setRepo->create(new CreateSetDto('Parent set')); $parentSet = $this->seedSet(1, 'Parent set');
$childSet = $this->setRepo->create(new CreateSetDto('Child set')); $childSet = $this->seedSet(2, 'Child set');
$parentElement = $this->createElement->execute( $parentElement = $this->createElement->execute(
new CreateElementRequest( new CreateElementRequest(
setId: $parentSet->getId(), setId: $parentSet->getId(),

View file

@ -1,63 +0,0 @@
<?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->findBySet($set);
$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));
}
}