Goal-Calibration/tests/Unit/Node/UseCases/BulkCreateNodesTest.php
Yisroel Baum cbbbc80326
update downstream tests for text user requirement
Text now requires a User on construction. seed a user in
each test setUp that creates a Text directly or through the
fake repository so the suite remains green.
2026-05-02 21:27:55 +03:00

209 lines
6.1 KiB
PHP

<?php
namespace Tests\Unit\Node\UseCases;
use App\Exceptions\BadRequestException;
use App\Node\CreateNodeDto;
use App\Node\Node;
use App\Node\UseCases\BulkCreateNodes;
use App\Node\UseCases\BulkCreateNodesRequest;
use App\Text\CreateTextDto;
use App\User\UseCases\CreateUserDto;
use App\ValueObjects\EmailAddress;
use DomainException;
use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakeNodeRepository;
use Tests\Fakes\FakeTextRepository;
use Tests\Fakes\FakeUserRepository;
class BulkCreateNodesTest extends TestCase
{
private FakeTextRepository $textRepo;
private FakeNodeRepository $nodeRepo;
private BulkCreateNodes $useCase;
private Node $parentNode;
public function setUp(): void
{
$userRepo = new FakeUserRepository();
$user = $userRepo->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,
));
}
}