test auth contracts
This commit is contained in:
parent
3da9c586c3
commit
b3266b38c8
15 changed files with 457 additions and 165 deletions
18
backend/tests/Fakes/FakePasswordHasher.php
Normal file
18
backend/tests/Fakes/FakePasswordHasher.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Auth\PasswordHasher;
|
||||
|
||||
class FakePasswordHasher implements PasswordHasher
|
||||
{
|
||||
public function hash(string $password): string
|
||||
{
|
||||
return 'hashed:'.$password;
|
||||
}
|
||||
|
||||
public function verify(string $password, string $hash): bool
|
||||
{
|
||||
return $this->hash($password) === $hash;
|
||||
}
|
||||
}
|
||||
28
backend/tests/Fakes/FakeTokenGenerator.php
Normal file
28
backend/tests/Fakes/FakeTokenGenerator.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Auth\TokenGenerator;
|
||||
use RuntimeException;
|
||||
|
||||
class FakeTokenGenerator implements TokenGenerator
|
||||
{
|
||||
private int $callCount = 0;
|
||||
|
||||
/**
|
||||
* @param string[] $tokens
|
||||
*/
|
||||
public function __construct(private array $tokens) {}
|
||||
|
||||
public function generate(): string
|
||||
{
|
||||
if ($this->callCount >= count($this->tokens)) {
|
||||
throw new RuntimeException('FakeTokenGenerator exhausted');
|
||||
}
|
||||
|
||||
$token = $this->tokens[$this->callCount];
|
||||
$this->callCount++;
|
||||
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
56
backend/tests/Fakes/FakeUserRepository.php
Normal file
56
backend/tests/Fakes/FakeUserRepository.php
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<?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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue