Jacek Kowalski
4 hours ago 6b9278a5d57091fce636dbb640b380cfb990db6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
declare(strict_types=1);
 
namespace App\Tests\Unit;
 
use App\Enum\LabelType;
use App\Model\Isbn;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
 
final class IsbnTest extends TestCase {
    /**
     * @return iterable<string, array{string, ?string}>
     */
    public static function values(): iterable {
        yield 'ISBN-10' => ['0306406152', '9780306406157'];
        yield 'ISBN-10 with hyphens' => ['0-306-40615-2', '9780306406157'];
        yield 'ISBN-10 ending in X' => ['080442957X', '9780804429573'];
        yield 'lower-case X' => ['080442957x', '9780804429573'];
        yield 'ISBN-13' => ['9788373843455', '9788373843455'];
        yield 'ISBN-13 with hyphens' => ['978-83-7384-345-5', '9788373843455'];
        yield '979 prefix' => ['9791034900107', '9791034900107'];
        yield 'bad ISBN-10 check digit' => ['0306406153', null];
        yield 'bad ISBN-13 check digit' => ['9788373843456', null];
        yield 'not a book prefix' => ['9770406063008', null];
        yield 'wrong length' => ['12345', null];
        yield 'text' => ['not a number', null];
    }
 
    #[DataProvider('values')]
    public function testConversion(string $value, ?string $expected): void {
        self::assertSame($expected, Isbn::toIsbn13($value));
    }
 
    public function testOnlyIsbn13PassesLabelValidation(): void {
        self::assertTrue(LabelType::ISBN->isValid('9780306406157'));
        self::assertFalse(LabelType::ISBN->isValid('0306406152'));
    }
}