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');