From 64acbfad60f63c1aa4f8ca7ac483a4c0618e4cbc Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Mon, 18 May 2026 22:01:45 +0300 Subject: [PATCH] extract auth test fakes --- backend/tests/Fakes/FakeClock.php | 15 ++++++++++++++ backend/tests/Fakes/FakeHasher.php | 18 +++++++++++++++++ backend/tests/Fakes/FakeTokenGenerator.php | 13 ++++++++++++ .../Auth/UseCases/AuthenticateUserTest.php | 18 +++++------------ .../Unit/Auth/UseCases/CreateSessionTest.php | 20 +++++-------------- .../tests/Unit/Auth/UseCases/LogoutTest.php | 3 ++- 6 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 backend/tests/Fakes/FakeClock.php create mode 100644 backend/tests/Fakes/FakeHasher.php create mode 100644 backend/tests/Fakes/FakeTokenGenerator.php diff --git a/backend/tests/Fakes/FakeClock.php b/backend/tests/Fakes/FakeClock.php new file mode 100644 index 0000000..f32d5a4 --- /dev/null +++ b/backend/tests/Fakes/FakeClock.php @@ -0,0 +1,15 @@ +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'); diff --git a/backend/tests/Unit/Auth/UseCases/CreateSessionTest.php b/backend/tests/Unit/Auth/UseCases/CreateSessionTest.php index 93c9f25..183ec36 100644 --- a/backend/tests/Unit/Auth/UseCases/CreateSessionTest.php +++ b/backend/tests/Unit/Auth/UseCases/CreateSessionTest.php @@ -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); } diff --git a/backend/tests/Unit/Auth/UseCases/LogoutTest.php b/backend/tests/Unit/Auth/UseCases/LogoutTest.php index 36c5f16..6697e81 100644 --- a/backend/tests/Unit/Auth/UseCases/LogoutTest.php +++ b/backend/tests/Unit/Auth/UseCases/LogoutTest.php @@ -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(),