Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
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
8declare(strict_types=1);
9
10use AppDemo\User\Domain\Email;
11use AppDemo\User\Domain\Exception\InvalidEmailException;
12use Tests\Phexium\Fake\Domain\StringValueObject as FakeValueObject;
13
14test('Can create email using static factory method', function (): void {
15    $email = Email::fromString('test@example.com');
16
17    expect($email->getValue())->toBe('test@example.com')
18        ->and($email->jsonSerialize())->toBe('test@example.com')
19        ->and((string) $email)->toBe('test@example.com')
20    ;
21});
22
23test('Normalize (trim whitespace) an email', function (): void {
24    $email = Email::fromString('  test@example.com  ');
25
26    expect($email->getValue())->toBe('test@example.com');
27});
28
29test('Two emails with same value are equal', function (): void {
30    $email1 = Email::fromString('test@example.com');
31    $email2 = Email::fromString('test@example.com ');
32
33    expect($email1->equals($email2))->toBeTrue();
34});
35
36test('Two emails with different values are not equal', function (): void {
37    $email1 = Email::fromString('test@example.com');
38    $email2 = Email::fromString('other@example.com');
39
40    expect($email1->equals($email2))->toBeFalse();
41});
42
43test('An email and another value object are not equal', function (): void {
44    $email = Email::fromString('test@example.com');
45    $object = FakeValueObject::fromString('test@example.com');
46
47    expect($email->equals($object))->toBeFalse();
48});
49
50it('throws exception for empty email', function (): void {
51    expect(fn (): Email => Email::fromString(''))
52        ->toThrow(InvalidEmailException::class, 'Email cannot be empty')
53    ;
54});
55
56it('throws exception for invalid email format', function (): void {
57    expect(fn (): Email => Email::fromString('invalid-email'))
58        ->toThrow(InvalidEmailException::class, 'Email must be a valid email address')
59    ;
60});