From 4897cc2e20c04320ba6e37c047584e59b5ee94d7 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Mon, 25 May 2026 08:36:16 +0300 Subject: [PATCH] test set creation repo --- backend/tests/Fakes/FakeSetRepository.php | 12 +++++- .../Element/UseCases/CreateElementTest.php | 37 ++++++++++--------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/backend/tests/Fakes/FakeSetRepository.php b/backend/tests/Fakes/FakeSetRepository.php index 87ed0fd..295e619 100644 --- a/backend/tests/Fakes/FakeSetRepository.php +++ b/backend/tests/Fakes/FakeSetRepository.php @@ -2,6 +2,7 @@ namespace Tests\Fakes; +use App\Set\CreateSetDto; use App\Set\Set as DomainSet; use App\Set\SetRepository; @@ -12,9 +13,16 @@ class FakeSetRepository implements SetRepository */ 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 diff --git a/backend/tests/Unit/Element/UseCases/CreateElementTest.php b/backend/tests/Unit/Element/UseCases/CreateElementTest.php index 26f13a8..2d01fc6 100644 --- a/backend/tests/Unit/Element/UseCases/CreateElementTest.php +++ b/backend/tests/Unit/Element/UseCases/CreateElementTest.php @@ -6,7 +6,7 @@ use App\Element\Element; use App\Element\UseCases\CreateElement\CreateElement; use App\Element\UseCases\CreateElement\CreateElementRequest; use App\Exceptions\BadRequestException; -use App\Set\Set as DomainSet; +use App\Set\CreateSetDto; use DomainException; use Tests\Fakes\FakeElementRepository; 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 { - $set = $this->seedSet(1, 'Daily learning'); + $set = $this->setRepo->create( + new CreateSetDto('Daily learning') + ); $element = $this->createElement->execute(new CreateElementRequest( setId: $set->getId(), @@ -59,7 +50,9 @@ class CreateElementTest extends TestCase public function testCreatesChildElement(): void { - $set = $this->seedSet(1, 'Daily learning'); + $set = $this->setRepo->create( + new CreateSetDto('Daily learning') + ); $rootElement = $this->createElement->execute( new CreateElementRequest( setId: $set->getId(), @@ -121,7 +114,9 @@ class CreateElementTest extends TestCase public function testThrowsWhenParentElementDoesNotExist(): void { - $set = $this->seedSet(1, 'Daily learning'); + $set = $this->setRepo->create( + new CreateSetDto('Daily learning') + ); $this->expectException(DomainException::class); $this->expectExceptionMessage( @@ -137,7 +132,9 @@ class CreateElementTest extends TestCase public function testThrowsWhenRootElementAlreadyExists(): void { - $set = $this->seedSet(1, 'Daily learning'); + $set = $this->setRepo->create( + new CreateSetDto('Daily learning') + ); $this->createElement->execute(new CreateElementRequest( setId: $set->getId(), title: 'Root', @@ -158,8 +155,12 @@ class CreateElementTest extends TestCase public function testThrowsWhenParentBelongsToAnotherSet(): void { - $parentSet = $this->seedSet(1, 'Parent set'); - $childSet = $this->seedSet(2, 'Child set'); + $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(),