Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
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 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | use AppDemo\Library\Domain\Exception\InvalidTitleException; |
| 11 | use AppDemo\Library\Domain\Title; |
| 12 | use Tests\Phexium\Fake\Domain\StringValueObject as FakeValueObject; |
| 13 | |
| 14 | test('Can create title using static factory method', function (): void { |
| 15 | $title = Title::fromString('Clean Architecture'); |
| 16 | |
| 17 | expect($title->getValue())->toBe('Clean Architecture') |
| 18 | ->and($title->jsonSerialize())->toBe('Clean Architecture') |
| 19 | ->and((string) $title)->toBe('Clean Architecture') |
| 20 | ; |
| 21 | }); |
| 22 | |
| 23 | test('Normalize (trim whitespace) a title', function (): void { |
| 24 | $title = Title::fromString(' Clean Architecture '); |
| 25 | |
| 26 | expect($title->getValue())->toBe('Clean Architecture'); |
| 27 | }); |
| 28 | |
| 29 | test('Two titles with same value are equal', function (): void { |
| 30 | $title1 = Title::fromString('Clean Architecture'); |
| 31 | $title2 = Title::fromString('Clean Architecture '); |
| 32 | |
| 33 | expect($title1->equals($title2))->toBeTrue(); |
| 34 | }); |
| 35 | |
| 36 | test('Two titles with different values are not equal', function (): void { |
| 37 | $title1 = Title::fromString('Clean Architecture'); |
| 38 | $title2 = Title::fromString('Clean Code'); |
| 39 | |
| 40 | expect($title1->equals($title2))->toBeFalse(); |
| 41 | }); |
| 42 | |
| 43 | test('A title and another value object are not equal', function (): void { |
| 44 | $title = Title::fromString('Clean Code'); |
| 45 | $object = FakeValueObject::fromString('Clean Code'); |
| 46 | |
| 47 | expect($title->equals($object))->toBeFalse(); |
| 48 | }); |
| 49 | |
| 50 | it('throws exception for invalid title', function (): void { |
| 51 | expect(fn (): Title => Title::fromString('')) |
| 52 | ->toThrow(InvalidTitleException::class, 'Title cannot be empty') |
| 53 | ; |
| 54 | }); |
| 55 | |
| 56 | test('Accepts title with exactly 200 characters', function (): void { |
| 57 | $title = Title::fromString(str_repeat('a', 200)); |
| 58 | |
| 59 | expect(strlen($title->getValue()))->toBe(200); |
| 60 | }); |
| 61 | |
| 62 | it('throws exception for title exceeding 200 characters', function (): void { |
| 63 | $longTitle = str_repeat('a', 201); |
| 64 | expect(fn (): Title => Title::fromString($longTitle)) |
| 65 | ->toThrow(InvalidTitleException::class, 'Title cannot exceed 200 characters') |
| 66 | ; |
| 67 | }); |