Goal-Calibration/tests/Unit/ScheduledNode/UseCases/CreateScheduledNodeTest.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

167 lines
4.7 KiB
PHP

<?php
namespace Tests\Unit\ScheduledNode\UseCases;
use App\Exceptions\BadRequestException;
use App\Node\CreateNodeDto;
use App\Node\Node;
use App\Plan\CreatePlanDto;
use App\Plan\Plan;
use App\ScheduledNode\ScheduledNode;
use App\ScheduledNode\ScheduledNodeRepository;
use App\ScheduledNode\UseCases\CreateScheduledNode;
use App\ScheduledNode\UseCases\CreateScheduledNodeRequest;
use App\Text\Text;
use App\User\User;
use App\ValueObjects\EmailAddress;
use DomainException;
use Tests\Fakes\FakeNodeRepository;
use Tests\Fakes\FakePlanRepository;
use Tests\Fakes\FakeScheduledNodeRepository;
use PHPUnit\Framework\TestCase;
class CreateScheduledNodeTest extends TestCase
{
private FakeScheduledNodeRepository $scheduledNodeRepo;
private FakePlanRepository $planRepo;
private FakeNodeRepository $nodeRepo;
private CreateScheduledNode $useCase;
public function setUp(): void
{
$this->scheduledNodeRepo = new FakeScheduledNodeRepository();
$this->planRepo = new FakePlanRepository();
$this->nodeRepo = new FakeNodeRepository();
$user = new User(
id: 0,
email: new EmailAddress('test@test.com'),
passwordHash: 'hashed:password1',
isAdmin: false,
);
$this->nodeRepo->create(new CreateNodeDto(
text: new Text(0, 'text name', $user),
title: 'test node',
parentNode: null,
));
$this->planRepo->create(new CreatePlanDto(
name: 'testplan',
user: $user,
));
$this->useCase = new CreateScheduledNode(
$this->scheduledNodeRepo,
$this->planRepo,
$this->nodeRepo,
);
}
public function test_create_scheduled_node(): void
{
$scheduledNode = $this->useCase->execute(
new CreateScheduledNodeRequest(
date: '2025-01-01',
planId: 0,
nodeId: 0,
)
);
$this->assertInstanceOf(ScheduledNode::class, $scheduledNode);
$this->assertInstanceOf(
ScheduledNodeRepository::class,
$this->scheduledNodeRepo
);
}
public function test_scheduled_node_belongs_to_plan(): void
{
$scheduledNode = $this->useCase->execute(
new CreateScheduledNodeRequest(
date: '2025-01-01',
planId: 0,
nodeId: 0
)
);
$this->assertInstanceOf(Plan::class, $scheduledNode->getPlan());
}
public function test_scheduled_node_belongs_to_node(): void
{
$scheduledNode = $this->useCase->execute(
new CreateScheduledNodeRequest(
date: '2025-01-01',
planId: 0,
nodeId: 0
)
);
$this->assertInstanceOf(Node::class, $scheduledNode->getNode());
}
public function test_nonexistant_plan_throws(): void
{
$this->expectException(DomainException::class);
$this->expectExceptionMessage('Plan with id: 1 doesnt exist');
$this->useCase->execute(
new CreateScheduledNodeRequest(
date: '2025-01-01',
planId: 1,
nodeId: 0,
)
);
}
public function test_nonexistant_node_throws(): void
{
$this->expectException(DomainException::class);
$this->expectExceptionMessage('Node with id: 1 doesnt exist');
$this->useCase->execute(
new CreateScheduledNodeRequest(
date: '2025-01-01',
planId: 0,
nodeId: 1,
)
);
}
public function test_throws_if_date_is_null(): void
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('date is required');
$this->useCase->execute(
new CreateScheduledNodeRequest(
date: null,
planId: 0,
nodeId: 0
)
);
}
public function test_throws_if_plan_id_is_null(): void
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('planId is required');
$this->useCase->execute(
new CreateScheduledNodeRequest(
date: '2025-01-01',
planId: null,
nodeId: 0,
)
);
}
public function test_throws_if_node_id_is_null(): void
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('nodeId is required');
$this->useCase->execute(
new CreateScheduledNodeRequest(
date: '2025-01-01',
planId: 0,
nodeId: null,
)
);
}
}