userRepo = new FakeUserRepository(); $this->passwordHasher = new FakePasswordHasher(); $this->useCase = new CreateUser( $this->userRepo, $this->passwordHasher, ); } public function test_create_user(): void { $this->useCase->execute(new CreateUserRequest( email: 'test@test.com', password: 'password1', isAdmin: false, )); $user = $this->userRepo->find(0); $this->assertInstanceOf(User::class, $user); $this->assertEquals('test@test.com', $user->getEmail()); } public function test_throws_if_email_is_null(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('email is required'); $this->useCase->execute(new CreateUserRequest( email: null, password: 'password1', isAdmin: false, )); } public function test_is_admin_can_be_set_true(): void { $this->useCase->execute(new CreateUserRequest( email: 'test@test.com', password: 'password1', isAdmin: true, )); $user = $this->userRepo->find(0); $this->assertTrue($user->isAdmin()); } public function test_throws_when_email_already_taken(): void { $this->useCase->execute(new CreateUserRequest( email: 'test@test.com', password: 'password1', isAdmin: false, )); $this->expectException(BadRequestException::class); $this->expectExceptionMessage('email already taken'); $this->useCase->execute(new CreateUserRequest( email: 'test@test.com', password: 'password1', isAdmin: false )); } public function test_throws_if_password_is_null(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('password is required'); $this->useCase->execute(new CreateUserRequest( email: 'test@test.com', password: null, isAdmin: false, )); } public function test_throws_if_password_too_short(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage( 'password must be at least 8 characters' ); $this->useCase->execute(new CreateUserRequest( email: 'test@test.com', password: 'short', isAdmin: false, )); } public function test_stores_hashed_password(): void { $this->useCase->execute(new CreateUserRequest( email: 'test@test.com', password: 'password1', isAdmin: false, )); $user = $this->userRepo->find(0); $this->assertNotEquals('password1', $user->getPasswordHash()); $this->assertTrue( $this->passwordHasher->verify( 'password1', $user->getPasswordHash() ) ); } }