diff --git a/app/Plan/UseCases/CreatePlan.php b/app/Plan/UseCases/CreatePlan.php index b12fc50..8a3cc91 100644 --- a/app/Plan/UseCases/CreatePlan.php +++ b/app/Plan/UseCases/CreatePlan.php @@ -64,6 +64,7 @@ class CreatePlan new CreateScheduledNodeRequest( date: $scheduledDate->format('Y-m-d'), planId: $plan->getId(), + nodeId: $node->getId(), ) ); } diff --git a/app/ScheduledNode/JsonScheduledNodeRepository.php b/app/ScheduledNode/JsonScheduledNodeRepository.php index bee31ea..866584f 100644 --- a/app/ScheduledNode/JsonScheduledNodeRepository.php +++ b/app/ScheduledNode/JsonScheduledNodeRepository.php @@ -20,6 +20,7 @@ class JsonScheduledNodeRepository implements ScheduledNodeRepository 'id' => $id, 'date' => $dto->date->format('Y-m-d'), 'planId' => $dto->plan->getId(), + 'nodeId' => $dto->node->getId(), ]; $this->writeScheduledNodes($scheduledNodes); @@ -27,6 +28,7 @@ class JsonScheduledNodeRepository implements ScheduledNodeRepository id: $id, date: $dto->date, plan: $dto->plan, + node: $dto->node, ); } diff --git a/app/ScheduledNode/UseCases/CreateScheduledNode.php b/app/ScheduledNode/UseCases/CreateScheduledNode.php index 752b658..444f4d0 100644 --- a/app/ScheduledNode/UseCases/CreateScheduledNode.php +++ b/app/ScheduledNode/UseCases/CreateScheduledNode.php @@ -3,6 +3,7 @@ namespace App\ScheduledNode\UseCases; use App\Exceptions\BadRequestException; +use App\Node\NodeRepository; use App\Plan\PlanRepository; use App\ScheduledNode\ScheduledNode; use App\ScheduledNode\CreateScheduledNodeDto; @@ -15,6 +16,7 @@ class CreateScheduledNode public function __construct( private ScheduledNodeRepository $scheduledNodeRepo, private PlanRepository $planRepo, + private NodeRepository $nodeRepo, ) {} /** @@ -24,24 +26,40 @@ class CreateScheduledNode public function execute( CreateScheduledNodeRequest $request ): ScheduledNode { - if ($request->date === null) { + $nodeId = $request->nodeId; + $planId = $request->planId; + $date = $request->date; + if ($date === null) { throw new BadRequestException('date is required'); } - if ($request->planId === null) { + if ($planId === null) { throw new BadRequestException('planId is required'); } - $id = $request->planId; - $plan = $this->planRepo->find($id); + if ($nodeId === null) { + throw new BadRequestException('nodeId is required'); + } + + $plan = $this->planRepo->find($planId); if ($plan === null) { - throw new DomainException("Plan with id: $id doesnt exist"); + throw new DomainException( + "Plan with id: $planId doesnt exist" + ); + } + + $node = $this->nodeRepo->find($nodeId); + if ($node === null) { + throw new DomainException( + "Node with id: $nodeId doesnt exist" + ); } return $this->scheduledNodeRepo->create( new CreateScheduledNodeDto( - date: new DateTimeImmutable($request->date), + date: new DateTimeImmutable($date), plan: $plan, + node: $node, ) ); } diff --git a/app/ScheduledNode/UseCases/CreateScheduledNodeRequest.php b/app/ScheduledNode/UseCases/CreateScheduledNodeRequest.php index 5931cb3..4c0a2fd 100644 --- a/app/ScheduledNode/UseCases/CreateScheduledNodeRequest.php +++ b/app/ScheduledNode/UseCases/CreateScheduledNodeRequest.php @@ -7,5 +7,6 @@ class CreateScheduledNodeRequest public function __construct( public ?string $date, public ?int $planId, + public ?int $nodeId, ) {} } diff --git a/tests/Fakes/FakeScheduledNodeRepository.php b/tests/Fakes/FakeScheduledNodeRepository.php index 4241813..8bf5419 100644 --- a/tests/Fakes/FakeScheduledNodeRepository.php +++ b/tests/Fakes/FakeScheduledNodeRepository.php @@ -20,6 +20,7 @@ class FakeScheduledNodeRepository implements ScheduledNodeRepository id: $id, date: $dto->date, plan: $dto->plan, + node: $dto->node, ); $this->existingScheduledNodes[$id] = $scheduledNode; diff --git a/tests/Unit/Plan/UseCases/CreatePlanTest.php b/tests/Unit/Plan/UseCases/CreatePlanTest.php index 94412b7..fbdfc60 100644 --- a/tests/Unit/Plan/UseCases/CreatePlanTest.php +++ b/tests/Unit/Plan/UseCases/CreatePlanTest.php @@ -45,6 +45,7 @@ class CreatePlanTest extends TestCase $this->createScheduledNode = new CreateScheduledNode( scheduledNodeRepo: $this->scheduledNodeRepo, planRepo: $this->planRepo, + nodeRepo: $this->nodeRepo, ); $this->textRepo->create(new CreateTextDto('testname')); $this->useCase = new CreatePlan( diff --git a/tests/Unit/ScheduledNode/UseCases/CreateScheduledNodeTest.php b/tests/Unit/ScheduledNode/UseCases/CreateScheduledNodeTest.php index 477a07a..8c866fe 100644 --- a/tests/Unit/ScheduledNode/UseCases/CreateScheduledNodeTest.php +++ b/tests/Unit/ScheduledNode/UseCases/CreateScheduledNodeTest.php @@ -3,15 +3,19 @@ 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; @@ -22,12 +26,20 @@ class CreateScheduledNodeTest extends TestCase 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(); + $this->nodeRepo->create(new CreateNodeDto( + text: new Text(0, 'text name'), + title: 'test node', + parentNode: null, + )); $this->planRepo->create(new CreatePlanDto( name: 'testplan', user: new User( @@ -40,6 +52,7 @@ class CreateScheduledNodeTest extends TestCase $this->useCase = new CreateScheduledNode( $this->scheduledNodeRepo, $this->planRepo, + $this->nodeRepo, ); } @@ -49,6 +62,7 @@ class CreateScheduledNodeTest extends TestCase new CreateScheduledNodeRequest( date: '2025-01-01', planId: 0, + nodeId: 0, ) ); $this->assertInstanceOf(ScheduledNode::class, $scheduledNode); @@ -64,11 +78,24 @@ class CreateScheduledNodeTest extends TestCase 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); @@ -77,6 +104,20 @@ class CreateScheduledNodeTest extends TestCase 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, ) ); } @@ -90,6 +131,7 @@ class CreateScheduledNodeTest extends TestCase new CreateScheduledNodeRequest( date: null, planId: 0, + nodeId: 0 ) ); } @@ -103,6 +145,21 @@ class CreateScheduledNodeTest extends TestCase 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, ) ); } diff --git a/tests/e2e/Controllers/PlanControllerTest.php b/tests/e2e/Controllers/PlanControllerTest.php index eb34ecd..818651c 100644 --- a/tests/e2e/Controllers/PlanControllerTest.php +++ b/tests/e2e/Controllers/PlanControllerTest.php @@ -55,6 +55,7 @@ class PlanControllerTest extends TestCase $createScheduledNode = new CreateScheduledNode( scheduledNodeRepo: $this->scheduledNodeRepo, planRepo: $this->planRepo, + nodeRepo: $this->nodeRepo, ); $this->createPlan = new CreatePlan( $this->planRepo,