test ListFeaturedPosts use case
This commit is contained in:
parent
a8f59afc30
commit
e61492f864
1 changed files with 71 additions and 0 deletions
71
backend/tests/Unit/Post/UseCases/ListFeaturedPostsTest.php
Normal file
71
backend/tests/Unit/Post/UseCases/ListFeaturedPostsTest.php
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Post\UseCases;
|
||||
|
||||
use App\Post\CreatePostDto;
|
||||
use App\Post\Post;
|
||||
use App\Post\UseCases\ListFeaturedPosts\ListFeaturedPosts;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use Tests\Fakes\FakePostRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ListFeaturedPostsTest extends TestCase
|
||||
{
|
||||
private FakePostRepository $postRepo;
|
||||
|
||||
private ListFeaturedPosts $useCase;
|
||||
|
||||
private DateTimeImmutable $now;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->now = new DateTimeImmutable(
|
||||
'2026-05-06T12:00:00',
|
||||
new DateTimeZone('UTC'),
|
||||
);
|
||||
$this->postRepo = new FakePostRepository;
|
||||
$this->useCase = new ListFeaturedPosts($this->postRepo);
|
||||
}
|
||||
|
||||
private function seedFeaturedPost(string $title, ?int $slot): Post
|
||||
{
|
||||
$post = $this->postRepo->create(new CreatePostDto(
|
||||
userId: 1,
|
||||
title: $title,
|
||||
body: 'B',
|
||||
createdAt: $this->now,
|
||||
));
|
||||
if ($slot === null) {
|
||||
return $post;
|
||||
}
|
||||
|
||||
return $this->postRepo->update(new Post(
|
||||
id: $post->getId(),
|
||||
userId: $post->getUserId(),
|
||||
title: $post->getTitle(),
|
||||
body: $post->getBody(),
|
||||
createdAt: $post->getCreatedAt(),
|
||||
featureSlot: $slot,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_returns_empty_when_none_featured(): void
|
||||
{
|
||||
$this->seedFeaturedPost('not featured', null);
|
||||
$this->assertSame([], $this->useCase->execute());
|
||||
}
|
||||
|
||||
public function test_returns_featured_posts_in_slot_order(): void
|
||||
{
|
||||
$this->seedFeaturedPost('slot-2', 2);
|
||||
$this->seedFeaturedPost('slot-1', 1);
|
||||
$this->seedFeaturedPost('not-featured', null);
|
||||
|
||||
$featured = $this->useCase->execute();
|
||||
|
||||
$this->assertCount(2, $featured);
|
||||
$this->assertSame('slot-1', $featured[0]->getTitle());
|
||||
$this->assertSame('slot-2', $featured[1]->getTitle());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue