Skip to content

Commit

Permalink
Add failint test for invalid ITF14 checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
vitkutny committed Jul 17, 2024
1 parent acaf80f commit be7f689
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions tests/ChecksumBarcodeTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

use PHPUnit\Framework\TestCase;
use Picqer\Barcode\Exceptions\BarcodeException;
use Picqer\Barcode\Exceptions\InvalidCheckDigitException;
use Picqer\Barcode\Types\TypeEan13;
use Picqer\Barcode\Types\TypeInterface;
use Picqer\Barcode\Types\TypeITF14;

class ChecksumBarcodeTest extends TestCase
{
public static $supportedBarcodes = [
private const VALID_BARCODES = [
['type' => TypeEan13::class, 'barcodes' => [
'081231723897' => '0812317238973',
'004900000463' => '0049000004632',
Expand All @@ -17,14 +19,36 @@ class ChecksumBarcodeTest extends TestCase
'0540014128876' => '05400141288766',
]],
];
private const INVALID_BARCODES = [
['type' => TypeEan13::class, 'barcodes' => ['0812317238972', '0049000004633']],
['type' => TypeITF14::class, 'barcodes' => ['00012345600016', '05400141288762']],
];

public function testAllSupportedBarcodeTypes()
public function testAllValidBarcodeTypes()
{
foreach ($this::$supportedBarcodes as $barcodeTestSet) {
foreach (self::VALID_BARCODES as $barcodeTestSet) {
$barcodeType = $this->getBarcodeType($barcodeTestSet['type']);

foreach ($barcodeTestSet['barcodes'] as $testBarcode => $expectedBarcode) {
$this->assertEquals($expectedBarcode, $barcodeType->getBarcodeData($testBarcode)->getBarcode());
foreach ($barcodeTestSet['barcodes'] as $testBarcode => $validBarcode) {
$this->assertEquals($validBarcode, $barcodeType->getBarcodeData($testBarcode)->getBarcode());
}
}
}

public function testAllInvalidBarcodeTypes()
{
foreach (self::INVALID_BARCODES as $barcodeTestSet) {
$barcodeType = $this->getBarcodeType($barcodeTestSet['type']);

foreach ($barcodeTestSet['barcodes'] as $invalidBarcode) {
try {
$barcodeType->getBarcodeData($invalidBarcode)->getBarcode();
} catch (BarcodeException $exception) {
$this->assertInstanceOf(InvalidCheckDigitException::class, $exception);
continue;
}

$this->assertTrue(false, 'Barcode exception was not thrown.');
}
}
}
Expand Down

0 comments on commit be7f689

Please sign in to comment.