/** * Handles the given command. * * @param InviteUserCommand $aCommand The command * * @throws UserAlreadyExistException when the user already exists */ public function __invoke(InviteUserCommand $aCommand) { $email = new UserEmail($aCommand->email()); $user = $this->repository->userOfEmail($email); if (null !== $user) { throw new UserAlreadyExistException(); } $user = $this->factory->build(new UserId($aCommand->id()), $email, array_map(function ($role) { return new UserRole($role); }, $aCommand->roles())); $this->repository->persist($user); }
function it_invites_user(InviteUserCommand $command, UserRepository $userRepository, UserFactoryInvite $factory, User $user) { $command->email()->shouldBeCalled()->willReturn('*****@*****.**'); $email = new UserEmail('*****@*****.**'); $userRepository->userOfEmail($email)->shouldBeCalled()->willReturn(null); $command->roles()->shouldBeCalled()->willReturn(['ROLE_USER']); $roles = [new UserRole('ROLE_USER')]; $command->id()->shouldBeCalled()->willReturn('user-id'); $id = new UserId('user-id'); $factory->build($id, $email, $roles)->shouldBeCalled()->willReturn($user); $userRepository->persist($user)->shouldBeCalled(); $this->__invoke($command); }