diff --git a/tests/Unit/Node/UseCases/BulkCreateNodesTest.php b/tests/Unit/Node/UseCases/BulkCreateNodesTest.php new file mode 100644 index 0000000..13b16d0 --- /dev/null +++ b/tests/Unit/Node/UseCases/BulkCreateNodesTest.php @@ -0,0 +1,134 @@ +textRepo = new FakeTextRepository; + $this->textRepo->create(new CreateTextDto(name: 'text')); + + $this->nodeRepo = new FakeNodeRepository; + $text = $this->textRepo->find(0); + $this->parentNode = $this->nodeRepo->create(new CreateNodeDto( + text: $text, + title: 'Root', + parentNode: null, + )); + + $this->useCase = new BulkCreateNodes( + nodeRepo: $this->nodeRepo, + textRepo: $this->textRepo, + ); + } + + public function test_creates_correct_number_of_nodes(): void + { + $nodes = $this->useCase->execute(new BulkCreateNodesRequest( + textId: 0, + parentNodeId: $this->parentNode->getId(), + titlePrefix: 'Page', + count: 5, + )); + + $this->assertCount(5, $nodes); + } + + public function test_nodes_have_correct_titles(): void + { + $nodes = $this->useCase->execute(new BulkCreateNodesRequest( + textId: 0, + parentNodeId: $this->parentNode->getId(), + titlePrefix: 'Page', + count: 3, + )); + + $this->assertEquals('Page 1', $nodes[0]->getTitle()); + $this->assertEquals('Page 2', $nodes[1]->getTitle()); + $this->assertEquals('Page 3', $nodes[2]->getTitle()); + } + + public function test_nodes_have_correct_parent(): void + { + $nodes = $this->useCase->execute(new BulkCreateNodesRequest( + textId: 0, + parentNodeId: $this->parentNode->getId(), + titlePrefix: 'Page', + count: 3, + )); + + foreach ($nodes as $node) { + $this->assertEquals($this->parentNode->getId(), $node->getParentNode()->getId()); + } + } + + public function test_nodes_belong_to_text(): void + { + $nodes = $this->useCase->execute(new BulkCreateNodesRequest( + textId: 0, + parentNodeId: $this->parentNode->getId(), + titlePrefix: 'Page', + count: 3, + )); + + foreach ($nodes as $node) { + $this->assertEquals(0, $node->getText()->getId()); + } + } + + public function test_returns_array_of_node_instances(): void + { + $nodes = $this->useCase->execute(new BulkCreateNodesRequest( + textId: 0, + parentNodeId: $this->parentNode->getId(), + titlePrefix: 'Chapter', + count: 2, + )); + + foreach ($nodes as $node) { + $this->assertInstanceOf(Node::class, $node); + } + } + + public function test_throws_if_text_doesnt_exist(): void + { + $this->expectException(DomainException::class); + $this->expectExceptionMessage("Text with id: 99 doesnt exist"); + + $this->useCase->execute(new BulkCreateNodesRequest( + textId: 99, + parentNodeId: $this->parentNode->getId(), + titlePrefix: 'Page', + count: 5, + )); + } + + public function test_throws_if_parent_node_doesnt_exist(): void + { + $this->expectException(DomainException::class); + $this->expectExceptionMessage("Node with id: 99 doesnt exist"); + + $this->useCase->execute(new BulkCreateNodesRequest( + textId: 0, + parentNodeId: 99, + titlePrefix: 'Page', + count: 5, + )); + } +}