diff --git a/app/Plan/UseCases/CreatePlan.php b/app/Plan/UseCases/CreatePlan.php index a061466..0d0124c 100644 --- a/app/Plan/UseCases/CreatePlan.php +++ b/app/Plan/UseCases/CreatePlan.php @@ -49,6 +49,9 @@ class CreatePlan } $textId = $request->textId; $text = $this->textRepo->find($textId); + if ($text === null) { + throw new DomainException("Text with id: $textId doesnt exist"); + } $nodesOfText = $this->filterForNonParentNodes( $this->nodeRepo->findByTextId($textId) ); diff --git a/tests/Unit/Plan/UseCases/CreatePlanTest.php b/tests/Unit/Plan/UseCases/CreatePlanTest.php index 26af21d..cccc4ea 100644 --- a/tests/Unit/Plan/UseCases/CreatePlanTest.php +++ b/tests/Unit/Plan/UseCases/CreatePlanTest.php @@ -43,6 +43,7 @@ class CreatePlanTest extends TestCase scheduledNodeRepo: $this->scheduledNodeRepo, planRepo: $this->planRepo, ); + $this->textRepo->create(new CreateTextDto('testname')); $this->useCase = new CreatePlan( $this->planRepo, $this->userRepo, @@ -83,11 +84,20 @@ class CreatePlanTest extends TestCase )); } + public function test_nonexistant_text_id_throws(): void + { + $this->expectException(DomainException::class); + $this->expectExceptionMessage("Text with id: 1 doesnt exist"); + $plan = $this->useCase->execute(new CreatePlanRequest( + userId: 0, + name: 'testPlan', + textId: 1, + )); + } + public function test_plan_schedules_nodes_on_creation(): void { - $text = $this->textRepo->create(new CreateTextDto( - name: 'testname', - )); + $text = $this->textRepo->find(0); $this->nodeRepo->create(new CreateNodeDto( text: $text, title: 'testtitle', @@ -103,9 +113,7 @@ class CreatePlanTest extends TestCase public function test_plan_only_schedules_nodes_which_arent_parents(): void { - $text = $this->textRepo->create(new CreateTextDto( - name: 'testname', - )); + $text = $this->textRepo->find(0); $rootNode = $this->nodeRepo->create(new CreateNodeDto( text: $text, title: 'root node',