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()); } }