Attainly/backend/tests/Feature/User/EloquentUserRepositoryTest.php
2026-07-31 11:34:50 +03:00

88 lines
2.6 KiB
PHP

<?php
namespace Tests\Feature\User;
use App\Shared\ValueObject\EmailAddress;
use App\User\CreateUserDto;
use App\User\UserRepository;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class EloquentUserRepositoryTest extends TestCase
{
use RefreshDatabase;
public function test_it_creates_and_finds_a_user(): void
{
$repository = app(UserRepository::class);
$user = $repository->create(new CreateUserDto(
email: new EmailAddress('Founder@EXAMPLE.COM'),
password: 'correct-password',
));
$this->assertGreaterThan(0, $user->getId());
$this->assertSame(
'Founder@example.com',
$user->getEmail()->value(),
);
$this->assertDatabaseHas('users', [
'id' => $user->getId(),
'email' => 'Founder@example.com',
]);
$this->assertDatabaseMissing('users', [
'id' => $user->getId(),
'password' => 'correct-password',
]);
$foundUser = $repository->find($user->getId());
$this->assertNotNull($foundUser);
$this->assertSame($user->getId(), $foundUser->getId());
$this->assertSame(
$user->getEmail()->value(),
$foundUser->getEmail()->value(),
);
}
public function test_it_returns_null_for_an_unknown_user(): void
{
$repository = app(UserRepository::class);
$this->assertNull($repository->find(999));
}
public function test_it_finds_a_user_with_matching_credentials(): void
{
$repository = app(UserRepository::class);
$createdUser = $repository->create(new CreateUserDto(
email: new EmailAddress('user@example.com'),
password: 'correct-password',
));
$foundUser = $repository->findByCredentials(
new EmailAddress('user@EXAMPLE.COM'),
'correct-password',
);
$this->assertNotNull($foundUser);
$this->assertSame($createdUser->getId(), $foundUser->getId());
}
public function test_it_rejects_non_matching_credentials(): void
{
$repository = app(UserRepository::class);
$repository->create(new CreateUserDto(
email: new EmailAddress('user@example.com'),
password: 'correct-password',
));
$this->assertNull($repository->findByCredentials(
new EmailAddress('user@example.com'),
'wrong-password',
));
$this->assertNull($repository->findByCredentials(
new EmailAddress('unknown@example.com'),
'correct-password',
));
}
}