From a8520b7870c923263eb5441b6e8269249ab24bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 21 Feb 2026 23:37:46 +0100 Subject: [PATCH] Added test for LCSCBarcodeScanResult --- .../BarcodeScanner/LCSCBarcodeScanResult.php | 2 +- .../BarcodeScanner/BarcodeRedirectorTest.php | 42 +++------ .../LCSCBarcodeScanResultTest.php | 86 +++++++++++++++++++ 3 files changed, 99 insertions(+), 31 deletions(-) create mode 100644 tests/Services/LabelSystem/BarcodeScanner/LCSCBarcodeScanResultTest.php diff --git a/src/Services/LabelSystem/BarcodeScanner/LCSCBarcodeScanResult.php b/src/Services/LabelSystem/BarcodeScanner/LCSCBarcodeScanResult.php index 02f87b47..0151cffa 100644 --- a/src/Services/LabelSystem/BarcodeScanner/LCSCBarcodeScanResult.php +++ b/src/Services/LabelSystem/BarcodeScanner/LCSCBarcodeScanResult.php @@ -51,7 +51,7 @@ readonly class LCSCBarcodeScanResult implements BarcodeScanResultInterface */ public function __construct( public array $fields, - public string $raw_input, + public string $rawInput, ) { $this->pickBatchNumber = $this->fields['pbn'] ?? null; diff --git a/tests/Services/LabelSystem/BarcodeScanner/BarcodeRedirectorTest.php b/tests/Services/LabelSystem/BarcodeScanner/BarcodeRedirectorTest.php index f13604d3..8a2810ba 100644 --- a/tests/Services/LabelSystem/BarcodeScanner/BarcodeRedirectorTest.php +++ b/tests/Services/LabelSystem/BarcodeScanner/BarcodeRedirectorTest.php @@ -116,45 +116,27 @@ final class BarcodeRedirectorTest extends KernelTestCase $this->assertNull($this->service->resolvePartOrNull($scan)); } + public function testLCSCBarcodeResolvePartOrNullReturnsNullWhenNotFound(): void + { + $scan = new LCSCBarcodeScanResult( + fields: ['pc' => 'C0000000', 'pm' => ''], + rawInput: '{pc:C0000000,pm:}' + ); + + $this->assertNull($this->service->resolvePartOrNull($scan)); + } + + public function testLCSCBarcodeMissingPmThrowsEntityNotFound(): void { // pc present but no pm => getPartFromLCSC() will throw EntityNotFoundException // because it falls back to PM when PC doesn't match anything. $scan = new LCSCBarcodeScanResult( fields: ['pc' => 'C0000000', 'pm' => ''], // pm becomes null via getPM() - raw_input: '{pc:C0000000,pm:}' + rawInput: '{pc:C0000000,pm:}' ); $this->expectException(EntityNotFoundException::class); $this->service->getRedirectURL($scan); } - - public function testLCSCBarcodeResolvePartOrNullReturnsNullWhenNotFound(): void - { - $scan = new LCSCBarcodeScanResult( - fields: ['pc' => 'C0000000', 'pm' => ''], - raw_input: '{pc:C0000000,pm:}' - ); - - $this->assertNull($this->service->resolvePartOrNull($scan)); - } - - public function testLCSCParseRejectsNonLCSCFormat(): void - { - $this->expectException(InvalidArgumentException::class); - LCSCBarcodeScanResult::parse('not-an-lcsc-barcode'); - } - - public function testLCSCParseExtractsFields(): void - { - $scan = LCSCBarcodeScanResult::parse('{pbn:PB1,on:ON1,pc:C138033,pm:RC0402FR-071ML,qty:10}'); - - $this->assertSame('RC0402FR-071ML', $scan->mpn); - $this->assertSame('C138033', $scan->lcscCode); - - $decoded = $scan->getDecodedForInfoMode(); - $this->assertSame('LCSC', $decoded['Barcode type']); - $this->assertSame('RC0402FR-071ML', $decoded['MPN (pm)']); - $this->assertSame('C138033', $decoded['LCSC code (pc)']); - } } diff --git a/tests/Services/LabelSystem/BarcodeScanner/LCSCBarcodeScanResultTest.php b/tests/Services/LabelSystem/BarcodeScanner/LCSCBarcodeScanResultTest.php new file mode 100644 index 00000000..2128f113 --- /dev/null +++ b/tests/Services/LabelSystem/BarcodeScanner/LCSCBarcodeScanResultTest.php @@ -0,0 +1,86 @@ +. + */ + +namespace App\Tests\Services\LabelSystem\BarcodeScanner; + +use App\Services\LabelSystem\BarcodeScanner\LCSCBarcodeScanResult; +use InvalidArgumentException; +use PHPUnit\Framework\TestCase; + +class LCSCBarcodeScanResultTest extends TestCase +{ + public function testIsLCSCBarcode(): void + { + $this->assertFalse(LCSCBarcodeScanResult::isLCSCBarcode('invalid')); + $this->assertFalse(LCSCBarcodeScanResult::isLCSCBarcode('LCSC-12345')); + $this->assertFalse(LCSCBarcodeScanResult::isLCSCBarcode('')); + + $this->assertTrue(LCSCBarcodeScanResult::isLCSCBarcode('{pbn:PB1,on:ON1,pc:C138033,pm:RC0402FR-071ML,qty:10}')); + $this->assertTrue(LCSCBarcodeScanResult::isLCSCBarcode('{pbn:PICK2506270148,on:GB2506270877,pc:C22437266,pm:IA0509S-2W,qty:3,mc:,cc:1,pdi:164234874,hp:null,wc:ZH}')); + } + + public function testConstruct(): void + { + $raw = '{pbn:PB1,on:ON1,pc:C138033,pm:RC0402FR-071ML,qty:10}'; + $fields = ['pbn' => 'PB1', 'on' => 'ON1', 'pc' => 'C138033', 'pm' => 'RC0402FR-071ML', 'qty' => '10']; + $scan = new LCSCBarcodeScanResult($fields, $raw); + //Splitting up should work and assign the correct values to the properties: + $this->assertSame('RC0402FR-071ML', $scan->mpn); + $this->assertSame('C138033', $scan->lcscCode); + + //Fields and raw input should be preserved + $this->assertSame($fields, $scan->fields); + $this->assertSame($raw, $scan->rawInput); + } + + public function testLCSCParseInvalidFormatThrows(): void + { + $this->expectException(InvalidArgumentException::class); + LCSCBarcodeScanResult::parse('not-an-lcsc-barcode'); + } + + public function testParse(): void + { + $scan = LCSCBarcodeScanResult::parse('{pbn:PICK2506270148,on:GB2506270877,pc:C22437266,pm:IA0509S-2W,qty:3,mc:,cc:1,pdi:164234874,hp:null,wc:ZH}'); + + $this->assertSame('IA0509S-2W', $scan->mpn); + $this->assertSame('C22437266', $scan->lcscCode); + $this->assertSame('PICK2506270148', $scan->pickBatchNumber); + $this->assertSame('GB2506270877', $scan->orderNumber); + $this->assertSame(3, $scan->quantity); + $this->assertSame('1', $scan->countryChannel); + $this->assertSame('164234874', $scan->pdi); + $this->assertSame('null', $scan->hp); + $this->assertSame('ZH', $scan->warehouseCode); + } + + public function testLCSCParseExtractsFields(): void + { + $scan = LCSCBarcodeScanResult::parse('{pbn:PB1,on:ON1,pc:C138033,pm:RC0402FR-071ML,qty:10}'); + + $this->assertSame('RC0402FR-071ML', $scan->mpn); + $this->assertSame('C138033', $scan->lcscCode); + + $decoded = $scan->getDecodedForInfoMode(); + $this->assertSame('LCSC', $decoded['Barcode type']); + $this->assertSame('RC0402FR-071ML', $decoded['MPN (pm)']); + $this->assertSame('C138033', $decoded['LCSC code (pc)']); + } +}