diff --git a/app/Plan/UseCases/CreatePlan.php b/app/Plan/UseCases/CreatePlan.php index 64c7bda..9841b33 100644 --- a/app/Plan/UseCases/CreatePlan.php +++ b/app/Plan/UseCases/CreatePlan.php @@ -82,8 +82,9 @@ class CreatePlan $currentDate = $currentDate->modify('+1 day'); } + $nodesPerDay = (int) ceil(count($nodesOfText) / count($dates)); foreach ($nodesOfText as $index => $node) { - $dateIndex = $index % count($dates); + $dateIndex = (int) floor($index / $nodesPerDay); $scheduledDate = $dates[$dateIndex]; $this->createScheduledNode->execute( diff --git a/tests/Unit/Plan/UseCases/CreatePlanTest.php b/tests/Unit/Plan/UseCases/CreatePlanTest.php index d6f686d..667e72c 100644 --- a/tests/Unit/Plan/UseCases/CreatePlanTest.php +++ b/tests/Unit/Plan/UseCases/CreatePlanTest.php @@ -270,4 +270,54 @@ class CreatePlanTest extends TestCase $childTwo->getDate() ); } + + public function test_more_scheduled_nodes_than_days(): void + { + $text = $this->textRepo->find(0); + $rootNode = $this->nodeRepo->create(new CreateNodeDto( + text: $text, + title: 'root node', + parentNode: null, + )); + $this->nodeRepo->create(new CreateNodeDto( + text: $text, + title: 'child 1', + parentNode: $rootNode, + )); + $this->nodeRepo->create(new CreateNodeDto( + text: $text, + title: 'child 2', + parentNode: $rootNode, + )); + $this->nodeRepo->create(new CreateNodeDto( + text: $text, + title: 'child 3', + parentNode: $rootNode, + )); + $plan = $this->useCase->execute(new CreatePlanRequest( + userId: 0, + name: 'testPlan', + textId: 0, + dateStart: '2025-01-01', + dateEnd: '2025-01-02', + )); + $childOne = $this->scheduledNodeRepo->find(0); + $childTwo = $this->scheduledNodeRepo->find(1); + $childThree = $this->scheduledNodeRepo->find(2); + $this->assertNotNull($childOne); + $this->assertNotNull($childTwo); + $this->assertNotNull($childThree); + $this->assertEquals( + new DateTimeImmutable('2025-01-01'), + $childOne->getDate() + ); + $this->assertEquals( + new DateTimeImmutable('2025-01-01'), + $childTwo->getDate() + ); + $this->assertEquals( + new DateTimeImmutable('2025-01-02'), + $childThree->getDate() + ); + } }