Part-DB-server/tests/Services/LabelSystem/BarcodeScanner/BarcodeScanResultHandlerTest.php

184 lines
7.1 KiB
PHP
Raw Normal View History

<?php
2022-11-29 21:21:26 +01:00
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-05-10 21:39:31 +02:00
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LabelSystem\BarcodeScanner;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Entity\Parts\StorageLocation;
use App\Services\LabelSystem\BarcodeScanner\BarcodeScanResultHandler;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use App\Entity\LabelSystem\LabelSupportedElement;
use App\Services\LabelSystem\BarcodeScanner\BarcodeSourceType;
use App\Services\LabelSystem\BarcodeScanner\LocalBarcodeScanResult;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use App\Services\LabelSystem\BarcodeScanner\EIGP114BarcodeScanResult;
use App\Services\LabelSystem\BarcodeScanner\LCSCBarcodeScanResult;
use App\Services\LabelSystem\BarcodeScanner\BarcodeScanResultInterface;
use InvalidArgumentException;
final class BarcodeScanResultHandlerTest extends KernelTestCase
{
private ?BarcodeScanResultHandler $service = null;
2020-05-10 21:39:31 +02:00
protected function setUp(): void
{
self::bootKernel();
$this->service = self::getContainer()->get(BarcodeScanResultHandler::class);
}
2024-06-22 00:31:43 +02:00
public static function urlDataProvider(): \Iterator
{
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART, 1, BarcodeSourceType::INTERNAL), '/en/part/1'];
2024-06-22 00:31:43 +02:00
//Part lot redirects to Part info page (Part lot 1 is associated with part 3)
2026-02-01 23:11:56 +01:00
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 1, BarcodeSourceType::INTERNAL), '/en/part/3?highlightLot=1'];
yield [new LocalBarcodeScanResult(LabelSupportedElement::STORELOCATION, 1, BarcodeSourceType::INTERNAL), '/en/store_location/1/parts'];
}
#[DataProvider('urlDataProvider')]
#[Group('DB')]
public function testGetRedirectURL(LocalBarcodeScanResult $scanResult, string $url): void
{
2026-02-22 01:38:41 +01:00
$this->assertSame($url, $this->service->getInfoURL($scanResult));
}
2026-02-22 19:35:59 +01:00
public function testGetRedirectEntityNotFound(): void
{
2026-02-22 19:35:59 +01:00
//If we encounter an invalid lot, we must get an null result
$url = $this->service->getInfoURL(new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT,
12_345_678, BarcodeSourceType::INTERNAL));
2026-02-22 19:35:59 +01:00
$this->assertNull($url);
}
public function testGetRedirectURLThrowsOnUnknownScanType(): void
{
$unknown = new class implements BarcodeScanResultInterface {
public function getDecodedForInfoMode(): array
{
return [];
}
};
$this->expectException(InvalidArgumentException::class);
2026-02-22 01:38:41 +01:00
$this->service->getInfoURL($unknown);
}
public function testEIGPBarcodeResolvePartOrNullReturnsNullWhenNotFound(): void
{
$scan = new EIGP114BarcodeScanResult([]);
2026-02-22 01:38:41 +01:00
$this->assertNull($this->service->resolvePart($scan));
2026-02-22 19:35:59 +01:00
$this->assertNull($this->service->getInfoURL($scan));
}
public function testLCSCBarcodeResolvePartOrNullReturnsNullWhenNotFound(): void
{
$scan = new LCSCBarcodeScanResult(
fields: ['pc' => 'C0000000', 'pm' => ''],
2026-02-21 23:37:46 +01:00
rawInput: '{pc:C0000000,pm:}'
);
2026-02-22 01:38:41 +01:00
$this->assertNull($this->service->resolvePart($scan));
2026-02-22 19:35:59 +01:00
$this->assertNull($this->service->getInfoURL($scan));
}
public function testResolveEntityThrowsOnUnknownScanType(): void
{
$unknown = new class implements BarcodeScanResultInterface {
public function getDecodedForInfoMode(): array
{
return [];
}
};
$this->expectException(InvalidArgumentException::class);
$this->service->resolvePart($unknown);
}
public function testResolveEntity(): void
{
$scan = new LocalBarcodeScanResult(LabelSupportedElement::PART, 1, BarcodeSourceType::INTERNAL);
$part = $this->service->resolveEntity($scan);
$this->assertSame(1, $part->getId());
$this->assertInstanceOf(Part::class, $part);
$scan = new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 1, BarcodeSourceType::INTERNAL);
$entity = $this->service->resolveEntity($scan);
$this->assertSame(1, $entity->getId());
$this->assertInstanceOf(PartLot::class, $entity);
$scan = new LocalBarcodeScanResult(LabelSupportedElement::STORELOCATION, 1, BarcodeSourceType::INTERNAL);
$entity = $this->service->resolveEntity($scan);
$this->assertSame(1, $entity->getId());
2026-02-22 21:26:09 +01:00
$this->assertInstanceOf(StorageLocation::class, $entity);
}
public function testResolvePart(): void
{
$scan = new LocalBarcodeScanResult(LabelSupportedElement::PART, 1, BarcodeSourceType::INTERNAL);
$part = $this->service->resolvePart($scan);
$this->assertSame(1, $part->getId());
$scan = new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 1, BarcodeSourceType::INTERNAL);
$part = $this->service->resolvePart($scan);
$this->assertSame(3, $part->getId());
$scan = new LocalBarcodeScanResult(LabelSupportedElement::STORELOCATION, 1, BarcodeSourceType::INTERNAL);
$part = $this->service->resolvePart($scan);
$this->assertNull($part); //Store location does not resolve to a part
}
public function testGetCreateInfos(): void
{
$lcscScan = LCSCBarcodeScanResult::parse('{pbn:PB1,on:ON1,pc:C138033,pm:RC0402FR-071ML,qty:10}');
$infos = $this->service->getCreateInfos($lcscScan);
$this->assertSame('lcsc', $infos['providerKey']);
$this->assertSame('C138033', $infos['providerId']);
}
}