7 cases: rejects spaces, double-@, empty input; trims whitespace; lowercases domain only (preserving local-part case); equality by normalized value; __toString and getDomain. fails red - class App\\Shared\\ValueObject\\EmailAddress not yet defined.
61 lines
1.9 KiB
PHP
61 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Shared\ValueObject;
|
|
|
|
use App\Shared\ValueObject\EmailAddress;
|
|
use InvalidArgumentException;
|
|
use Tests\TestCase;
|
|
|
|
class EmailAddressTest extends TestCase
|
|
{
|
|
public function test_it_throws_on_invalid_spaces_in_email(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Invalid email address: inv alid @spa cing.co m');
|
|
|
|
new EmailAddress('inv alid @spa cing.co m');
|
|
}
|
|
|
|
public function test_it_throws_on_invalid_email_format(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Invalid email address: invalid-email@format@com');
|
|
|
|
new EmailAddress('invalid-email@format@com');
|
|
}
|
|
|
|
public function test_it_throws_on_empty_email(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Invalid email address: ');
|
|
|
|
new EmailAddress('');
|
|
}
|
|
|
|
public function test_it_trims_and_normalizes_domain_case(): void
|
|
{
|
|
$email = new EmailAddress(' tEsT@YaHoO.cOm ');
|
|
$this->assertSame('tEsT@yahoo.com', $email->value());
|
|
}
|
|
|
|
public function test_equality_is_by_normalized_value(): void
|
|
{
|
|
$first = new EmailAddress('test@TEST.COM');
|
|
$second = new EmailAddress('test@test.com');
|
|
|
|
$this->assertTrue($first->equals($second));
|
|
$this->assertTrue($second->equals($first));
|
|
}
|
|
|
|
public function test_to_string_returns_normalized_email(): void
|
|
{
|
|
$email = new EmailAddress('Test@GMAIL.COM ');
|
|
$this->assertSame('Test@gmail.com', $email->__toString());
|
|
}
|
|
|
|
public function test_get_domain_returns_lowercased_domain(): void
|
|
{
|
|
$email = new EmailAddress('User@SubDomain.Example.COM');
|
|
$this->assertSame('subdomain.example.com', $email->getDomain());
|
|
}
|
|
}
|