tests for bulk create nodes

This commit is contained in:
Yisroel Baum 2026-04-18 23:02:32 +03:00
parent 687283b9db
commit 3670fdd869
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,134 @@
<?php
namespace Tests\Unit\Node\UseCases;
use App\Node\CreateNodeDto;
use App\Node\Node;
use App\Node\UseCases\BulkCreateNodes;
use App\Node\UseCases\BulkCreateNodesRequest;
use App\Text\CreateTextDto;
use DomainException;
use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakeNodeRepository;
use Tests\Fakes\FakeTextRepository;
class BulkCreateNodesTest extends TestCase
{
private FakeTextRepository $textRepo;
private FakeNodeRepository $nodeRepo;
private BulkCreateNodes $useCase;
private Node $parentNode;
public function setUp(): void
{
$this->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,
));
}
}