From 143a4ffe3945f538715cc5c241dc3d65c09d17cf Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Sat, 21 Feb 2026 22:23:30 +0200 Subject: [PATCH] test nonexistant parent node id throws exception --- app/Node/UseCases/CreateNode.php | 12 ++++++++---- tests/Unit/Node/UseCases/CreateNodeTest.php | 12 ++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/Node/UseCases/CreateNode.php b/app/Node/UseCases/CreateNode.php index ad2a7e6..25882f0 100644 --- a/app/Node/UseCases/CreateNode.php +++ b/app/Node/UseCases/CreateNode.php @@ -17,10 +17,10 @@ class CreateNode public function execute(CreateNodeRequest $request): Node { - $id = $request->textId; - $text = $this->textRepo->find($id); + $textId = $request->textId; + $text = $this->textRepo->find($textId); if ($text === null) { - throw new DomainException("Text with id: $id doesnt exist"); + throw new DomainException("Text with id: $textId doesnt exist"); } if ($request->parentNodeId === null) { return $this->nodeRepo->create(new CreateNodeDto( @@ -29,7 +29,11 @@ class CreateNode parentNode: null, )); } - $parentNode = $this->nodeRepo->find($request->parentNodeId); + $parentNodeId = $request->parentNodeId; + $parentNode = $this->nodeRepo->find($parentNodeId); + if ($parentNode === null) { + throw new DomainException("Node with id: $parentNodeId doesnt exist"); + } return $this->nodeRepo->create(new CreateNodeDto( text: $text, diff --git a/tests/Unit/Node/UseCases/CreateNodeTest.php b/tests/Unit/Node/UseCases/CreateNodeTest.php index ef6f793..bcbe369 100644 --- a/tests/Unit/Node/UseCases/CreateNodeTest.php +++ b/tests/Unit/Node/UseCases/CreateNodeTest.php @@ -59,6 +59,7 @@ class CreateNodeTest extends TestCase public function test_throws_if_text_doesnt_exist(): void { $this->expectException(DomainException::class); + $this->expectExceptionMessage("Text with id: 1 doesnt exist"); $node = $this->useCase->execute(new CreateNodeRequest( textId: 1, title: 'test', @@ -83,4 +84,15 @@ class CreateNodeTest extends TestCase $node2->getParentNode()->getId() ); } + + public function test_nonexistant_parent_id_throws(): void + { + $this->expectException(DomainException::class); + $this->expectExceptionMessage("Node with id: 0 doesnt exist"); + $node = $this->useCase->execute(new CreateNodeRequest( + textId: 0, + title: 'test', + parentNodeId: 0, + )); + } }