extend User entity with displayname and email confirmation
Add display_name (unique) and email_confirmed_at columns plus matching getters, DTO fields, repo methods (findByDisplayName, update), and migration. Existing auth tests updated to construct User with the new params.
This commit is contained in:
parent
d547ec2c61
commit
298b8634ec
10 changed files with 131 additions and 15 deletions
|
|
@ -6,6 +6,7 @@ use App\Shared\ValueObject\EmailAddress;
|
|||
use App\User\CreateUserDto;
|
||||
use App\User\User;
|
||||
use App\User\UserRepository;
|
||||
use RuntimeException;
|
||||
|
||||
class FakeUserRepository implements UserRepository
|
||||
{
|
||||
|
|
@ -20,12 +21,14 @@ class FakeUserRepository implements UserRepository
|
|||
$user = new User(
|
||||
id: $id,
|
||||
email: $dto->email,
|
||||
displayName: $dto->displayName,
|
||||
passwordHash: $dto->passwordHash,
|
||||
isAdmin: $dto->isAdmin,
|
||||
emailConfirmedAt: $dto->emailConfirmedAt,
|
||||
);
|
||||
$this->existingUsers[$id] = $user;
|
||||
|
||||
return $user;
|
||||
return $this->copy($user);
|
||||
}
|
||||
|
||||
public function find(int $id): ?User
|
||||
|
|
@ -35,30 +38,59 @@ class FakeUserRepository implements UserRepository
|
|||
return null;
|
||||
}
|
||||
|
||||
return new User(
|
||||
id: $user->getId(),
|
||||
email: $user->getEmail(),
|
||||
passwordHash: $user->getPasswordHash(),
|
||||
isAdmin: $user->isAdmin(),
|
||||
);
|
||||
return $this->copy($user);
|
||||
}
|
||||
|
||||
public function findByEmail(EmailAddress $email): ?User
|
||||
{
|
||||
foreach ($this->existingUsers as $user) {
|
||||
if ($user->getEmail()->equals($email)) {
|
||||
return new User(
|
||||
id: $user->getId(),
|
||||
email: $user->getEmail(),
|
||||
passwordHash: $user->getPasswordHash(),
|
||||
isAdmin: $user->isAdmin(),
|
||||
);
|
||||
return $this->copy($user);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function findByDisplayName(string $displayName): ?User
|
||||
{
|
||||
foreach ($this->existingUsers as $user) {
|
||||
if ($user->getDisplayName() === $displayName) {
|
||||
return $this->copy($user);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function update(User $user): User
|
||||
{
|
||||
$id = $user->getId();
|
||||
if (! isset($this->existingUsers[$id])) {
|
||||
throw new RuntimeException(
|
||||
"User with id: $id does not exist"
|
||||
);
|
||||
}
|
||||
$this->existingUsers[$id] = $user;
|
||||
|
||||
return $this->copy($user);
|
||||
}
|
||||
|
||||
private function copy(User $user): User
|
||||
{
|
||||
return new User(
|
||||
id: $user->getId(),
|
||||
email: $user->getEmail(),
|
||||
displayName: $user->getDisplayName(),
|
||||
passwordHash: $user->getPasswordHash(),
|
||||
isAdmin: $user->isAdmin(),
|
||||
emailConfirmedAt: $user->getEmailConfirmedAt(),
|
||||
);
|
||||
}
|
||||
|
||||
private function getNextId(): int
|
||||
{
|
||||
return count($this->existingUsers) + 1;
|
||||
|
|
|
|||
|
|
@ -58,13 +58,15 @@ class AuthMiddlewareTest extends TestCase
|
|||
};
|
||||
}
|
||||
|
||||
private function makeUser(int $id = 7): User
|
||||
private function makeUser(int $id): User
|
||||
{
|
||||
return new User(
|
||||
id: $id,
|
||||
email: new EmailAddress('user@example.com'),
|
||||
displayName: 'user',
|
||||
passwordHash: 'hashed:irrelevant',
|
||||
isAdmin: false,
|
||||
emailConfirmedAt: null,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -100,7 +102,7 @@ class AuthMiddlewareTest extends TestCase
|
|||
{
|
||||
$this->sessionRepo->create(new CreateSessionDto(
|
||||
token: 'expired-token',
|
||||
user: $this->makeUser(),
|
||||
user: $this->makeUser(7),
|
||||
createdAt: $this->now->modify('-8 days'),
|
||||
expiresAt: $this->now->modify('-1 day'),
|
||||
));
|
||||
|
|
|
|||
|
|
@ -46,8 +46,10 @@ class CreateSessionTest extends TestCase
|
|||
return new User(
|
||||
id: 7,
|
||||
email: new EmailAddress('user@example.com'),
|
||||
displayName: 'user',
|
||||
passwordHash: 'hashed:irrelevant',
|
||||
isAdmin: false,
|
||||
emailConfirmedAt: null,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,8 +34,10 @@ class LogoutTest extends TestCase
|
|||
$user = new User(
|
||||
id: 7,
|
||||
email: new EmailAddress('user@example.com'),
|
||||
displayName: 'user',
|
||||
passwordHash: 'hashed:irrelevant',
|
||||
isAdmin: false,
|
||||
emailConfirmedAt: null,
|
||||
);
|
||||
$this->sessionRepo->create(new CreateSessionDto(
|
||||
token: 'token-abc',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue