From eb0ebc6f63e52e487388ae813e76febd84ff4c19 Mon Sep 17 00:00:00 2001 From: yisroel Date: Wed, 6 May 2026 15:25:36 +0300 Subject: [PATCH] test GetPost use case 4 cases: zero/negative id -> BadRequest; unknown id -> null (controller maps to 404); existing id returns the Post. GetPost takes int id directly (no Request object - the value is trivial and controllers pull it from a route param). --- .../tests/Unit/Post/UseCases/GetPostTest.php | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 backend/tests/Unit/Post/UseCases/GetPostTest.php diff --git a/backend/tests/Unit/Post/UseCases/GetPostTest.php b/backend/tests/Unit/Post/UseCases/GetPostTest.php new file mode 100644 index 0000000..d0e2740 --- /dev/null +++ b/backend/tests/Unit/Post/UseCases/GetPostTest.php @@ -0,0 +1,66 @@ +now = new DateTimeImmutable( + '2026-05-06T12:00:00', + new DateTimeZone('UTC') + ); + $this->postRepo = new FakePostRepository; + $this->useCase = new GetPost($this->postRepo); + } + + public function test_zero_id_throws_bad_request(): void + { + $this->expectException(BadRequestException::class); + $this->useCase->execute(0); + } + + public function test_negative_id_throws_bad_request(): void + { + $this->expectException(BadRequestException::class); + $this->useCase->execute(-5); + } + + public function test_unknown_id_returns_null(): void + { + $result = $this->useCase->execute(999); + + $this->assertNull($result); + } + + public function test_existing_id_returns_post(): void + { + $created = $this->postRepo->create(new CreatePostDto( + userId: 7, + title: 'My Post', + body: 'Some body content.', + createdAt: $this->now, + )); + + $result = $this->useCase->execute($created->getId()); + + $this->assertInstanceOf(Post::class, $result); + $this->assertSame('My Post', $result->getTitle()); + $this->assertSame(7, $result->getUserId()); + } +}