extract auth test fakes

This commit is contained in:
Yisroel Baum 2026-05-18 22:01:45 +03:00
parent ae07a6ff7c
commit 64acbfad60
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
6 changed files with 58 additions and 29 deletions

View file

@ -8,8 +8,10 @@ use App\Auth\UseCases\AuthenticateUser\AuthenticateUserRequest;
use App\Exceptions\BadRequestException;
use App\Exceptions\UnauthorizedException;
use App\Shared\ValueObject\EmailAddress;
use App\User\CreateUserDto;
use App\User\User;
use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakeHasher;
use Tests\Fakes\FakeUserRepository;
class AuthenticateUserTest extends TestCase
@ -21,17 +23,7 @@ class AuthenticateUserTest extends TestCase
protected function setUp(): void
{
$this->userRepo = new FakeUserRepository();
$this->hasher = new class implements PasswordHasher {
public function hash(string $password): string
{
return 'hashed-' . $password;
}
public function verify(string $password, string $hash): bool
{
return $hash === 'hashed-' . $password;
}
};
$this->hasher = new FakeHasher();
$this->authenticateUser = new AuthenticateUser($this->userRepo, $this->hasher);
}
@ -39,7 +31,7 @@ class AuthenticateUserTest extends TestCase
public function testAuthenticatesValidUser(): void
{
$email = new EmailAddress('user@example.com');
$this->userRepo->create(new \App\User\CreateUserDto($email, 'hashed-secret'));
$this->userRepo->create(new CreateUserDto($email, 'hashed-secret'));
$request = new AuthenticateUserRequest('user@example.com', 'secret');
$user = $this->authenticateUser->execute($request);
@ -78,7 +70,7 @@ class AuthenticateUserTest extends TestCase
public function testThrowsWhenPasswordIncorrect(): void
{
$email = new EmailAddress('user@example.com');
$this->userRepo->create(new \App\User\CreateUserDto($email, 'hashed-secret'));
$this->userRepo->create(new CreateUserDto($email, 'hashed-secret'));
$this->expectException(UnauthorizedException::class);
$this->expectExceptionMessage('invalid credentials');

View file

@ -3,36 +3,26 @@
namespace Tests\Unit\Auth\UseCases;
use App\Auth\Clock;
use App\Auth\TokenGenerator;
use App\Auth\UseCases\CreateSession\CreateSession;
use App\Shared\ValueObject\EmailAddress;
use App\User\User;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeSessionRepository;
use Tests\Fakes\FakeTokenGenerator;
class CreateSessionTest extends TestCase
{
private FakeSessionRepository $sessionRepo;
private TokenGenerator $tokenGenerator;
private FakeTokenGenerator $tokenGenerator;
private Clock $clock;
private CreateSession $createSession;
protected function setUp(): void
{
$this->sessionRepo = new FakeSessionRepository();
$this->tokenGenerator = new class implements TokenGenerator {
public function generate(): string
{
return 'fake-token-123';
}
};
$this->clock = new class implements Clock {
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('2026-05-18 12:00:00', new \DateTimeZone('UTC'));
}
};
$this->tokenGenerator = new FakeTokenGenerator();
$this->clock = new FakeClock();
$this->createSession = new CreateSession($this->sessionRepo, $this->tokenGenerator, $this->clock);
}

View file

@ -2,6 +2,7 @@
namespace Tests\Unit\Auth\UseCases;
use App\Auth\CreateSessionDto;
use App\Auth\UseCases\Logout\Logout;
use App\Shared\ValueObject\EmailAddress;
use App\User\User;
@ -24,7 +25,7 @@ class LogoutTest extends TestCase
$email = new EmailAddress('user@example.com');
$user = new User(1, $email, 'hashed-password');
$session = $this->sessionRepo->create(new \App\Auth\CreateSessionDto(
$session = $this->sessionRepo->create(new CreateSessionDto(
'session-token',
$user,
new \DateTimeImmutable(),