Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
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
8declare(strict_types=1);
9
10pest()->group('unit');
11
12use Phexium\Plugin\Mailer\EmailAddress;
13use Phexium\Plugin\Mailer\InvalidEmailAddressException;
14
15describe('Creation', function (): void {
16    it('creates with email only', function (): void {
17        $email = EmailAddress::withEmail('john@example.com');
18
19        expect($email->getAddress())->toBe('john@example.com');
20        expect($email->getName())->toBeNull();
21    });
22
23    it('creates with name via wither', function (): void {
24        $email = EmailAddress::withEmail('john@example.com')->withName('John Doe');
25
26        expect($email->getAddress())->toBe('john@example.com');
27        expect($email->getName())->toBe('John Doe');
28    });
29
30    it('returns new instance from withName', function (): void {
31        $original = EmailAddress::withEmail('john@example.com');
32        $withName = $original->withName('John Doe');
33
34        expect($original->getName())->toBeNull();
35        expect($withName->getName())->toBe('John Doe');
36    });
37});
38
39describe('Formatting', function (): void {
40    it('formats toString with name', function (): void {
41        $email = EmailAddress::withEmail('john@example.com')->withName('John Doe');
42
43        expect($email->toString())->toBe('John Doe <john@example.com>');
44        expect((string) $email)->toBe('John Doe <john@example.com>');
45    });
46
47    it('formats toString without name', function (): void {
48        $email = EmailAddress::withEmail('john@example.com');
49
50        expect($email->toString())->toBe('john@example.com');
51        expect((string) $email)->toBe('john@example.com');
52    });
53});
54
55describe('Validation', function (): void {
56    it('rejects empty string', function (): void {
57        expect(fn (): EmailAddress => EmailAddress::withEmail(''))
58            ->toThrow(InvalidEmailAddressException::class)
59        ;
60    });
61
62    it('rejects invalid email', function (): void {
63        expect(fn (): EmailAddress => EmailAddress::withEmail('not-an-email'))
64            ->toThrow(InvalidEmailAddressException::class)
65        ;
66    });
67});