diff --git a/src/app/Packages/Features/QueryUseCases/Tests/UseCase/GetUserByIdUseCaseTest.php b/src/app/Packages/Features/QueryUseCases/Tests/UseCase/GetUserByIdUseCaseTest.php new file mode 100644 index 0000000..42e71fc --- /dev/null +++ b/src/app/Packages/Features/QueryUseCases/Tests/UseCase/GetUserByIdUseCaseTest.php @@ -0,0 +1,67 @@ +unique()->randomNumber(5), + firstName: $faker->firstName(), + lastName: $faker->lastName(), + email: $faker->unique()->safeEmail(), + ageRange: 'teens', + subscriptionTier: 'free', + subscriptionExpiresAt: null, + ); + } + + public function test_handle_returns_user_dto_when_user_exists(): void + { + $dto = $this->buildDto(); + + /** @var UserRepositroyInterface|MockInterface $repository */ + $repository = Mockery::mock(UserRepositroyInterface::class); + $repository->shouldReceive('findById') + ->once() + ->with($dto->id) + ->andReturn($dto); + + $result = (new GetUserByIdUseCase($repository))->handle($dto->id); + + $this->assertSame($dto, $result); + } + + public function test_handle_throws_exception_when_user_not_found(): void + { + /** @var UserRepositroyInterface|MockInterface $repository */ + $repository = Mockery::mock(UserRepositroyInterface::class); + $repository->shouldReceive('findById') + ->once() + ->with(999) + ->andReturn(null); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('User not found.'); + + (new GetUserByIdUseCase($repository))->handle(999); + } +} diff --git a/src/app/Packages/Features/QueryUseCases/UseCase/User/GetUserByIdUseCase.php b/src/app/Packages/Features/QueryUseCases/UseCase/User/GetUserByIdUseCase.php new file mode 100644 index 0000000..1fe1893 --- /dev/null +++ b/src/app/Packages/Features/QueryUseCases/UseCase/User/GetUserByIdUseCase.php @@ -0,0 +1,25 @@ +userRepository->findById($id); + + if ($userDto === null) { + throw new Exception('User not found.'); + } + + return $userDto; + } +}