test CreateComment use case
This commit is contained in:
parent
93da08756c
commit
2557c9b6a9
1 changed files with 138 additions and 0 deletions
138
backend/tests/Unit/Comment/UseCases/CreateCommentTest.php
Normal file
138
backend/tests/Unit/Comment/UseCases/CreateCommentTest.php
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Comment\UseCases;
|
||||
|
||||
use App\Comment\UseCases\CreateComment\CreateComment;
|
||||
use App\Comment\UseCases\CreateComment\CreateCommentRequest;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Post\CreatePostDto;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use DomainException;
|
||||
use Tests\Fakes\FakeClock;
|
||||
use Tests\Fakes\FakeCommentRepository;
|
||||
use Tests\Fakes\FakePostRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CreateCommentTest extends TestCase
|
||||
{
|
||||
private FakeCommentRepository $commentRepo;
|
||||
|
||||
private FakePostRepository $postRepo;
|
||||
|
||||
private FakeClock $clock;
|
||||
|
||||
private DateTimeImmutable $now;
|
||||
|
||||
private CreateComment $useCase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->now = new DateTimeImmutable(
|
||||
'2026-05-06T12:00:00',
|
||||
new DateTimeZone('UTC'),
|
||||
);
|
||||
$this->commentRepo = new FakeCommentRepository;
|
||||
$this->postRepo = new FakePostRepository;
|
||||
$this->clock = new FakeClock($this->now);
|
||||
$this->useCase = new CreateComment(
|
||||
$this->commentRepo,
|
||||
$this->postRepo,
|
||||
$this->clock,
|
||||
);
|
||||
}
|
||||
|
||||
private function seedPost(): int
|
||||
{
|
||||
$post = $this->postRepo->create(new CreatePostDto(
|
||||
userId: 1,
|
||||
title: 'Some Post',
|
||||
body: 'Body.',
|
||||
createdAt: $this->now,
|
||||
));
|
||||
|
||||
return $post->getId();
|
||||
}
|
||||
|
||||
public function test_zero_post_id_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new CreateCommentRequest(
|
||||
postId: 0,
|
||||
userId: 1,
|
||||
body: 'hi',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_zero_user_id_throws_bad_request(): void
|
||||
{
|
||||
$postId = $this->seedPost();
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new CreateCommentRequest(
|
||||
postId: $postId,
|
||||
userId: 0,
|
||||
body: 'hi',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_null_body_throws_bad_request(): void
|
||||
{
|
||||
$postId = $this->seedPost();
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new CreateCommentRequest(
|
||||
postId: $postId,
|
||||
userId: 1,
|
||||
body: null,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_blank_body_throws_bad_request(): void
|
||||
{
|
||||
$postId = $this->seedPost();
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new CreateCommentRequest(
|
||||
postId: $postId,
|
||||
userId: 1,
|
||||
body: ' ',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_unknown_post_throws_domain_exception(): void
|
||||
{
|
||||
$this->expectException(DomainException::class);
|
||||
$this->useCase->execute(new CreateCommentRequest(
|
||||
postId: 999,
|
||||
userId: 1,
|
||||
body: 'hi',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_valid_create_returns_comment(): void
|
||||
{
|
||||
$postId = $this->seedPost();
|
||||
$created = $this->useCase->execute(new CreateCommentRequest(
|
||||
postId: $postId,
|
||||
userId: 5,
|
||||
body: ' Hello world ',
|
||||
));
|
||||
|
||||
$this->assertSame($postId, $created->getPostId());
|
||||
$this->assertSame(5, $created->getUserId());
|
||||
$this->assertSame('Hello world', $created->getBody());
|
||||
$this->assertEquals($this->now, $created->getCreatedAt());
|
||||
}
|
||||
|
||||
public function test_created_comment_is_findable(): void
|
||||
{
|
||||
$postId = $this->seedPost();
|
||||
$created = $this->useCase->execute(new CreateCommentRequest(
|
||||
postId: $postId,
|
||||
userId: 5,
|
||||
body: 'Hello',
|
||||
));
|
||||
|
||||
$found = $this->commentRepo->find($created->getId());
|
||||
$this->assertNotNull($found);
|
||||
$this->assertSame('Hello', $found->getBody());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue