extract auth test fakes
This commit is contained in:
parent
ae07a6ff7c
commit
64acbfad60
6 changed files with 58 additions and 29 deletions
15
backend/tests/Fakes/FakeClock.php
Normal file
15
backend/tests/Fakes/FakeClock.php
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Fakes;
|
||||||
|
|
||||||
|
use App\Auth\Clock;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
use DateTimeZone;
|
||||||
|
|
||||||
|
class FakeClock implements Clock
|
||||||
|
{
|
||||||
|
public function now(): DateTimeImmutable
|
||||||
|
{
|
||||||
|
return new DateTimeImmutable('2026-05-18 12:00:00', new DateTimeZone('UTC'));
|
||||||
|
}
|
||||||
|
}
|
||||||
18
backend/tests/Fakes/FakeHasher.php
Normal file
18
backend/tests/Fakes/FakeHasher.php
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Fakes;
|
||||||
|
|
||||||
|
use App\Auth\PasswordHasher;
|
||||||
|
|
||||||
|
class FakeHasher implements PasswordHasher
|
||||||
|
{
|
||||||
|
public function hash(string $password): string
|
||||||
|
{
|
||||||
|
return 'hashed-' . $password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function verify(string $password, string $hash): bool
|
||||||
|
{
|
||||||
|
return $hash === 'hashed-' . $password;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
backend/tests/Fakes/FakeTokenGenerator.php
Normal file
13
backend/tests/Fakes/FakeTokenGenerator.php
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Fakes;
|
||||||
|
|
||||||
|
use App\Auth\TokenGenerator;
|
||||||
|
|
||||||
|
class FakeTokenGenerator implements TokenGenerator
|
||||||
|
{
|
||||||
|
public function generate(): string
|
||||||
|
{
|
||||||
|
return 'fake-token-123';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,8 +8,10 @@ use App\Auth\UseCases\AuthenticateUser\AuthenticateUserRequest;
|
||||||
use App\Exceptions\BadRequestException;
|
use App\Exceptions\BadRequestException;
|
||||||
use App\Exceptions\UnauthorizedException;
|
use App\Exceptions\UnauthorizedException;
|
||||||
use App\Shared\ValueObject\EmailAddress;
|
use App\Shared\ValueObject\EmailAddress;
|
||||||
|
use App\User\CreateUserDto;
|
||||||
use App\User\User;
|
use App\User\User;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Tests\Fakes\FakeHasher;
|
||||||
use Tests\Fakes\FakeUserRepository;
|
use Tests\Fakes\FakeUserRepository;
|
||||||
|
|
||||||
class AuthenticateUserTest extends TestCase
|
class AuthenticateUserTest extends TestCase
|
||||||
|
|
@ -21,17 +23,7 @@ class AuthenticateUserTest extends TestCase
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->userRepo = new FakeUserRepository();
|
$this->userRepo = new FakeUserRepository();
|
||||||
$this->hasher = new class implements PasswordHasher {
|
$this->hasher = new FakeHasher();
|
||||||
public function hash(string $password): string
|
|
||||||
{
|
|
||||||
return 'hashed-' . $password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function verify(string $password, string $hash): bool
|
|
||||||
{
|
|
||||||
return $hash === 'hashed-' . $password;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
$this->authenticateUser = new AuthenticateUser($this->userRepo, $this->hasher);
|
$this->authenticateUser = new AuthenticateUser($this->userRepo, $this->hasher);
|
||||||
}
|
}
|
||||||
|
|
@ -39,7 +31,7 @@ class AuthenticateUserTest extends TestCase
|
||||||
public function testAuthenticatesValidUser(): void
|
public function testAuthenticatesValidUser(): void
|
||||||
{
|
{
|
||||||
$email = new EmailAddress('user@example.com');
|
$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');
|
$request = new AuthenticateUserRequest('user@example.com', 'secret');
|
||||||
$user = $this->authenticateUser->execute($request);
|
$user = $this->authenticateUser->execute($request);
|
||||||
|
|
@ -78,7 +70,7 @@ class AuthenticateUserTest extends TestCase
|
||||||
public function testThrowsWhenPasswordIncorrect(): void
|
public function testThrowsWhenPasswordIncorrect(): void
|
||||||
{
|
{
|
||||||
$email = new EmailAddress('user@example.com');
|
$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->expectException(UnauthorizedException::class);
|
||||||
$this->expectExceptionMessage('invalid credentials');
|
$this->expectExceptionMessage('invalid credentials');
|
||||||
|
|
|
||||||
|
|
@ -3,36 +3,26 @@
|
||||||
namespace Tests\Unit\Auth\UseCases;
|
namespace Tests\Unit\Auth\UseCases;
|
||||||
|
|
||||||
use App\Auth\Clock;
|
use App\Auth\Clock;
|
||||||
use App\Auth\TokenGenerator;
|
|
||||||
use App\Auth\UseCases\CreateSession\CreateSession;
|
use App\Auth\UseCases\CreateSession\CreateSession;
|
||||||
use App\Shared\ValueObject\EmailAddress;
|
use App\Shared\ValueObject\EmailAddress;
|
||||||
use App\User\User;
|
use App\User\User;
|
||||||
use DateTimeImmutable;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Tests\Fakes\FakeClock;
|
||||||
use Tests\Fakes\FakeSessionRepository;
|
use Tests\Fakes\FakeSessionRepository;
|
||||||
|
use Tests\Fakes\FakeTokenGenerator;
|
||||||
|
|
||||||
class CreateSessionTest extends TestCase
|
class CreateSessionTest extends TestCase
|
||||||
{
|
{
|
||||||
private FakeSessionRepository $sessionRepo;
|
private FakeSessionRepository $sessionRepo;
|
||||||
private TokenGenerator $tokenGenerator;
|
private FakeTokenGenerator $tokenGenerator;
|
||||||
private Clock $clock;
|
private Clock $clock;
|
||||||
private CreateSession $createSession;
|
private CreateSession $createSession;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->sessionRepo = new FakeSessionRepository();
|
$this->sessionRepo = new FakeSessionRepository();
|
||||||
$this->tokenGenerator = new class implements TokenGenerator {
|
$this->tokenGenerator = new FakeTokenGenerator();
|
||||||
public function generate(): string
|
$this->clock = new FakeClock();
|
||||||
{
|
|
||||||
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->createSession = new CreateSession($this->sessionRepo, $this->tokenGenerator, $this->clock);
|
$this->createSession = new CreateSession($this->sessionRepo, $this->tokenGenerator, $this->clock);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Tests\Unit\Auth\UseCases;
|
namespace Tests\Unit\Auth\UseCases;
|
||||||
|
|
||||||
|
use App\Auth\CreateSessionDto;
|
||||||
use App\Auth\UseCases\Logout\Logout;
|
use App\Auth\UseCases\Logout\Logout;
|
||||||
use App\Shared\ValueObject\EmailAddress;
|
use App\Shared\ValueObject\EmailAddress;
|
||||||
use App\User\User;
|
use App\User\User;
|
||||||
|
|
@ -24,7 +25,7 @@ class LogoutTest extends TestCase
|
||||||
$email = new EmailAddress('user@example.com');
|
$email = new EmailAddress('user@example.com');
|
||||||
$user = new User(1, $email, 'hashed-password');
|
$user = new User(1, $email, 'hashed-password');
|
||||||
|
|
||||||
$session = $this->sessionRepo->create(new \App\Auth\CreateSessionDto(
|
$session = $this->sessionRepo->create(new CreateSessionDto(
|
||||||
'session-token',
|
'session-token',
|
||||||
$user,
|
$user,
|
||||||
new \DateTimeImmutable(),
|
new \DateTimeImmutable(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue