7 cases: null + whitespace title -> BadRequest; null + whitespace body -> BadRequest; valid request returns Post with correct userId/title/body and createdAt = clock.now(); the post is findable via the repo afterwards; title and body get trimmed of leading/trailing whitespace. fails red - CreatePost class absent.
118 lines
3.3 KiB
PHP
118 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Post\UseCases;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Post\Post;
|
|
use App\Post\UseCases\CreatePost\CreatePost;
|
|
use App\Post\UseCases\CreatePost\CreatePostRequest;
|
|
use DateTimeImmutable;
|
|
use DateTimeZone;
|
|
use Tests\Fakes\FakeClock;
|
|
use Tests\Fakes\FakePostRepository;
|
|
use Tests\TestCase;
|
|
|
|
class CreatePostTest extends TestCase
|
|
{
|
|
private FakePostRepository $postRepo;
|
|
|
|
private FakeClock $clock;
|
|
|
|
private DateTimeImmutable $now;
|
|
|
|
private CreatePost $useCase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->now = new DateTimeImmutable(
|
|
'2026-05-06T12:00:00',
|
|
new DateTimeZone('UTC')
|
|
);
|
|
$this->postRepo = new FakePostRepository;
|
|
$this->clock = new FakeClock($this->now);
|
|
$this->useCase = new CreatePost(
|
|
$this->postRepo,
|
|
$this->clock,
|
|
);
|
|
}
|
|
|
|
public function test_null_title_throws_bad_request(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->useCase->execute(new CreatePostRequest(
|
|
userId: 7,
|
|
title: null,
|
|
body: 'some body content',
|
|
));
|
|
}
|
|
|
|
public function test_empty_title_throws_bad_request(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->useCase->execute(new CreatePostRequest(
|
|
userId: 7,
|
|
title: ' ',
|
|
body: 'some body content',
|
|
));
|
|
}
|
|
|
|
public function test_null_body_throws_bad_request(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->useCase->execute(new CreatePostRequest(
|
|
userId: 7,
|
|
title: 'My Post',
|
|
body: null,
|
|
));
|
|
}
|
|
|
|
public function test_empty_body_throws_bad_request(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->useCase->execute(new CreatePostRequest(
|
|
userId: 7,
|
|
title: 'My Post',
|
|
body: ' ',
|
|
));
|
|
}
|
|
|
|
public function test_valid_request_returns_post(): void
|
|
{
|
|
$post = $this->useCase->execute(new CreatePostRequest(
|
|
userId: 7,
|
|
title: 'My Post',
|
|
body: 'Some body content here.',
|
|
));
|
|
|
|
$this->assertInstanceOf(Post::class, $post);
|
|
$this->assertSame(7, $post->getUserId());
|
|
$this->assertSame('My Post', $post->getTitle());
|
|
$this->assertSame('Some body content here.', $post->getBody());
|
|
$this->assertEquals($this->now, $post->getCreatedAt());
|
|
}
|
|
|
|
public function test_post_is_findable_after_creation(): void
|
|
{
|
|
$created = $this->useCase->execute(new CreatePostRequest(
|
|
userId: 7,
|
|
title: 'My Post',
|
|
body: 'Some body content here.',
|
|
));
|
|
|
|
$found = $this->postRepo->find($created->getId());
|
|
$this->assertNotNull($found);
|
|
$this->assertSame('My Post', $found->getTitle());
|
|
}
|
|
|
|
public function test_title_and_body_are_trimmed(): void
|
|
{
|
|
$post = $this->useCase->execute(new CreatePostRequest(
|
|
userId: 7,
|
|
title: ' Padded Title ',
|
|
body: " Padded body \n",
|
|
));
|
|
|
|
$this->assertSame('Padded Title', $post->getTitle());
|
|
$this->assertSame('Padded body', $post->getBody());
|
|
}
|
|
}
|