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\InvalidIsbnException; |
| 11 | use AppDemo\Library\Domain\ISBN; |
| 12 | use Tests\Phexium\Fake\Domain\StringValueObject as FakeValueObject; |
| 13 | |
| 14 | test('Can create an ISBN-13 using static factory method', function (): void { |
| 15 | $isbn = ISBN::fromString('9780134494166'); |
| 16 | |
| 17 | expect($isbn->getValue())->toBe('9780134494166') |
| 18 | ->and($isbn->jsonSerialize())->toBe('9780134494166') |
| 19 | ->and($isbn->getFormattedValue())->toBe('978-013-449416-6') |
| 20 | ->and((string) $isbn)->toBe('978-013-449416-6') |
| 21 | ; |
| 22 | }); |
| 23 | |
| 24 | test('Normalize (remove non digits) an ISBN-13', function (): void { |
| 25 | $isbn = ISBN::fromString('978-0-13-449416-6'); |
| 26 | |
| 27 | expect($isbn->getValue())->toBe('9780134494166'); |
| 28 | }); |
| 29 | |
| 30 | test('Valid ISBN-13 with checksum digit 10 becomes 0', function (): void { |
| 31 | $isbn = ISBN::fromString('9783161484100'); |
| 32 | |
| 33 | expect($isbn->getValue())->toBe('9783161484100'); |
| 34 | }); |
| 35 | |
| 36 | test('Two ISBNs with same value are equal', function (): void { |
| 37 | $isbn1 = ISBN::fromString('9780134494166'); |
| 38 | $isbn2 = ISBN::fromString('978-0-13-449416-6'); |
| 39 | |
| 40 | expect($isbn1->equals($isbn2))->toBeTrue(); |
| 41 | }); |
| 42 | |
| 43 | test('Two ISBNs with different values are not equal', function (): void { |
| 44 | $isbn1 = ISBN::fromString('9780134494166'); |
| 45 | $isbn2 = ISBN::fromString('9781737519799'); |
| 46 | |
| 47 | expect($isbn1->equals($isbn2))->toBeFalse(); |
| 48 | }); |
| 49 | |
| 50 | test('An ISBN and another value object are not equal', function (): void { |
| 51 | $isbn = ISBN::fromString('9780134494166'); |
| 52 | $object = FakeValueObject::fromString('9780134494166'); |
| 53 | |
| 54 | expect($isbn->equals($object))->toBeFalse(); |
| 55 | }); |
| 56 | |
| 57 | it('throws exception for empty ISBN', function (): void { |
| 58 | expect(fn (): ISBN => ISBN::fromString('')) |
| 59 | ->toThrow(InvalidIsbnException::class, 'ISBN cannot be empty') |
| 60 | ; |
| 61 | }); |
| 62 | |
| 63 | it('throws exception for invalid ISBN-13', function (): void { |
| 64 | expect(fn (): ISBN => ISBN::fromString('1234567890123')) |
| 65 | ->toThrow(InvalidIsbnException::class, 'Invalid ISBN-13 checksum') |
| 66 | ; |
| 67 | }); |