Attainly/backend/tests/Fakes/FakeUserRepository.php
2026-08-02 20:58:44 +03:00

56 lines
1.2 KiB
PHP

<?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 array<int, User>
*/
private array $users = [];
public function create(CreateUserDto $dto): User
{
$id = count($this->users) + 1;
$user = new User(
id: $id,
email: $dto->email,
passwordHash: $dto->passwordHash,
);
$this->users[$id] = $user;
return $this->copy($user);
}
public function find(int $id): ?User
{
$user = $this->users[$id] ?? null;
return $user === null ? null : $this->copy($user);
}
public function findByEmail(EmailAddress $email): ?User
{
foreach ($this->users as $user) {
if ($user->getEmail()->value() === $email->value()) {
return $this->copy($user);
}
}
return null;
}
private function copy(User $user): User
{
return new User(
id: $user->getId(),
email: $user->getEmail(),
passwordHash: $user->getPasswordHash(),
);
}
}