test that use case throws error if non existant user id given

This commit is contained in:
Yisroel Baum 2026-02-23 11:05:42 +02:00
parent 21da1a8991
commit f0eb4cf5d5
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 21 additions and 1 deletions

View file

@ -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,

View file

@ -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',
));
}
}