Goal-Calibration/tests/Unit/ScheduledNode/UseCases/GetTodaysScheduleTest.php
Yisroel Baum db93871194
move inline use statements to file headers
two type hints introduced earlier on this branch referenced
classes by their fully-qualified names inline. hoist them to
the top-of-file use block per backend-context.md PHP rules.
2026-05-02 22:05:58 +03:00

225 lines
6.9 KiB
PHP

<?php
namespace Tests\Unit\ScheduledNode\UseCases;
use App\Exceptions\BadRequestException;
use App\Node\Node;
use App\Plan\CreatePlanDto;
use App\ScheduledNode\CreateScheduledNodeDto;
use App\ScheduledNode\ScheduledNode;
use App\ScheduledNode\UseCases\GetTodaysSchedule;
use App\ScheduledNode\UseCases\GetTodaysScheduleRequest;
use App\Text\Text;
use App\User\UseCases\CreateUserDto;
use App\User\User;
use App\ValueObjects\EmailAddress;
use DateTimeImmutable;
use DomainException;
use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakePlanRepository;
use Tests\Fakes\FakeScheduledNodeRepository;
use Tests\Fakes\FakeUserRepository;
class GetTodaysScheduleTest extends TestCase
{
private FakeUserRepository $userRepo;
private FakePlanRepository $planRepo;
private FakeScheduledNodeRepository $scheduledNodeRepo;
private GetTodaysSchedule $useCase;
private User $user;
protected function setUp(): void
{
$this->userRepo = new FakeUserRepository();
$this->scheduledNodeRepo = new FakeScheduledNodeRepository();
$this->planRepo = new FakePlanRepository();
$this->user = $this->userRepo->create(new CreateUserDto(
email: new EmailAddress('email@email.com'),
passwordHash: 'hash',
isAdmin: false,
));
$plan = $this->planRepo->create(new CreatePlanDto(
name: 'test plan',
user: $this->user,
));
$this->scheduledNodeRepo->create(new CreateScheduledNodeDto(
date: new DateTimeImmutable('2025-01-02'),
plan: $plan,
node: new Node(
id: 0,
title: 'test node',
text: new Text(id: 0, name: 'test text', user: $this->user),
parentNode: null,
),
));
$this->useCase = new GetTodaysSchedule(
userRepo: $this->userRepo,
scheduledNodeRepo: $this->scheduledNodeRepo,
);
}
public function test_returns_array_of_scheduled_nodes(): void
{
$result = $this->useCase->execute(new GetTodaysScheduleRequest(
date: '2025-01-02',
userId: 0,
));
$this->assertIsArray($result);
$this->assertInstanceOf(ScheduledNode::class, $result[0]);
}
public function test_returns_all_unfinished_scheduled_nodes_up_until_today(): void
{
$this->scheduledNodeRepo->create(new CreateScheduledNodeDto(
date: new DateTimeImmutable('2025-01-01'),
plan: $this->planRepo->find(0),
node: new Node(
id: 0,
title: 'test node',
text: new Text(id: 0, name: 'test text', user: $this->user),
parentNode: null,
),
));
$result = $this->useCase->execute(new GetTodaysScheduleRequest(
date: '2025-01-02',
userId: 0,
));
$this->assertEquals(2, count($result));
}
public function test_only_returns_uncompleted_nodes(): void
{
$node = $this->scheduledNodeRepo->create(
new CreateScheduledNodeDto(
date: new DateTimeImmutable('2025-01-01'),
plan: $this->planRepo->find(0),
node: new Node(
id: 0,
title: 'test node',
text: new Text(id: 0, name: 'test text', user: $this->user),
parentNode: null,
),
)
);
$node->setCompleted(true);
$this->scheduledNodeRepo->update($node);
$result = $this->useCase->execute(new GetTodaysScheduleRequest(
date: '2025-01-02',
userId: 0,
));
$this->assertEquals(1, count($result));
}
public function test_throws_if_date_is_null(): void
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('date is required');
$this->useCase->execute(new GetTodaysScheduleRequest(
date: null,
userId: 0,
));
}
public function test_throws_if_user_id_is_null(): void
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('userId is required');
$this->useCase->execute(new GetTodaysScheduleRequest(
date: '2025-01-02',
userId: null,
));
}
public function test_nonexistant_user_throws(): void
{
$this->expectException(DomainException::class);
$this->expectExceptionMessage('User with id: 99 doesnt exist');
$this->useCase->execute(new GetTodaysScheduleRequest(
date: '2025-01-02',
userId: 99,
));
}
public function test_returns_empty_array_when_user_has_no_scheduled_nodes(): void
{
$otherUser = $this->userRepo->create(new CreateUserDto(
email: new EmailAddress('other@email.com'),
passwordHash: 'hash',
isAdmin: false,
));
$result = $this->useCase->execute(new GetTodaysScheduleRequest(
date: '2025-01-02',
userId: $otherUser->getId(),
));
$this->assertIsArray($result);
$this->assertEquals(0, count($result));
}
public function test_excludes_scheduled_nodes_dated_after_today(): void
{
$this->scheduledNodeRepo->create(new CreateScheduledNodeDto(
date: new DateTimeImmutable('2025-01-05'),
plan: $this->planRepo->find(0),
node: new Node(
id: 0,
title: 'future node',
text: new Text(id: 0, name: 'test text', user: $this->user),
parentNode: null,
),
));
$result = $this->useCase->execute(new GetTodaysScheduleRequest(
date: '2025-01-02',
userId: 0,
));
$this->assertEquals(1, count($result));
}
public function test_does_not_return_other_users_scheduled_nodes(): void
{
$otherUser = $this->userRepo->create(new CreateUserDto(
email: new EmailAddress('other@email.com'),
passwordHash: 'hash',
isAdmin: false,
));
$otherPlan = $this->planRepo->create(new CreatePlanDto(
name: 'other plan',
user: $otherUser,
));
$this->scheduledNodeRepo->create(new CreateScheduledNodeDto(
date: new DateTimeImmutable('2025-01-02'),
plan: $otherPlan,
node: new Node(
id: 0,
title: 'other node',
text: new Text(id: 0, name: 'test text', user: $otherUser),
parentNode: null,
),
));
$result = $this->useCase->execute(new GetTodaysScheduleRequest(
date: '2025-01-02',
userId: 0,
));
$this->assertEquals(1, count($result));
$resultNode = array_values($result)[0];
$this->assertEquals(
0,
$resultNode->getPlan()->getUser()->getId()
);
}
}