implement ListCommentsForPost use case
Renames seed() helper to seedComment() to avoid clashing with Illuminate\Foundation\Testing\TestCase::seed().
This commit is contained in:
parent
f9e2529994
commit
a59fc4890f
3 changed files with 42 additions and 4 deletions
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Comment\UseCases\ListCommentsForPost;
|
||||||
|
|
||||||
|
class ListCommentsForPostRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $postId,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
|
|
@ -29,7 +29,7 @@ class ListCommentsForPostTest extends TestCase
|
||||||
$this->useCase = new ListCommentsForPost($this->commentRepo);
|
$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(
|
$this->commentRepo->create(new CreateCommentDto(
|
||||||
postId: $postId,
|
postId: $postId,
|
||||||
|
|
@ -58,9 +58,9 @@ class ListCommentsForPostTest extends TestCase
|
||||||
|
|
||||||
public function test_returns_only_comments_for_given_post(): void
|
public function test_returns_only_comments_for_given_post(): void
|
||||||
{
|
{
|
||||||
$this->seed(1, 'first', '+0 seconds');
|
$this->seedComment(1, 'first', '+0 seconds');
|
||||||
$this->seed(2, 'other-post', '+0 seconds');
|
$this->seedComment(2, 'other-post', '+0 seconds');
|
||||||
$this->seed(1, 'second', '+1 minute');
|
$this->seedComment(1, 'second', '+1 minute');
|
||||||
|
|
||||||
$result = $this->useCase->execute(new ListCommentsForPostRequest(
|
$result = $this->useCase->execute(new ListCommentsForPostRequest(
|
||||||
postId: 1,
|
postId: 1,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue