test SetFeaturedPost use case
This commit is contained in:
parent
f73e5a1af5
commit
13ca655f9b
1 changed files with 171 additions and 0 deletions
171
backend/tests/Unit/Post/UseCases/SetFeaturedPostTest.php
Normal file
171
backend/tests/Unit/Post/UseCases/SetFeaturedPostTest.php
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Post\UseCases;
|
||||
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\ForbiddenException;
|
||||
use App\Post\CreatePostDto;
|
||||
use App\Post\Post;
|
||||
use App\Post\UseCases\SetFeaturedPost\SetFeaturedPost;
|
||||
use App\Post\UseCases\SetFeaturedPost\SetFeaturedPostRequest;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use DomainException;
|
||||
use Tests\Fakes\FakePostRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SetFeaturedPostTest extends TestCase
|
||||
{
|
||||
private FakePostRepository $postRepo;
|
||||
|
||||
private SetFeaturedPost $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 SetFeaturedPost($this->postRepo);
|
||||
}
|
||||
|
||||
private function seedPost(): Post
|
||||
{
|
||||
return $this->postRepo->create(new CreatePostDto(
|
||||
userId: 1,
|
||||
title: 'A',
|
||||
body: 'B',
|
||||
createdAt: $this->now,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_non_admin_throws_forbidden(): void
|
||||
{
|
||||
$post = $this->seedPost();
|
||||
$this->expectException(ForbiddenException::class);
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: $post->getId(),
|
||||
slot: 1,
|
||||
requesterIsAdmin: false,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_invalid_slot_throws_bad_request(): void
|
||||
{
|
||||
$post = $this->seedPost();
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: $post->getId(),
|
||||
slot: 3,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_zero_post_id_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: 0,
|
||||
slot: 1,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_unknown_post_throws_domain_exception(): void
|
||||
{
|
||||
$this->expectException(DomainException::class);
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: 999,
|
||||
slot: 1,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_admin_assigns_slot(): void
|
||||
{
|
||||
$post = $this->seedPost();
|
||||
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: $post->getId(),
|
||||
slot: 1,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
|
||||
$reloaded = $this->postRepo->find($post->getId());
|
||||
$this->assertSame(1, $reloaded->getFeatureSlot());
|
||||
}
|
||||
|
||||
public function test_assigning_same_slot_evicts_previous_post(): void
|
||||
{
|
||||
$first = $this->seedPost();
|
||||
$second = $this->seedPost();
|
||||
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: $first->getId(),
|
||||
slot: 1,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: $second->getId(),
|
||||
slot: 1,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
|
||||
$this->assertNull(
|
||||
$this->postRepo->find($first->getId())->getFeatureSlot(),
|
||||
);
|
||||
$this->assertSame(
|
||||
1,
|
||||
$this->postRepo->find($second->getId())->getFeatureSlot(),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_two_posts_can_occupy_separate_slots(): void
|
||||
{
|
||||
$first = $this->seedPost();
|
||||
$second = $this->seedPost();
|
||||
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: $first->getId(),
|
||||
slot: 1,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: $second->getId(),
|
||||
slot: 2,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
|
||||
$featured = $this->postRepo->findFeatured();
|
||||
$this->assertCount(2, $featured);
|
||||
$this->assertSame(1, $featured[0]->getFeatureSlot());
|
||||
$this->assertSame(2, $featured[1]->getFeatureSlot());
|
||||
}
|
||||
|
||||
public function test_moving_post_to_other_slot_clears_old_slot(): void
|
||||
{
|
||||
$post = $this->seedPost();
|
||||
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: $post->getId(),
|
||||
slot: 1,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
$this->useCase->execute(new SetFeaturedPostRequest(
|
||||
postId: $post->getId(),
|
||||
slot: 2,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
|
||||
$this->assertNull(
|
||||
$this->postRepo->findByFeatureSlot(1),
|
||||
);
|
||||
$this->assertSame(
|
||||
$post->getId(),
|
||||
$this->postRepo->findByFeatureSlot(2)->getId(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue