function it_does_not_change_password_because_user_password_is_invalid(ChangeUserPasswordCommand $command, UserRepository $repository, User $user) { $encoder = new DummyUserPasswordEncoder('encoded-pass', false); $userPassword = UserPassword::fromPlain('plain-pass', $encoder, 'dummy-salt'); $this->beConstructedWith($repository, $encoder); $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'); $this->shouldThrow(UserPasswordInvalidException::class)->during__invoke($command); }
/** * 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); }