Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
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\Library\Domain\Author;
11use AppDemo\Library\Domain\Exception\InvalidAuthorException;
12use Tests\Phexium\Fake\Domain\StringValueObject as FakeValueObject;
13
14test('Can create author using static factory method', function (): void {
15    $author = Author::fromString('John Doe');
16
17    expect($author->getValue())->toBe('John Doe')
18        ->and($author->jsonSerialize())->toBe('John Doe')
19        ->and((string) $author)->toBe('John Doe')
20    ;
21});
22
23test('Normalize (trim whitespace) an author', function (): void {
24    $author = Author::fromString('  John Doe  ');
25
26    expect($author->getValue())->toBe('John Doe');
27});
28
29test('Two authors with same value are equal', function (): void {
30    $author1 = Author::fromString('John Doe');
31    $author2 = Author::fromString('John Doe ');
32
33    expect($author1->equals($author2))->toBeTrue();
34});
35
36test('Two authors with different values are not equal', function (): void {
37    $author1 = Author::fromString('John Doe');
38    $author2 = Author::fromString('Jane Doe');
39
40    expect($author1->equals($author2))->toBeFalse();
41});
42
43test('An author and another value object are not equal', function (): void {
44    $author = Author::fromString('John Doe');
45    $object = FakeValueObject::fromString('John Doe');
46
47    expect($author->equals($object))->toBeFalse();
48});
49
50it('throws exception for empty author', function (): void {
51    expect(fn (): Author => Author::fromString(''))
52        ->toThrow(InvalidAuthorException::class, 'Author cannot be empty')
53    ;
54});
55
56test('Accepts author with exactly 100 characters', function (): void {
57    $author = Author::fromString(str_repeat('a', 100));
58
59    expect(strlen($author->getValue()))->toBe(100);
60});
61
62it('throws exception for author exceeding 100 characters', function (): void {
63    $longAuthor = str_repeat('a', 101);
64    expect(fn (): Author => Author::fromString($longAuthor))
65        ->toThrow(InvalidAuthorException::class, 'Author cannot exceed 100 characters')
66    ;
67});