test set creation repo

This commit is contained in:
Yisroel Baum 2026-05-25 08:36:16 +03:00
parent f58cf050e3
commit 4897cc2e20
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 29 additions and 20 deletions

View file

@ -2,6 +2,7 @@
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;
@ -12,9 +13,16 @@ class FakeSetRepository implements SetRepository
*/ */
private array $setsById = []; private array $setsById = [];
public function store(DomainSet $set): void public function create(CreateSetDto $dto): DomainSet
{ {
$this->setsById[$set->getId()] = $set; $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 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\Set as DomainSet; use App\Set\CreateSetDto;
use DomainException; use DomainException;
use Tests\Fakes\FakeElementRepository; use Tests\Fakes\FakeElementRepository;
use Tests\Fakes\FakeSetRepository; use Tests\Fakes\FakeSetRepository;
@ -30,20 +30,11 @@ 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->seedSet(1, 'Daily learning'); $set = $this->setRepo->create(
new CreateSetDto('Daily learning')
);
$element = $this->createElement->execute(new CreateElementRequest( $element = $this->createElement->execute(new CreateElementRequest(
setId: $set->getId(), setId: $set->getId(),
@ -59,7 +50,9 @@ class CreateElementTest extends TestCase
public function testCreatesChildElement(): void public function testCreatesChildElement(): void
{ {
$set = $this->seedSet(1, 'Daily learning'); $set = $this->setRepo->create(
new CreateSetDto('Daily learning')
);
$rootElement = $this->createElement->execute( $rootElement = $this->createElement->execute(
new CreateElementRequest( new CreateElementRequest(
setId: $set->getId(), setId: $set->getId(),
@ -121,7 +114,9 @@ class CreateElementTest extends TestCase
public function testThrowsWhenParentElementDoesNotExist(): void public function testThrowsWhenParentElementDoesNotExist(): void
{ {
$set = $this->seedSet(1, 'Daily learning'); $set = $this->setRepo->create(
new CreateSetDto('Daily learning')
);
$this->expectException(DomainException::class); $this->expectException(DomainException::class);
$this->expectExceptionMessage( $this->expectExceptionMessage(
@ -137,7 +132,9 @@ class CreateElementTest extends TestCase
public function testThrowsWhenRootElementAlreadyExists(): void public function testThrowsWhenRootElementAlreadyExists(): void
{ {
$set = $this->seedSet(1, 'Daily learning'); $set = $this->setRepo->create(
new CreateSetDto('Daily learning')
);
$this->createElement->execute(new CreateElementRequest( $this->createElement->execute(new CreateElementRequest(
setId: $set->getId(), setId: $set->getId(),
title: 'Root', title: 'Root',
@ -158,8 +155,12 @@ class CreateElementTest extends TestCase
public function testThrowsWhenParentBelongsToAnotherSet(): void public function testThrowsWhenParentBelongsToAnotherSet(): void
{ {
$parentSet = $this->seedSet(1, 'Parent set'); $parentSet = $this->setRepo->create(
$childSet = $this->seedSet(2, 'Child set'); new CreateSetDto('Parent set')
);
$childSet = $this->setRepo->create(
new CreateSetDto('Child set')
);
$parentElement = $this->createElement->execute( $parentElement = $this->createElement->execute(
new CreateElementRequest( new CreateElementRequest(
setId: $parentSet->getId(), setId: $parentSet->getId(),