From e97ca282377e612e5fa97c54c33445276335364c Mon Sep 17 00:00:00 2001 From: yisroel Date: Wed, 6 May 2026 15:24:41 +0300 Subject: [PATCH] test ListUserPosts use case 5 cases: zero/negative userId -> BadRequest; user with no posts -> []; returns only requested user's posts (filters out other authors); ordered newest-first by createdAt. fails red. --- .../Unit/Post/UseCases/ListUserPostsTest.php | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 backend/tests/Unit/Post/UseCases/ListUserPostsTest.php diff --git a/backend/tests/Unit/Post/UseCases/ListUserPostsTest.php b/backend/tests/Unit/Post/UseCases/ListUserPostsTest.php new file mode 100644 index 0000000..6d24b30 --- /dev/null +++ b/backend/tests/Unit/Post/UseCases/ListUserPostsTest.php @@ -0,0 +1,95 @@ +now = new DateTimeImmutable( + '2026-05-06T12:00:00', + new DateTimeZone('UTC') + ); + $this->postRepo = new FakePostRepository; + $this->useCase = new ListUserPosts($this->postRepo); + } + + private function seedPost(int $userId, string $title, string $offset): void + { + $this->postRepo->create(new CreatePostDto( + userId: $userId, + title: $title, + body: 'body', + createdAt: $this->now->modify($offset), + )); + } + + public function test_zero_user_id_throws_bad_request(): void + { + $this->expectException(BadRequestException::class); + $this->useCase->execute(new ListUserPostsRequest(userId: 0)); + } + + public function test_negative_user_id_throws_bad_request(): void + { + $this->expectException(BadRequestException::class); + $this->useCase->execute(new ListUserPostsRequest(userId: -1)); + } + + public function test_user_with_no_posts_returns_empty_array(): void + { + $this->seedPost(2, 'other-user-post', '-1 hour'); + + $posts = $this->useCase->execute( + new ListUserPostsRequest(userId: 1) + ); + + $this->assertSame([], $posts); + } + + public function test_returns_only_requested_users_posts(): void + { + $this->seedPost(1, 'mine-1', '-2 days'); + $this->seedPost(2, 'theirs', '-1 day'); + $this->seedPost(1, 'mine-2', '-1 hour'); + + $posts = $this->useCase->execute( + new ListUserPostsRequest(userId: 1) + ); + + $this->assertCount(2, $posts); + foreach ($posts as $post) { + $this->assertSame(1, $post->getUserId()); + } + } + + public function test_returns_posts_ordered_newest_first(): void + { + $this->seedPost(1, 'first', '-3 days'); + $this->seedPost(1, 'second', '-2 days'); + $this->seedPost(1, 'third', '-1 hour'); + + $posts = $this->useCase->execute( + new ListUserPostsRequest(userId: 1) + ); + + $this->assertSame('third', $posts[0]->getTitle()); + $this->assertSame('second', $posts[1]->getTitle()); + $this->assertSame('first', $posts[2]->getTitle()); + } +}