implement ListCommentsForPost use case

Renames seed() helper to seedComment() to avoid clashing with
Illuminate\Foundation\Testing\TestCase::seed().
This commit is contained in:
Yisroel Baum 2026-05-06 22:15:49 +03:00
parent f9e2529994
commit a59fc4890f
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
3 changed files with 42 additions and 4 deletions

View file

@ -0,0 +1,28 @@
<?php
namespace App\Comment\UseCases\ListCommentsForPost;
use App\Comment\Comment;
use App\Comment\CommentRepository;
use App\Exceptions\BadRequestException;
class ListCommentsForPost
{
public function __construct(
private CommentRepository $commentRepo,
) {}
/**
* @return Comment[]
*
* @throws BadRequestException
*/
public function execute(ListCommentsForPostRequest $request): array
{
if ($request->postId <= 0) {
throw new BadRequestException('postId must be positive');
}
return $this->commentRepo->findByPostId($request->postId);
}
}

View file

@ -0,0 +1,10 @@
<?php
namespace App\Comment\UseCases\ListCommentsForPost;
class ListCommentsForPostRequest
{
public function __construct(
public int $postId,
) {}
}

View file

@ -29,7 +29,7 @@ class ListCommentsForPostTest extends TestCase
$this->useCase = new ListCommentsForPost($this->commentRepo);
}
private function seed(int $postId, string $body, string $offset): void
private function seedComment(int $postId, string $body, string $offset): void
{
$this->commentRepo->create(new CreateCommentDto(
postId: $postId,
@ -58,9 +58,9 @@ class ListCommentsForPostTest extends TestCase
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');
$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,