public function let(AuthenticationMapper $authenticationMapper, UserMapper $userMapper, User $user) { $hash = password_hash('abc', PASSWORD_DEFAULT); $key = KeyFactory::generateEncryptionKey(); $authenticationData = new Authentication(1, 'userA', $hash, $key->getRawKeyMaterial()); $this->authenticationData = $authenticationData; $orphanAuthData = new Authentication(2, 'orphan', $hash, $key->getRawKeyMaterial()); $authenticationMapper->findByUsername(Argument::any())->willReturn(null); $authenticationMapper->findByUsername('userA')->willReturn($authenticationData); $authenticationMapper->findByUsername('orphan')->willReturn($orphanAuthData); $authenticationMapper->findByUserId(Argument::any())->willReturn(null); $authenticationMapper->findByUserId(1)->willReturn($authenticationData); $authenticationMapper->update($authenticationData)->willReturn(true); $user->getId()->willReturn(1); $userMapper->findByEmail(Argument::any())->willReturn(null); $userMapper->findByEmail('*****@*****.**')->willReturn($user); $userMapper->getUser(Argument::any())->willReturn(null); $userMapper->getUser(1)->willReturn($user); $this->systemEncryptionKey = KeyFactory::generateEncryptionKey(); $this->beConstructedWith($authenticationMapper, $userMapper, $this->systemEncryptionKey->getRawKeyMaterial(), false, false); }
/** * Very similar to create, except that it won't log the user in. This was created to satisfy circumstances where * you are creating users from an admin panel for example. This function is also used by create. * * @param User $user * @param string $username * @param string $password * * @return AuthenticationRecordInterface * @throws EmailUsernameTakenException * @throws MismatchedEmailsException * @throws PersistedUserRequiredException * @throws UsernameTakenException */ public function registerAuthenticationRecord(User $user, string $username, string $password) : AuthenticationRecordInterface { if (!$user->getId()) { throw new PersistedUserRequiredException("Your user must have an ID before you can create auth records with it"); } if ($this->authenticationProvider->findByUsername($username)) { throw new UsernameTakenException(); } if (filter_var($username, FILTER_VALIDATE_EMAIL)) { if ($user->getEmail() != $username) { throw new MismatchedEmailsException(); } if ($emailUser = $this->userProvider->findByEmail($username)) { if ($emailUser != $user) { throw new EmailUsernameTakenException(); } } } $hash = password_hash($password, PASSWORD_DEFAULT); $auth = $this->authenticationProvider->create($user->getId(), $username, $hash, KeyFactory::generateEncryptionKey()->getRawKeyMaterial()); $this->authenticationProvider->save($auth); return $auth; }