/** * Handles the given command. * * @param ChangeUserPasswordCommand $aCommand The command * * @throws UserDoesNotExistException when the user does not exist * @throws UserPasswordInvalidException when the user password is invalid */ public function __invoke(ChangeUserPasswordCommand $aCommand) { $user = $this->repository->userOfId(new UserId($aCommand->id())); if (null === $user) { throw new UserDoesNotExistException(); } if (false === $user->password()->equals($aCommand->oldPlainPassword(), $this->encoder)) { throw new UserPasswordInvalidException(); } $user->changePassword(UserPassword::fromPlain($aCommand->newPlainPassword(), $this->encoder)); $this->repository->persist($user); }
function it_changes_password(ChangeUserPasswordCommand $command, UserRepository $repository, User $user) { $encoder = new DummyUserPasswordEncoder('encoded-pass'); $userPassword = UserPassword::fromPlain('old-plain-pass', $encoder, 'dummy-salt'); $command->id()->shouldBeCalled()->willReturn('user-id'); $repository->userOfId(new UserId('user-id'))->shouldBeCalled()->willReturn($user); $user->password()->shouldBeCalled()->willReturn($userPassword); $command->oldPlainPassword()->shouldBeCalled()->willReturn('old-plain-pass'); $command->newPlainPassword()->shouldBeCalled()->willReturn('new-plain-pass'); $user->changePassword(Argument::type(UserPassword::class))->shouldBeCalled(); $repository->persist($user)->shouldBeCalled(); $this->__invoke($command); }