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()); + } +}