diff --git a/tests/e2e/Controllers/ScheduledNodeControllerTest.php b/tests/e2e/Controllers/ScheduledNodeControllerTest.php new file mode 100644 index 0000000..c5f7b1c --- /dev/null +++ b/tests/e2e/Controllers/ScheduledNodeControllerTest.php @@ -0,0 +1,198 @@ +userRepo = new FakeUserRepository(); + $this->planRepo = new FakePlanRepository(); + $this->scheduledNodeRepo = new FakeScheduledNodeRepository(); + + $this->user = $this->userRepo->create(new CreateUserDto( + email: new EmailAddress('test@test.com'), + passwordHash: '', + isAdmin: false, + )); + + $this->getTodaysSchedule = new GetTodaysSchedule( + userRepo: $this->userRepo, + scheduledNodeRepo: $this->scheduledNodeRepo, + ); + $this->controller = new ScheduledNodeController(); + } + + private function makeRequest( + ?string $date, + ?User $user = null, + ): ServerRequestInterface { + $request = new ServerRequestFactory() + ->createServerRequest( + 'GET', + 'http://localhost/api/scheduled-nodes' + ); + if ($user !== null) { + $request = $request->withAttribute('user', $user); + } + if ($date !== null) { + $request = $request->withQueryParams(['date' => $date]); + } + return $request; + } + + private function seedScheduledNode( + User $user, + string $date, + string $planName = 'My reading plan', + string $nodeTitle = 'Bereishis', + ): void { + $plan = $this->planRepo->create(new CreatePlanDto( + name: $planName, + user: $user, + )); + $this->scheduledNodeRepo->create(new CreateScheduledNodeDto( + date: new DateTimeImmutable($date), + plan: $plan, + node: new Node( + id: 0, + title: $nodeTitle, + text: new Text(id: 0, name: 'test text'), + parentNode: null, + ), + )); + } + + public function test_returns_200_with_scheduled_nodes_for_user(): void + { + $this->seedScheduledNode($this->user, '2025-01-02'); + + $response = $this->controller->getScheduledNodes( + $this->makeRequest('2025-01-02', $this->user), + new Response(), + $this->getTodaysSchedule, + ); + + $this->assertEquals(200, $response->getStatusCode()); + $body = json_decode((string) $response->getBody(), true); + $this->assertIsArray($body); + $this->assertCount(1, $body); + } + + public function test_response_has_expected_fields(): void + { + $this->seedScheduledNode( + $this->user, + '2025-01-02', + 'My reading plan', + 'Bereishis', + ); + + $response = $this->controller->getScheduledNodes( + $this->makeRequest('2025-01-02', $this->user), + new Response(), + $this->getTodaysSchedule, + ); + + $body = json_decode((string) $response->getBody(), true); + $this->assertArrayHasKey('id', $body[0]); + $this->assertArrayHasKey('date', $body[0]); + $this->assertEquals('2025-01-02', $body[0]['date']); + $this->assertEquals('My reading plan', $body[0]['planName']); + $this->assertEquals('Bereishis', $body[0]['nodeTitle']); + $this->assertEquals(false, $body[0]['completed']); + } + + public function test_returns_401_when_no_user_attribute(): void + { + $response = $this->controller->getScheduledNodes( + $this->makeRequest('2025-01-02'), + new Response(), + $this->getTodaysSchedule, + ); + + $this->assertEquals(401, $response->getStatusCode()); + } + + public function test_returns_400_when_date_query_param_missing(): void + { + $response = $this->controller->getScheduledNodes( + $this->makeRequest(null, $this->user), + new Response(), + $this->getTodaysSchedule, + ); + + $this->assertEquals(400, $response->getStatusCode()); + $body = json_decode((string) $response->getBody(), true); + $this->assertArrayHasKey('error', $body); + } + + public function test_excludes_future_scheduled_nodes(): void + { + $this->seedScheduledNode($this->user, '2025-01-10'); + + $response = $this->controller->getScheduledNodes( + $this->makeRequest('2025-01-02', $this->user), + new Response(), + $this->getTodaysSchedule, + ); + + $body = json_decode((string) $response->getBody(), true); + $this->assertCount(0, $body); + } + + public function test_excludes_completed_scheduled_nodes(): void + { + $this->seedScheduledNode($this->user, '2025-01-02'); + $stored = $this->scheduledNodeRepo->find(0); + $stored->setCompleted(true); + $this->scheduledNodeRepo->update($stored); + + $response = $this->controller->getScheduledNodes( + $this->makeRequest('2025-01-02', $this->user), + new Response(), + $this->getTodaysSchedule, + ); + + $body = json_decode((string) $response->getBody(), true); + $this->assertCount(0, $body); + } + + public function test_returns_empty_array_when_user_has_no_nodes(): void + { + $response = $this->controller->getScheduledNodes( + $this->makeRequest('2025-01-02', $this->user), + new Response(), + $this->getTodaysSchedule, + ); + + $this->assertEquals(200, $response->getStatusCode()); + $body = json_decode((string) $response->getBody(), true); + $this->assertEquals([], $body); + } +}