Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
43 / 43 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | pest()->group('unit'); |
| 11 | |
| 12 | use AppDemo\User\Domain\Email; |
| 13 | use AppDemo\User\Domain\Exception\InvalidEmailException; |
| 14 | use Tests\Phexium\Fake\Domain\StringValueObject as FakeValueObject; |
| 15 | |
| 16 | describe('Creation', function (): void { |
| 17 | it('creates an email using the static factory method', function (): void { |
| 18 | $email = Email::fromString('test@example.com'); |
| 19 | |
| 20 | expect($email->getValue())->toBe('test@example.com') |
| 21 | ->and($email->jsonSerialize())->toBe('test@example.com') |
| 22 | ->and((string) $email)->toBe('test@example.com') |
| 23 | ; |
| 24 | }); |
| 25 | |
| 26 | it('normalizes by trimming whitespace', function (): void { |
| 27 | $email = Email::fromString(' test@example.com '); |
| 28 | |
| 29 | expect($email->getValue())->toBe('test@example.com'); |
| 30 | }); |
| 31 | }); |
| 32 | |
| 33 | describe('Equality', function (): void { |
| 34 | it('considers two emails with same value as equal', function (): void { |
| 35 | $email1 = Email::fromString('test@example.com'); |
| 36 | $email2 = Email::fromString('test@example.com '); |
| 37 | |
| 38 | expect($email1->equals($email2))->toBeTrue(); |
| 39 | }); |
| 40 | |
| 41 | it('considers two emails with different values as not equal', function (): void { |
| 42 | $email1 = Email::fromString('test@example.com'); |
| 43 | $email2 = Email::fromString('other@example.com'); |
| 44 | |
| 45 | expect($email1->equals($email2))->toBeFalse(); |
| 46 | }); |
| 47 | |
| 48 | it('considers an email and another value object as not equal', function (): void { |
| 49 | $email = Email::fromString('test@example.com'); |
| 50 | $object = FakeValueObject::fromString('test@example.com'); |
| 51 | |
| 52 | expect($email->equals($object))->toBeFalse(); |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | describe('Validation', function (): void { |
| 57 | it('throws exception for empty email', function (): void { |
| 58 | expect(fn (): Email => Email::fromString('')) |
| 59 | ->toThrow(InvalidEmailException::class, 'Email cannot be empty') |
| 60 | ; |
| 61 | }); |
| 62 | |
| 63 | it('throws exception for invalid email format', function (): void { |
| 64 | expect(fn (): Email => Email::fromString('invalid-email')) |
| 65 | ->toThrow(InvalidEmailException::class, 'Email must be a valid email address') |
| 66 | ; |
| 67 | }); |
| 68 | }); |