<?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'));
|
}
|
}
|