78 lines
1.9 KiB
PHP
78 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Shared\Models\ValueObjects;
|
|
|
|
use DomainException;
|
|
use Shared\Models\ValueObjects\Email;
|
|
use stdClass;
|
|
|
|
covers(Email::class);
|
|
|
|
describe('email', function (): void {
|
|
it('creates a valid email', function (): void {
|
|
// GIVEN
|
|
$emailValue = ' Contact@Example.COM ';
|
|
|
|
// WHEN
|
|
$email = new Email($emailValue);
|
|
|
|
// THEN
|
|
expect($email)->toBeInstanceOf(Email::class)
|
|
->and($email->value())->toBe('contact@example.com');
|
|
});
|
|
|
|
it('rejects an empty email', function (): void {
|
|
// GIVEN
|
|
$emailValue = '';
|
|
|
|
// WHEN
|
|
$action = fn (): Email => new Email($emailValue);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(DomainException::class, 'Email cannot be empty.');
|
|
});
|
|
|
|
it('rejects invalid email formats', function (string $invalid): void {
|
|
// GIVEN & WHEN & THEN
|
|
expect(fn (): Email => new Email($invalid))->toThrow(DomainException::class);
|
|
})->with([
|
|
'',
|
|
' ',
|
|
'not-an-email',
|
|
'@example.com',
|
|
'contact@',
|
|
'contact@.com',
|
|
'contact@com',
|
|
str_repeat('a', 2048) . '@example.fr'
|
|
]);
|
|
|
|
it('considers two emails with the same value as equal', function (): void {
|
|
// GIVEN
|
|
$first = new Email('contact@example.fr');
|
|
$second = new Email('contact@example.fr');
|
|
|
|
// WHEN
|
|
$result = $first->equals($second);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
});
|
|
|
|
it('considers an email as not equal to', function (mixed $invalid): void {
|
|
// GIVEN
|
|
$email = new Email('contact-1@example.fr');
|
|
|
|
// WHEN
|
|
$result = $email->equals($invalid);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
|
|
// GIVEN
|
|
})->with([
|
|
'different email' => new Email('contact-2@example.fr'),
|
|
'non-email value' => new stdClass(),
|
|
]);
|
|
});
|