test ListCommentsForPost use case
This commit is contained in:
parent
e8d2ff3fdf
commit
f9e2529994
1 changed files with 73 additions and 0 deletions
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Comment\UseCases;
|
||||
|
||||
use App\Comment\CreateCommentDto;
|
||||
use App\Comment\UseCases\ListCommentsForPost\ListCommentsForPost;
|
||||
use App\Comment\UseCases\ListCommentsForPost\ListCommentsForPostRequest;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use Tests\Fakes\FakeCommentRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ListCommentsForPostTest extends TestCase
|
||||
{
|
||||
private FakeCommentRepository $commentRepo;
|
||||
|
||||
private ListCommentsForPost $useCase;
|
||||
|
||||
private DateTimeImmutable $now;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->now = new DateTimeImmutable(
|
||||
'2026-05-06T12:00:00',
|
||||
new DateTimeZone('UTC'),
|
||||
);
|
||||
$this->commentRepo = new FakeCommentRepository;
|
||||
$this->useCase = new ListCommentsForPost($this->commentRepo);
|
||||
}
|
||||
|
||||
private function seed(int $postId, string $body, string $offset): void
|
||||
{
|
||||
$this->commentRepo->create(new CreateCommentDto(
|
||||
postId: $postId,
|
||||
userId: 1,
|
||||
body: $body,
|
||||
createdAt: $this->now->modify($offset),
|
||||
));
|
||||
}
|
||||
|
||||
public function test_zero_post_id_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new ListCommentsForPostRequest(
|
||||
postId: 0,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_returns_empty_list_when_no_comments(): void
|
||||
{
|
||||
$result = $this->useCase->execute(new ListCommentsForPostRequest(
|
||||
postId: 1,
|
||||
));
|
||||
|
||||
$this->assertSame([], $result);
|
||||
}
|
||||
|
||||
public function test_returns_only_comments_for_given_post(): void
|
||||
{
|
||||
$this->seed(1, 'first', '+0 seconds');
|
||||
$this->seed(2, 'other-post', '+0 seconds');
|
||||
$this->seed(1, 'second', '+1 minute');
|
||||
|
||||
$result = $this->useCase->execute(new ListCommentsForPostRequest(
|
||||
postId: 1,
|
||||
));
|
||||
|
||||
$this->assertCount(2, $result);
|
||||
$this->assertSame('first', $result[0]->getBody());
|
||||
$this->assertSame('second', $result[1]->getBody());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue