merge comment-domain
This commit is contained in:
commit
fa63005db7
16 changed files with 787 additions and 0 deletions
41
backend/app/Comment/Comment.php
Normal file
41
backend/app/Comment/Comment.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace App\Comment;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class Comment
|
||||
{
|
||||
public function __construct(
|
||||
private int $id,
|
||||
private int $postId,
|
||||
private int $userId,
|
||||
private string $body,
|
||||
private DateTimeImmutable $createdAt,
|
||||
) {}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getPostId(): int
|
||||
{
|
||||
return $this->postId;
|
||||
}
|
||||
|
||||
public function getUserId(): int
|
||||
{
|
||||
return $this->userId;
|
||||
}
|
||||
|
||||
public function getBody(): string
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
}
|
||||
43
backend/app/Comment/CommentModel.php
Normal file
43
backend/app/Comment/CommentModel.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\Comment;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $post_id
|
||||
* @property int $user_id
|
||||
* @property string $body
|
||||
* @property DateTimeImmutable $created_at
|
||||
*
|
||||
* @method static Builder<static>|CommentModel newModelQuery()
|
||||
* @method static Builder<static>|CommentModel newQuery()
|
||||
* @method static Builder<static>|CommentModel query()
|
||||
* @method static Builder<static>|CommentModel whereId($value)
|
||||
* @method static Builder<static>|CommentModel wherePostId($value)
|
||||
* @method static Builder<static>|CommentModel whereUserId($value)
|
||||
* @method static Builder<static>|CommentModel whereBody($value)
|
||||
* @method static Builder<static>|CommentModel whereCreatedAt($value)
|
||||
*
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class CommentModel extends Model
|
||||
{
|
||||
protected $table = 'comments';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'post_id',
|
||||
'user_id',
|
||||
'body',
|
||||
'created_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'created_at' => 'immutable_datetime',
|
||||
];
|
||||
}
|
||||
17
backend/app/Comment/CommentRepository.php
Normal file
17
backend/app/Comment/CommentRepository.php
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace App\Comment;
|
||||
|
||||
interface CommentRepository
|
||||
{
|
||||
public function create(CreateCommentDto $dto): Comment;
|
||||
|
||||
public function find(int $id): ?Comment;
|
||||
|
||||
/**
|
||||
* @return Comment[]
|
||||
*/
|
||||
public function findByPostId(int $postId): array;
|
||||
|
||||
public function delete(int $id): void;
|
||||
}
|
||||
15
backend/app/Comment/CreateCommentDto.php
Normal file
15
backend/app/Comment/CreateCommentDto.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace App\Comment;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
readonly class CreateCommentDto
|
||||
{
|
||||
public function __construct(
|
||||
public int $postId,
|
||||
public int $userId,
|
||||
public string $body,
|
||||
public DateTimeImmutable $createdAt,
|
||||
) {}
|
||||
}
|
||||
66
backend/app/Comment/EloquentCommentRepository.php
Normal file
66
backend/app/Comment/EloquentCommentRepository.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace App\Comment;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
|
||||
class EloquentCommentRepository implements CommentRepository
|
||||
{
|
||||
public function create(CreateCommentDto $dto): Comment
|
||||
{
|
||||
$model = CommentModel::create([
|
||||
'post_id' => $dto->postId,
|
||||
'user_id' => $dto->userId,
|
||||
'body' => $dto->body,
|
||||
'created_at' => $dto->createdAt,
|
||||
]);
|
||||
|
||||
return $this->toDomain($model);
|
||||
}
|
||||
|
||||
public function find(int $id): ?Comment
|
||||
{
|
||||
$model = CommentModel::find($id);
|
||||
|
||||
return $model === null ? null : $this->toDomain($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Comment[]
|
||||
*/
|
||||
public function findByPostId(int $postId): array
|
||||
{
|
||||
$models = CommentModel::query()
|
||||
->where('post_id', $postId)
|
||||
->orderBy('created_at', 'asc')
|
||||
->get();
|
||||
|
||||
return $models->map(
|
||||
function (CommentModel $model) {
|
||||
return $this->toDomain($model);
|
||||
},
|
||||
)->all();
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
CommentModel::query()->where('id', $id)->delete();
|
||||
}
|
||||
|
||||
private function toDomain(CommentModel $model): Comment
|
||||
{
|
||||
$utc = new DateTimeZone('UTC');
|
||||
|
||||
return new Comment(
|
||||
id: $model->id,
|
||||
postId: $model->post_id,
|
||||
userId: $model->user_id,
|
||||
body: $model->body,
|
||||
createdAt: new DateTimeImmutable(
|
||||
$model->created_at->toDateTimeString(),
|
||||
$utc,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
49
backend/app/Comment/UseCases/CreateComment/CreateComment.php
Normal file
49
backend/app/Comment/UseCases/CreateComment/CreateComment.php
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace App\Comment\UseCases\CreateComment;
|
||||
|
||||
use App\Auth\Clock;
|
||||
use App\Comment\Comment;
|
||||
use App\Comment\CommentRepository;
|
||||
use App\Comment\CreateCommentDto;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Post\PostRepository;
|
||||
use DomainException;
|
||||
|
||||
class CreateComment
|
||||
{
|
||||
public function __construct(
|
||||
private CommentRepository $commentRepo,
|
||||
private PostRepository $postRepo,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws DomainException
|
||||
*/
|
||||
public function execute(CreateCommentRequest $request): Comment
|
||||
{
|
||||
if ($request->postId <= 0) {
|
||||
throw new BadRequestException('postId must be positive');
|
||||
}
|
||||
if ($request->userId <= 0) {
|
||||
throw new BadRequestException('userId must be positive');
|
||||
}
|
||||
$body = $request->body === null ? '' : trim($request->body);
|
||||
if ($body === '') {
|
||||
throw new BadRequestException('body is required');
|
||||
}
|
||||
|
||||
if ($this->postRepo->find($request->postId) === null) {
|
||||
throw new DomainException('post not found');
|
||||
}
|
||||
|
||||
return $this->commentRepo->create(new CreateCommentDto(
|
||||
postId: $request->postId,
|
||||
userId: $request->userId,
|
||||
body: $body,
|
||||
createdAt: $this->clock->now(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Comment\UseCases\CreateComment;
|
||||
|
||||
class CreateCommentRequest
|
||||
{
|
||||
public function __construct(
|
||||
public int $postId,
|
||||
public int $userId,
|
||||
public ?string $body,
|
||||
) {}
|
||||
}
|
||||
42
backend/app/Comment/UseCases/DeleteComment/DeleteComment.php
Normal file
42
backend/app/Comment/UseCases/DeleteComment/DeleteComment.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace App\Comment\UseCases\DeleteComment;
|
||||
|
||||
use App\Comment\CommentRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\ForbiddenException;
|
||||
|
||||
class DeleteComment
|
||||
{
|
||||
public function __construct(
|
||||
private CommentRepository $commentRepo,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws ForbiddenException
|
||||
*/
|
||||
public function execute(DeleteCommentRequest $request): void
|
||||
{
|
||||
if ($request->commentId <= 0) {
|
||||
throw new BadRequestException('commentId must be positive');
|
||||
}
|
||||
if ($request->requesterId <= 0) {
|
||||
throw new BadRequestException('requesterId must be positive');
|
||||
}
|
||||
|
||||
$comment = $this->commentRepo->find($request->commentId);
|
||||
if ($comment === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$isAuthor = $comment->getUserId() === $request->requesterId;
|
||||
if (! $isAuthor && ! $request->requesterIsAdmin) {
|
||||
throw new ForbiddenException(
|
||||
'requester is not allowed to delete this comment'
|
||||
);
|
||||
}
|
||||
|
||||
$this->commentRepo->delete($request->commentId);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Comment\UseCases\DeleteComment;
|
||||
|
||||
class DeleteCommentRequest
|
||||
{
|
||||
public function __construct(
|
||||
public int $commentId,
|
||||
public int $requesterId,
|
||||
public bool $requesterIsAdmin,
|
||||
) {}
|
||||
}
|
||||
|
|
@ -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,
|
||||
) {}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('comments', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('post_id')
|
||||
->constrained('posts')
|
||||
->cascadeOnDelete();
|
||||
$table->foreignId('user_id')
|
||||
->constrained('users')
|
||||
->cascadeOnDelete();
|
||||
$table->text('body');
|
||||
$table->dateTime('created_at')->index();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('comments');
|
||||
}
|
||||
};
|
||||
82
backend/tests/Fakes/FakeCommentRepository.php
Normal file
82
backend/tests/Fakes/FakeCommentRepository.php
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Comment\Comment;
|
||||
use App\Comment\CommentRepository;
|
||||
use App\Comment\CreateCommentDto;
|
||||
|
||||
class FakeCommentRepository implements CommentRepository
|
||||
{
|
||||
/**
|
||||
* @var Comment[]
|
||||
*/
|
||||
private array $existingComments = [];
|
||||
|
||||
public function create(CreateCommentDto $dto): Comment
|
||||
{
|
||||
$id = $this->getNextId();
|
||||
$comment = new Comment(
|
||||
id: $id,
|
||||
postId: $dto->postId,
|
||||
userId: $dto->userId,
|
||||
body: $dto->body,
|
||||
createdAt: $dto->createdAt,
|
||||
);
|
||||
$this->existingComments[$id] = $comment;
|
||||
|
||||
return $this->copy($comment);
|
||||
}
|
||||
|
||||
public function find(int $id): ?Comment
|
||||
{
|
||||
$comment = $this->existingComments[$id] ?? null;
|
||||
if ($comment === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->copy($comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Comment[]
|
||||
*/
|
||||
public function findByPostId(int $postId): array
|
||||
{
|
||||
$matching = [];
|
||||
foreach ($this->existingComments as $comment) {
|
||||
if ($comment->getPostId() === $postId) {
|
||||
$matching[] = $this->copy($comment);
|
||||
}
|
||||
}
|
||||
usort(
|
||||
$matching,
|
||||
function (Comment $left, Comment $right) {
|
||||
return $left->getCreatedAt() <=> $right->getCreatedAt();
|
||||
},
|
||||
);
|
||||
|
||||
return $matching;
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
unset($this->existingComments[$id]);
|
||||
}
|
||||
|
||||
private function copy(Comment $comment): Comment
|
||||
{
|
||||
return new Comment(
|
||||
id: $comment->getId(),
|
||||
postId: $comment->getPostId(),
|
||||
userId: $comment->getUserId(),
|
||||
body: $comment->getBody(),
|
||||
createdAt: $comment->getCreatedAt(),
|
||||
);
|
||||
}
|
||||
|
||||
private function getNextId(): int
|
||||
{
|
||||
return count($this->existingComments) + 1;
|
||||
}
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
131
backend/tests/Unit/Comment/UseCases/DeleteCommentTest.php
Normal file
131
backend/tests/Unit/Comment/UseCases/DeleteCommentTest.php
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Comment\UseCases;
|
||||
|
||||
use App\Comment\Comment;
|
||||
use App\Comment\CreateCommentDto;
|
||||
use App\Comment\UseCases\DeleteComment\DeleteComment;
|
||||
use App\Comment\UseCases\DeleteComment\DeleteCommentRequest;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\ForbiddenException;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use Tests\Fakes\FakeCommentRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DeleteCommentTest extends TestCase
|
||||
{
|
||||
private FakeCommentRepository $commentRepo;
|
||||
|
||||
private DateTimeImmutable $now;
|
||||
|
||||
private DeleteComment $useCase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->now = new DateTimeImmutable(
|
||||
'2026-05-06T12:00:00',
|
||||
new DateTimeZone('UTC'),
|
||||
);
|
||||
$this->commentRepo = new FakeCommentRepository;
|
||||
$this->useCase = new DeleteComment($this->commentRepo);
|
||||
}
|
||||
|
||||
private function seedCommentByUser(int $userId): Comment
|
||||
{
|
||||
return $this->commentRepo->create(new CreateCommentDto(
|
||||
postId: 1,
|
||||
userId: $userId,
|
||||
body: 'comment body',
|
||||
createdAt: $this->now,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_zero_comment_id_throws_bad_request(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new DeleteCommentRequest(
|
||||
commentId: 0,
|
||||
requesterId: 1,
|
||||
requesterIsAdmin: false,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_zero_requester_id_throws_bad_request(): void
|
||||
{
|
||||
$comment = $this->seedCommentByUser(1);
|
||||
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->useCase->execute(new DeleteCommentRequest(
|
||||
commentId: $comment->getId(),
|
||||
requesterId: 0,
|
||||
requesterIsAdmin: false,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_unknown_comment_is_no_op(): void
|
||||
{
|
||||
$this->useCase->execute(new DeleteCommentRequest(
|
||||
commentId: 999,
|
||||
requesterId: 1,
|
||||
requesterIsAdmin: false,
|
||||
));
|
||||
|
||||
$this->assertNull($this->commentRepo->find(999));
|
||||
}
|
||||
|
||||
public function test_author_can_delete_own_comment(): void
|
||||
{
|
||||
$comment = $this->seedCommentByUser(1);
|
||||
|
||||
$this->useCase->execute(new DeleteCommentRequest(
|
||||
commentId: $comment->getId(),
|
||||
requesterId: 1,
|
||||
requesterIsAdmin: false,
|
||||
));
|
||||
|
||||
$this->assertNull($this->commentRepo->find($comment->getId()));
|
||||
}
|
||||
|
||||
public function test_admin_can_delete_anyones_comment(): void
|
||||
{
|
||||
$comment = $this->seedCommentByUser(1);
|
||||
|
||||
$this->useCase->execute(new DeleteCommentRequest(
|
||||
commentId: $comment->getId(),
|
||||
requesterId: 99,
|
||||
requesterIsAdmin: true,
|
||||
));
|
||||
|
||||
$this->assertNull($this->commentRepo->find($comment->getId()));
|
||||
}
|
||||
|
||||
public function test_other_user_cannot_delete_comment(): void
|
||||
{
|
||||
$comment = $this->seedCommentByUser(1);
|
||||
|
||||
$this->expectException(ForbiddenException::class);
|
||||
$this->useCase->execute(new DeleteCommentRequest(
|
||||
commentId: $comment->getId(),
|
||||
requesterId: 2,
|
||||
requesterIsAdmin: false,
|
||||
));
|
||||
}
|
||||
|
||||
public function test_forbidden_delete_does_not_remove_comment(): void
|
||||
{
|
||||
$comment = $this->seedCommentByUser(1);
|
||||
|
||||
try {
|
||||
$this->useCase->execute(new DeleteCommentRequest(
|
||||
commentId: $comment->getId(),
|
||||
requesterId: 2,
|
||||
requesterIsAdmin: false,
|
||||
));
|
||||
} catch (ForbiddenException) {
|
||||
// expected
|
||||
}
|
||||
|
||||
$this->assertNotNull($this->commentRepo->find($comment->getId()));
|
||||
}
|
||||
}
|
||||
|
|
@ -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 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());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue