TIDE/backend/tests/Unit/Comment/UseCases/ListCommentsForPostTest.php
Yisroel Baum a59fc4890f
implement ListCommentsForPost use case
Renames seed() helper to seedComment() to avoid clashing with
Illuminate\Foundation\Testing\TestCase::seed().
2026-05-06 22:15:49 +03:00

73 lines
2.1 KiB
PHP

<?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 seedComment(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->seedComment(1, 'first', '+0 seconds');
$this->seedComment(2, 'other-post', '+0 seconds');
$this->seedComment(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());
}
}