From 504554bf7fb07b9b0ba336f8ea8fccb19b3abbb0 Mon Sep 17 00:00:00 2001 From: yisroel Date: Wed, 6 May 2026 15:22:57 +0300 Subject: [PATCH] test CreatePost use case 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. --- .../Unit/Post/UseCases/CreatePostTest.php | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 backend/tests/Unit/Post/UseCases/CreatePostTest.php diff --git a/backend/tests/Unit/Post/UseCases/CreatePostTest.php b/backend/tests/Unit/Post/UseCases/CreatePostTest.php new file mode 100644 index 0000000..f7ebefd --- /dev/null +++ b/backend/tests/Unit/Post/UseCases/CreatePostTest.php @@ -0,0 +1,118 @@ +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()); + } +}