63 lines
1.4 KiB
PHP
63 lines
1.4 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;
|
|
}
|
|
|
|
public function update(User $user): User
|
|
{
|
|
$this->users[$user->getId()] = $this->copy($user);
|
|
|
|
return $this->copy($user);
|
|
}
|
|
|
|
private function copy(User $user): User
|
|
{
|
|
return new User(
|
|
id: $user->getId(),
|
|
email: $user->getEmail(),
|
|
passwordHash: $user->getPasswordHash(),
|
|
);
|
|
}
|
|
}
|