add User persistence: model, migration, eloquent + fake repo
UserModel maps users table (id, email unique, password_hash, is_admin bool default false). EloquentUserRepository implements UserRepository: create from CreateUserDto, find by id, findByEmail. toDomain() materializes a User entity wrapping email in EmailAddress vo. FakeUserRepository: in-memory map keyed by auto-incrementing id, returns defensive copies on read (per youngstartup pattern). composer stan script now passes --no-progress for cleaner ci output.
This commit is contained in:
parent
533320fcac
commit
eca73213f5
5 changed files with 171 additions and 1 deletions
66
backend/tests/Fakes/FakeUserRepository.php
Normal file
66
backend/tests/Fakes/FakeUserRepository.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\User\CreateUserDto;
|
||||
use App\User\User;
|
||||
use App\User\UserRepository;
|
||||
|
||||
class FakeUserRepository implements UserRepository
|
||||
{
|
||||
/**
|
||||
* @var User[]
|
||||
*/
|
||||
private array $existingUsers = [];
|
||||
|
||||
public function create(CreateUserDto $dto): User
|
||||
{
|
||||
$id = $this->getNextId();
|
||||
$user = new User(
|
||||
id: $id,
|
||||
email: $dto->email,
|
||||
passwordHash: $dto->passwordHash,
|
||||
isAdmin: $dto->isAdmin,
|
||||
);
|
||||
$this->existingUsers[$id] = $user;
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function find(int $id): ?User
|
||||
{
|
||||
$user = $this->existingUsers[$id] ?? null;
|
||||
if ($user === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new User(
|
||||
id: $user->getId(),
|
||||
email: $user->getEmail(),
|
||||
passwordHash: $user->getPasswordHash(),
|
||||
isAdmin: $user->isAdmin(),
|
||||
);
|
||||
}
|
||||
|
||||
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 null;
|
||||
}
|
||||
|
||||
private function getNextId(): int
|
||||
{
|
||||
return count($this->existingUsers) + 1;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue