test user login and create fake user repo
This commit is contained in:
parent
7ece9dd072
commit
5566395dfb
2 changed files with 84 additions and 0 deletions
35
tests/Fakes/User/FakeUserRepository.php
Normal file
35
tests/Fakes/User/FakeUserRepository.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
49
tests/Unit/User/UseCases/LoginUserTest.php
Normal file
49
tests/Unit/User/UseCases/LoginUserTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue