From 11f7298c062122902bccf45a0eb815e25337ca8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Fri, 3 Jan 2025 22:04:06 +0100 Subject: [PATCH] Added tests to EIGP114Barcode parser --- .../LabelSystem/Barcodes/EIGP114Barcode.php | 31 +++- .../Barcodes/EIGP114BarcodeTest.php | 132 ++++++++++++++++++ 2 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 tests/Services/LabelSystem/Barcodes/EIGP114BarcodeTest.php diff --git a/src/Services/LabelSystem/Barcodes/EIGP114Barcode.php b/src/Services/LabelSystem/Barcodes/EIGP114Barcode.php index 11902e9d..b4dced70 100644 --- a/src/Services/LabelSystem/Barcodes/EIGP114Barcode.php +++ b/src/Services/LabelSystem/Barcodes/EIGP114Barcode.php @@ -238,9 +238,9 @@ class EIGP114Barcode return 'mouser'; } - //According to this thread (https://github.com/inventree/InvenTree/issues/853), Newark codes contains a "3P" field + //According to this thread (https://github.com/inventree/InvenTree/issues/853), Newark/element14 codes contains a "3P" field if (isset($this->data['3P'])) { - return 'newark'; + return 'element14'; } return null; @@ -259,7 +259,7 @@ class EIGP114Barcode } //Code must end with - if(!str_ends_with($input, "\u{1D}\u{04}")){ + if(!str_ends_with($input, "\u{1E}\u{04}")){ return false; } @@ -282,11 +282,30 @@ class EIGP114Barcode $input = substr($input, 5, -2); //Split the input into the different fields (using the separator) - $fields = explode("\u{1D}", $input); + $parts = explode("\u{1D}", $input); //The first field is the format identifier, which we do not need - array_shift($fields); + array_shift($parts); - return new self($fields); + //Split the fields into key-value pairs + $results = []; + + foreach($parts as $part) { + //^ 0* ([1-9]? \d* [A-Z]) + //Start of the string Leading zeros are discarded Not a zero Any number of digits single uppercase Letter + // 00 1 4 K + + if(!preg_match('/^0*([1-9]?\d*[A-Z])/', $part, $matches)) { + throw new \LogicException("Could not parse field: $part"); + } + //Extract the key + $key = $matches[0]; + //Extract the field value + $fieldValue = substr($part, strlen($matches[0])); + + $results[$key] = $fieldValue; + } + + return new self($results); } } \ No newline at end of file diff --git a/tests/Services/LabelSystem/Barcodes/EIGP114BarcodeTest.php b/tests/Services/LabelSystem/Barcodes/EIGP114BarcodeTest.php new file mode 100644 index 00000000..c23ea06d --- /dev/null +++ b/tests/Services/LabelSystem/Barcodes/EIGP114BarcodeTest.php @@ -0,0 +1,132 @@ +. + */ + +namespace App\Tests\Services\LabelSystem\Barcodes; + +use App\Services\LabelSystem\Barcodes\EIGP114Barcode; +use PHPUnit\Framework\TestCase; + +class EIGP114BarcodeTest extends TestCase +{ + + public function testGuessBarcodeVendor(): void + { + //Generic barcode: + + $barcode = new EIGP114Barcode([ + 'P' => '596-777A1-ND', + '1P' => 'XAF4444', + 'Q' => '3', + '10D' => '1452', + '1T' => 'BF1103', + '4L' => 'US', + ]); + + $this->assertNull($barcode->guessBarcodeVendor()); + + //Digikey barcode: + $barcode = new EIGP114Barcode([ + 'P' => '596-777A1-ND', + '1P' => 'XAF4444', + 'Q' => '3', + '10D' => '1452', + '1T' => 'BF1103', + '4L' => 'US', + '13Z' => 'Digi-Key', + ]); + $this->assertEquals('digikey', $barcode->guessBarcodeVendor()); + + //Mouser barcode: + $barcode = new EIGP114Barcode([ + 'P' => '596-777A1-ND', + '1P' => 'XAF4444', + 'Q' => '3', + '10D' => '1452', + '1T' => 'BF1103', + '4L' => 'US', + '1V' => 'Mouser', + ]); + + $this->assertEquals('mouser', $barcode->guessBarcodeVendor()); + + //Farnell barcode: + $barcode = new EIGP114Barcode([ + 'P' => '596-777A1-ND', + '1P' => 'XAF4444', + 'Q' => '3', + '10D' => '1452', + '1T' => 'BF1103', + '4L' => 'US', + '3P' => 'Farnell', + ]); + + $this->assertEquals('element14', $barcode->guessBarcodeVendor()); + } + + public function testIsFormat06Code(): void + { + $this->assertFalse(EIGP114Barcode::isFormat06Code('')); + $this->assertFalse(EIGP114Barcode::isFormat06Code('test')); + $this->assertFalse(EIGP114Barcode::isFormat06Code('12232435ew4rf')); + //Missing trailer + $this->assertFalse(EIGP114Barcode::isFormat06Code("[)>\x1E06\x1DP596-777A1-ND\x1D1PXAF4444\x1DQ3\x1D10D1452\x1D1TBF1103\x1D4LUS")); + + //Valid code + $this->assertTrue(EIGP114Barcode::isFormat06Code("[)>\x1E06\x1DP596-777A1-ND\x1D1PXAF4444\x1DQ3\x1D10D1452\x1D1TBF1103\x1D4LUS\x1E\x04")); + } + + public function testParseFormat06CodeInvalid(): void + { + $this->expectException(\InvalidArgumentException::class); + EIGP114Barcode::parseFormat06Code(''); + } + + public function testParseFormat06Code(): void + { + $barcode = EIGP114Barcode::parseFormat06Code("[)>\x1E06\x1DP596-777A1-ND\x1D1PXAF4444\x1DQ3\x1D10D1452\x1D1TBF1103\x1D4LUS\x1E\x04"); + $this->assertEquals([ + 'P' => '596-777A1-ND', + '1P' => 'XAF4444', + 'Q' => '3', + '10D' => '1452', + '1T' => 'BF1103', + '4L' => 'US', + ], $barcode->data); + } + + public function testDataParsing(): void + { + $barcode = new EIGP114Barcode([ + 'P' => '596-777A1-ND', + '1P' => 'XAF4444', + 'Q' => '3', + '10D' => '1452', + '1T' => 'BF1103', + '4L' => 'US', + ]); + + $this->assertEquals('596-777A1-ND', $barcode->customerPartNumber); + $this->assertEquals('XAF4444', $barcode->supplierPartNumber); + $this->assertEquals(3, $barcode->quantity); + $this->assertEquals('1452', $barcode->alternativeDateCode); + $this->assertEquals('BF1103', $barcode->lotCode); + $this->assertEquals('US', $barcode->countryOfOrigin); + } +}