test ListCommentsForPost use case

This commit is contained in:
Yisroel Baum 2026-05-06 22:15:10 +03:00
parent e8d2ff3fdf
commit f9e2529994
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -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());
}
}