diff --git a/app/Plan/UseCases/CreatePlan.php b/app/Plan/UseCases/CreatePlan.php index 1c2ea6e..c172653 100644 --- a/app/Plan/UseCases/CreatePlan.php +++ b/app/Plan/UseCases/CreatePlan.php @@ -6,6 +6,7 @@ use App\Plan\CreatePlanDto; use App\Plan\Plan; use App\Plan\PlanRepository; use App\User\UserRepository; +use DomainException; class CreatePlan { @@ -14,9 +15,17 @@ class CreatePlan private UserRepository $userRepo, ) {} + /** + * @throws DomainException + */ public function execute(CreatePlanRequest $request): Plan { - $user = $this->userRepo->find($request->userId); + $userId = $request->userId; + $user = $this->userRepo->find($userId); + + if ($user === null) { + throw new DomainException("User with id: $userId doesnt exist"); + } return $this->planRepo->create(new CreatePlanDto( name: $request->name, diff --git a/tests/Unit/Plan/UseCases/CreatePlanTest.php b/tests/Unit/Plan/UseCases/CreatePlanTest.php index c03b939..9366c80 100644 --- a/tests/Unit/Plan/UseCases/CreatePlanTest.php +++ b/tests/Unit/Plan/UseCases/CreatePlanTest.php @@ -8,6 +8,7 @@ use App\User\UseCases\CreateUserDto; use App\User\User; use App\User\UserRepository; use App\ValueObjects\EmailAddress; +use DomainException; use Tests\Fakes\FakePlanRepository; use PHPUnit\Framework\TestCase; use Tests\Fakes\FakeUserRepository; @@ -48,4 +49,14 @@ class CreatePlanTest extends TestCase )); $this->assertInstanceOf(User::class, $plan->getUser()); } + + public function test_nonexistant_user_id_throws(): void + { + $this->expectException(DomainException::class); + $this->expectExceptionMessage("User with id: 1 doesnt exist"); + $plan = $this->useCase->execute(new CreatePlanRequest( + userId: 1, + name: 'testPlan', + )); + } }