test get todays schedule and use case with request
This commit is contained in:
parent
0ea300f4d2
commit
2047cd72e7
3 changed files with 117 additions and 0 deletions
35
app/ScheduledNode/UseCases/GetTodaysSchedule.php
Normal file
35
app/ScheduledNode/UseCases/GetTodaysSchedule.php
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\ScheduledNode\UseCases;
|
||||||
|
|
||||||
|
use App\ScheduledNode\ScheduledNode;
|
||||||
|
use App\ScheduledNode\ScheduledNodeRepository;
|
||||||
|
use App\User\UserRepository;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
|
||||||
|
class GetTodaysSchedule
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private UserRepository $userRepo,
|
||||||
|
private ScheduledNodeRepository $scheduledNodeRepo,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return ScheduledNode[]
|
||||||
|
*/
|
||||||
|
public function execute(GetTodaysScheduleRequest $request): array
|
||||||
|
{
|
||||||
|
$date = new DateTimeImmutable($request->date);
|
||||||
|
$userId = $request->userId;
|
||||||
|
$user = $this->userRepo->find($userId);
|
||||||
|
$scheduledNodes = $this->scheduledNodeRepo->findByUser($user);
|
||||||
|
|
||||||
|
return array_filter(
|
||||||
|
$scheduledNodes,
|
||||||
|
function (ScheduledNode $node) use ($date) {
|
||||||
|
return $node->getDate()->format('Y-m-d')
|
||||||
|
=== $date->format('Y-m-d');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
app/ScheduledNode/UseCases/GetTodaysScheduleRequest.php
Normal file
11
app/ScheduledNode/UseCases/GetTodaysScheduleRequest.php
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\ScheduledNode\UseCases;
|
||||||
|
|
||||||
|
class GetTodaysScheduleRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public ?string $date,
|
||||||
|
public ?int $userId,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
71
tests/Unit/ScheduledNode/UseCases/GetTodaysScheduleTest.php
Normal file
71
tests/Unit/ScheduledNode/UseCases/GetTodaysScheduleTest.php
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\ScheduledNode\UseCases;
|
||||||
|
|
||||||
|
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\ValueObjects\EmailAddress;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
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;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->userRepo = new FakeUserRepository;
|
||||||
|
$this->scheduledNodeRepo = new FakeScheduledNodeRepository;
|
||||||
|
$this->planRepo = new FakePlanRepository;
|
||||||
|
$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: $user,
|
||||||
|
));
|
||||||
|
$this->scheduledNodeRepo->create(new CreateScheduledNodeDto(
|
||||||
|
date: new DateTimeImmutable('2025-01-01'),
|
||||||
|
plan: $plan,
|
||||||
|
node: new Node(
|
||||||
|
id: 0,
|
||||||
|
title: 'test node',
|
||||||
|
text: new Text(id: 0, name: 'test text'),
|
||||||
|
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-01',
|
||||||
|
userId: 0,
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertIsArray($result);
|
||||||
|
$this->assertInstanceOf(ScheduledNode::class, $result[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue