test EmailAddress value object
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.
This commit is contained in:
parent
fb5f8d4f02
commit
f862348a31
1 changed files with 61 additions and 0 deletions
61
backend/tests/Unit/Shared/ValueObject/EmailAddressTest.php
Normal file
61
backend/tests/Unit/Shared/ValueObject/EmailAddressTest.php
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
<?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());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue