add node to scheduled node
This commit is contained in:
parent
d47a0235d2
commit
a9265abeae
8 changed files with 88 additions and 6 deletions
|
|
@ -64,6 +64,7 @@ class CreatePlan
|
|||
new CreateScheduledNodeRequest(
|
||||
date: $scheduledDate->format('Y-m-d'),
|
||||
planId: $plan->getId(),
|
||||
nodeId: $node->getId(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,5 +7,6 @@ class CreateScheduledNodeRequest
|
|||
public function __construct(
|
||||
public ?string $date,
|
||||
public ?int $planId,
|
||||
public ?int $nodeId,
|
||||
) {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ class FakeScheduledNodeRepository implements ScheduledNodeRepository
|
|||
id: $id,
|
||||
date: $dto->date,
|
||||
plan: $dto->plan,
|
||||
node: $dto->node,
|
||||
);
|
||||
$this->existingScheduledNodes[$id] = $scheduledNode;
|
||||
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue