add unit tests for user and auth

This commit is contained in:
Yisroel Baum 2026-05-18 21:36:10 +03:00
parent 613180d459
commit 410b752183
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
6 changed files with 315 additions and 0 deletions

View file

@ -0,0 +1,70 @@
<?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 $usersById = [];
private array $usersByEmail = [];
public function create(CreateUserDto $dto): User
{
$id = count($this->usersById) + 1;
$user = new User($id, $dto->email, $dto->passwordHash);
$this->usersById[$id] = $user;
$this->usersByEmail[$dto->email->value()] = $user;
return $user;
}
public function findByEmail(EmailAddress $email): ?User
{
if (! isset($this->usersByEmail[$email->value()])) {
return null;
}
$stored = $this->usersByEmail[$email->value()];
return new User(
$stored->getId(),
$stored->getEmail(),
$stored->getPasswordHash()
);
}
public function findByEmailDomain(string $domain): array
{
$result = [];
foreach ($this->usersByEmail as $email => $stored) {
if (str_ends_with($email, '@' . $domain)) {
$result[] = new User(
$stored->getId(),
$stored->getEmail(),
$stored->getPasswordHash()
);
}
}
return $result;
}
public function find(int $id): ?User
{
if (! isset($this->usersById[$id])) {
return null;
}
$stored = $this->usersById[$id];
return new User(
$stored->getId(),
$stored->getEmail(),
$stored->getPasswordHash()
);
}
}