Red phase: write AuthenticateUserTest with cases for valid credentials, empty email/password (null and empty string), unknown email, wrong password, and fresh instance guarantee. Fakes included.
63 lines
1.3 KiB
PHP
63 lines
1.3 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
|
|
{
|
|
private array $users = [];
|
|
|
|
public function create(CreateUserDto $dto): User
|
|
{
|
|
$id = $this->nextId();
|
|
|
|
$user = new User(
|
|
id: $id,
|
|
email: $dto->email,
|
|
passwordHash: $dto->passwordHash,
|
|
);
|
|
|
|
$this->users[$id] = $user;
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function findByEmail(EmailAddress $email): ?User
|
|
{
|
|
foreach ($this->users as $user) {
|
|
if ($user->getEmail()->value() === $email->value()) {
|
|
return new User(
|
|
id: $user->getId(),
|
|
email: $user->getEmail(),
|
|
passwordHash: $user->getPasswordHash(),
|
|
);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function find(int $id): ?User
|
|
{
|
|
$user = $this->users[$id] ?? null;
|
|
|
|
if ($user === null) {
|
|
return null;
|
|
}
|
|
|
|
return new User(
|
|
id: $user->getId(),
|
|
email: $user->getEmail(),
|
|
passwordHash: $user->getPasswordHash(),
|
|
);
|
|
}
|
|
|
|
private function nextId(): int
|
|
{
|
|
return count($this->users);
|
|
}
|
|
}
|