function it_does_not_grant_the_user_role_because_the_user_does_not_exist(UserRepository $repository, GrantUserRoleCommand $command) { $command->id()->shouldBeCalled()->willReturn('user-id'); $id = new UserId('user-id'); $repository->userOfId($id)->shouldBeCalled()->willReturn(null); $this->shouldThrow(new UserDoesNotExistException())->during__invoke($command); }
/** * Handles the given command. * * @param GrantUserRoleCommand $aCommand The command * * @throws UserDoesNotExistException when user does not exist */ public function __invoke(GrantUserRoleCommand $aCommand) { $user = $this->repository->userOfId(new UserId($aCommand->id())); if (null === $user) { throw new UserDoesNotExistException(); } $user->grant(new UserRole($aCommand->role())); $this->repository->persist($user); }