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.
This commit is contained in:
parent
7ec46aa8f9
commit
e97ca28237
1 changed files with 95 additions and 0 deletions
95
backend/tests/Unit/Post/UseCases/ListUserPostsTest.php
Normal file
95
backend/tests/Unit/Post/UseCases/ListUserPostsTest.php
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Post\UseCases;
|
||||
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Post\CreatePostDto;
|
||||
use App\Post\UseCases\ListUserPosts\ListUserPosts;
|
||||
use App\Post\UseCases\ListUserPosts\ListUserPostsRequest;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use Tests\Fakes\FakePostRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ListUserPostsTest extends TestCase
|
||||
{
|
||||
private FakePostRepository $postRepo;
|
||||
|
||||
private DateTimeImmutable $now;
|
||||
|
||||
private ListUserPosts $useCase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->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());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue