From f9e2529994176cce3c107d5d581a89a3194c8dd2 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Wed, 6 May 2026 22:15:10 +0300 Subject: [PATCH] test ListCommentsForPost use case --- .../UseCases/ListCommentsForPostTest.php | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 backend/tests/Unit/Comment/UseCases/ListCommentsForPostTest.php diff --git a/backend/tests/Unit/Comment/UseCases/ListCommentsForPostTest.php b/backend/tests/Unit/Comment/UseCases/ListCommentsForPostTest.php new file mode 100644 index 0000000..b69a25e --- /dev/null +++ b/backend/tests/Unit/Comment/UseCases/ListCommentsForPostTest.php @@ -0,0 +1,73 @@ +now = new DateTimeImmutable( + '2026-05-06T12:00:00', + new DateTimeZone('UTC'), + ); + $this->commentRepo = new FakeCommentRepository; + $this->useCase = new ListCommentsForPost($this->commentRepo); + } + + private function seed(int $postId, string $body, string $offset): void + { + $this->commentRepo->create(new CreateCommentDto( + postId: $postId, + userId: 1, + body: $body, + createdAt: $this->now->modify($offset), + )); + } + + public function test_zero_post_id_throws_bad_request(): void + { + $this->expectException(BadRequestException::class); + $this->useCase->execute(new ListCommentsForPostRequest( + postId: 0, + )); + } + + public function test_returns_empty_list_when_no_comments(): void + { + $result = $this->useCase->execute(new ListCommentsForPostRequest( + postId: 1, + )); + + $this->assertSame([], $result); + } + + public function test_returns_only_comments_for_given_post(): void + { + $this->seed(1, 'first', '+0 seconds'); + $this->seed(2, 'other-post', '+0 seconds'); + $this->seed(1, 'second', '+1 minute'); + + $result = $this->useCase->execute(new ListCommentsForPostRequest( + postId: 1, + )); + + $this->assertCount(2, $result); + $this->assertSame('first', $result[0]->getBody()); + $this->assertSame('second', $result[1]->getBody()); + } +}