test GetPost use case
4 cases: zero/negative id -> BadRequest; unknown id -> null (controller maps to 404); existing id returns the Post. GetPost takes int id directly (no Request object - the value is trivial and controllers pull it from a route param).
This commit is contained in:
parent
32cbf4229c
commit
eb0ebc6f63
1 changed files with 66 additions and 0 deletions
66
backend/tests/Unit/Post/UseCases/GetPostTest.php
Normal file
66
backend/tests/Unit/Post/UseCases/GetPostTest.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Post\UseCases;
|
||||
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Post\CreatePostDto;
|
||||
use App\Post\Post;
|
||||
use App\Post\UseCases\GetPost\GetPost;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use Tests\Fakes\FakePostRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class GetPostTest extends TestCase
|
||||
{
|
||||
private FakePostRepository $postRepo;
|
||||
|
||||
private DateTimeImmutable $now;
|
||||
|
||||
private GetPost $useCase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->now = new DateTimeImmutable(
|
||||
'2026-05-06T12:00:00',
|
||||
new DateTimeZone('UTC')
|
||||
);
|
||||
$this->postRepo = new FakePostRepository;
|
||||
$this->useCase = new GetPost($this->postRepo);
|
||||
}
|
||||
|
||||
public function test_zero_id_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(0);
|
||||
}
|
||||
|
||||
public function test_negative_id_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(-5);
|
||||
}
|
||||
|
||||
public function test_unknown_id_returns_null(): void
|
||||
{
|
||||
$result = $this->useCase->execute(999);
|
||||
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
public function test_existing_id_returns_post(): void
|
||||
{
|
||||
$created = $this->postRepo->create(new CreatePostDto(
|
||||
userId: 7,
|
||||
title: 'My Post',
|
||||
body: 'Some body content.',
|
||||
createdAt: $this->now,
|
||||
));
|
||||
|
||||
$result = $this->useCase->execute($created->getId());
|
||||
|
||||
$this->assertInstanceOf(Post::class, $result);
|
||||
$this->assertSame('My Post', $result->getTitle());
|
||||
$this->assertSame(7, $result->getUserId());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue