Skip to content

Commit 5e76e1c

Browse files
committed
Add failint test for invalid ITF14 checksum
1 parent acaf80f commit 5e76e1c

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

tests/ChecksumBarcodeTest.php

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

33
use PHPUnit\Framework\TestCase;
4+
use Picqer\Barcode\Exceptions\BarcodeException;
5+
use Picqer\Barcode\Exceptions\InvalidCheckDigitException;
46
use Picqer\Barcode\Types\TypeEan13;
57
use Picqer\Barcode\Types\TypeInterface;
68
use Picqer\Barcode\Types\TypeITF14;
79

810
class ChecksumBarcodeTest extends TestCase
911
{
10-
public static $supportedBarcodes = [
12+
private const VALID_BARCODES = [
1113
['type' => TypeEan13::class, 'barcodes' => [
1214
'081231723897' => '0812317238973',
1315
'004900000463' => '0049000004632',
@@ -17,14 +19,36 @@ class ChecksumBarcodeTest extends TestCase
1719
'0540014128876' => '05400141288766',
1820
]],
1921
];
22+
private const INVALID_BARCODES = [
23+
['type' => TypeEan13::class, 'barcodes' => ['0812317238972', '0049000004633']],
24+
['type' => TypeITF14::class, 'barcodes' => ['00012345600016', '05400141288762']],
25+
];
2026

21-
public function testAllSupportedBarcodeTypes()
27+
public function testAllValidBarcodeTypes()
2228
{
23-
foreach ($this::$supportedBarcodes as $barcodeTestSet) {
29+
foreach (self::VALID_BARCODES as $barcodeTestSet) {
2430
$barcodeType = $this->getBarcodeType($barcodeTestSet['type']);
2531

26-
foreach ($barcodeTestSet['barcodes'] as $testBarcode => $expectedBarcode) {
27-
$this->assertEquals($expectedBarcode, $barcodeType->getBarcodeData($testBarcode)->getBarcode());
32+
foreach ($barcodeTestSet['barcodes'] as $testBarcode => $validBarcode) {
33+
$this->assertEquals($validBarcode, $barcodeType->getBarcodeData($testBarcode)->getBarcode());
34+
}
35+
}
36+
}
37+
38+
public function testAllInvalidBarcodeTypes()
39+
{
40+
foreach (self::INVALID_BARCODES as $barcodeTestSet) {
41+
$barcodeType = $this->getBarcodeType($barcodeTestSet['type']);
42+
43+
foreach ($barcodeTestSet['barcodes'] as $invalidBarcode) {
44+
try {
45+
$barcodeType->getBarcodeData($invalidBarcode)->getBarcode();
46+
} catch (BarcodeException $exception) {
47+
$this->assertInstanceOf(InvalidCheckDigitException::class, $exception);
48+
continue;
49+
}
50+
51+
$this->assertTrue(false, sprintf('Exception was not thrown for barcode "%s".', $invalidBarcode));
2852
}
2953
}
3054
}

0 commit comments

Comments
 (0)