test user persistence
This commit is contained in:
parent
a8a6177a1d
commit
b5697e1e1f
3 changed files with 94 additions and 0 deletions
48
backend/tests/Feature/User/EloquentUserRepositoryTest.php
Normal file
48
backend/tests/Feature/User/EloquentUserRepositoryTest.php
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
<?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'),
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertGreaterThan(0, $user->getId());
|
||||||
|
$this->assertSame(
|
||||||
|
'Founder@example.com',
|
||||||
|
$user->getEmail()->value(),
|
||||||
|
);
|
||||||
|
$this->assertDatabaseHas('users', [
|
||||||
|
'id' => $user->getId(),
|
||||||
|
'email' => 'Founder@example.com',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$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));
|
||||||
|
}
|
||||||
|
}
|
||||||
27
backend/tests/Unit/Shared/ValueObject/EmailAddressTest.php
Normal file
27
backend/tests/Unit/Shared/ValueObject/EmailAddressTest.php
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Shared\ValueObject;
|
||||||
|
|
||||||
|
use App\Shared\ValueObject\EmailAddress;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class EmailAddressTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_it_rejects_an_invalid_email_address(): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage(
|
||||||
|
'Invalid email address: invalid-email',
|
||||||
|
);
|
||||||
|
|
||||||
|
new EmailAddress('invalid-email');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_it_trims_and_normalizes_the_domain(): void
|
||||||
|
{
|
||||||
|
$email = new EmailAddress(' Founder@EXAMPLE.COM ');
|
||||||
|
|
||||||
|
$this->assertSame('Founder@example.com', $email->value());
|
||||||
|
}
|
||||||
|
}
|
||||||
19
backend/tests/Unit/User/UserTest.php
Normal file
19
backend/tests/Unit/User/UserTest.php
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\User;
|
||||||
|
|
||||||
|
use App\Shared\ValueObject\EmailAddress;
|
||||||
|
use App\User\User;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class UserTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_it_exposes_its_identity_and_email(): void
|
||||||
|
{
|
||||||
|
$email = new EmailAddress('user@example.com');
|
||||||
|
$user = new User(id: 42, email: $email);
|
||||||
|
|
||||||
|
$this->assertSame(42, $user->getId());
|
||||||
|
$this->assertSame($email, $user->getEmail());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue