test user login and create fake user repo

This commit is contained in:
Yisroel Baum 2025-11-01 23:19:09 +02:00
parent 7ece9dd072
commit 5566395dfb
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 84 additions and 0 deletions

View file

@ -0,0 +1,35 @@
<?php
namespace Tests\Fakes\User;
use FreightQuote\User\User;
use FreightQuote\User\UserRepository;
class FakeUserRepository implements UserRepository
{
/**
* @var User[] $existingUsers
*/
private array $existingUsers = [];
public function findByEmail(string $email): ?User
{
foreach ($this->existingUsers as $user) {
if ($user->getEmail() === $email) {
return new User(
$user->getEmail(),
$user->getPassword()
);
}
}
return null;
}
public function save(User $user): User
{
$this->existingUsers[$user->getEmail()] = $user;
return new User($user->getEmail(), $user->getPassword());
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace Tests\Unit\User\UseCases;
use PHPUnit\Framework\TestCase;
use FreightQuote\User\User;
use FreightQuote\User\UseCases\LoginUser;
use FreightQuote\User\UseCases\LoginUserRequest;
use Tests\Fakes\User\FakeUserRepository;
class LoginUserTest extends TestCase
{
public function test_login(): void
{
$email = 'test@test.com';
$password = password_hash('testPassword', PASSWORD_DEFAULT);
$user = new User($email, $password);
$userRepo = new FakeUserRepository();
$userRepo->save($user);
$dto = new LoginUserRequest($email, 'testPassword');
$useCase = new LoginUser($dto, $userRepo);
$response = $useCase->execute();
$this->assertTrue($response);
}
public function test_non_existing_user_login_fails(): void
{
$email = 'test@test.com';
$password = 'testPassword';
$dto = new LoginUserRequest($email, $password);
$userRepo = new FakeUserRepository();
$useCase = new LoginUser($dto, $userRepo);
$response = $useCase->execute();
$this->assertFalse($response);
}
public function test_wrong_password_fails(): void
{
$email = 'test@test.com';
$password = 'testPassword';
$user = new User($email, $password);
$userRepo = new FakeUserRepository();
$userRepo->save($user);
$dto = new LoginUserRequest($email, 'wrongPassword');
$useCase = new LoginUser($dto, $userRepo);
$response = $useCase->execute();
$this->assertFalse($response);
}
}