Attainly/backend/tests/Feature/User/EloquentUserRepositoryTest.php
2026-08-02 20:58:44 +03:00

81 lines
2.3 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'),
passwordHash: 'hashed-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->assertDatabaseHas('users', [
'id' => $user->getId(),
'passwordHash' => 'hashed-password',
]);
$foundUser = $repository->find($user->getId());
$this->assertNotNull($foundUser);
$this->assertSame($user->getId(), $foundUser->getId());
$this->assertSame(
$user->getEmail()->value(),
$foundUser->getEmail()->value(),
);
$this->assertSame(
$user->getPasswordHash(),
$foundUser->getPasswordHash(),
);
}
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_by_email(): void
{
$repository = app(UserRepository::class);
$createdUser = $repository->create(new CreateUserDto(
email: new EmailAddress('user@example.com'),
passwordHash: 'hashed-password',
));
$foundUser = $repository->findByEmail(
new EmailAddress('user@EXAMPLE.COM'),
);
$this->assertNotNull($foundUser);
$this->assertSame($createdUser->getId(), $foundUser->getId());
}
public function test_it_returns_null_for_an_unknown_email(): void
{
$repository = app(UserRepository::class);
$this->assertNull($repository->findByEmail(
new EmailAddress('unknown@example.com'),
));
}
}