create(new CreateUserDto( email: new EmailAddress('a@b.com'), passwordHash: '', isAdmin: false, )); $this->textRepo = new FakeTextRepository(); $this->textRepo->create(new CreateTextDto(name: 'text', user: $user)); $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, )); } public function test_throws_if_text_id_is_null(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('textId is required'); $this->useCase->execute(new BulkCreateNodesRequest( textId: null, parentNodeId: $this->parentNode->getId(), titlePrefix: 'Page', count: 5, )); } public function test_throws_if_parent_node_id_is_null(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('parentNodeId is required'); $this->useCase->execute(new BulkCreateNodesRequest( textId: 0, parentNodeId: null, titlePrefix: 'Page', count: 5, )); } public function test_throws_if_title_prefix_is_null(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('titlePrefix is required'); $this->useCase->execute(new BulkCreateNodesRequest( textId: 0, parentNodeId: $this->parentNode->getId(), titlePrefix: null, count: 5, )); } public function test_throws_if_count_is_null(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('count is required'); $this->useCase->execute(new BulkCreateNodesRequest( textId: 0, parentNodeId: $this->parentNode->getId(), titlePrefix: 'Page', count: null, )); } public function test_throws_if_count_is_less_than_one(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('count must be at least 1'); $this->useCase->execute(new BulkCreateNodesRequest( textId: 0, parentNodeId: $this->parentNode->getId(), titlePrefix: 'Page', count: 0, )); } }