Merge branch 'master' into bulk-edit-tags

This commit is contained in:
d-buchmann 2025-10-07 09:07:30 +02:00 committed by GitHub
commit 7df3e8e634
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
421 changed files with 39491 additions and 16789 deletions

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Tests\Services\Attachments;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Services\Formatters\AmountFormatter;
use App\Services\Attachments\AttachmentPathResolver;
use const DIRECTORY_SEPARATOR;
@ -69,19 +70,19 @@ class AttachmentPathResolverTest extends WebTestCase
$this->assertNull($this->service->parameterToAbsolutePath('/./this/one/too'));
}
public function placeholderDataProvider(): \Iterator
public static function placeholderDataProvider(): \Iterator
{
//We need to do initialization (again), as dataprovider is called before setUp()
self::bootKernel();
$this->projectDir_orig = realpath(self::$kernel->getProjectDir());
$this->projectDir = str_replace('\\', '/', $this->projectDir_orig);
$this->media_path = $this->projectDir.'/public/media';
$this->footprint_path = $this->projectDir.'/public/img/footprints';
yield ['%FOOTPRINTS%/test/test.jpg', $this->footprint_path.'/test/test.jpg'];
yield ['%FOOTPRINTS%/test/', $this->footprint_path.'/test/'];
yield ['%MEDIA%/test', $this->media_path.'/test'];
yield ['%MEDIA%', $this->media_path];
yield ['%FOOTPRINTS%', $this->footprint_path];
$projectDir_orig = realpath(self::$kernel->getProjectDir());
$projectDir = str_replace('\\', '/', $projectDir_orig);
$media_path = $projectDir.'/public/media';
$footprint_path = $projectDir.'/public/img/footprints';
yield ['%FOOTPRINTS%/test/test.jpg', $footprint_path.'/test/test.jpg'];
yield ['%FOOTPRINTS%/test/', $footprint_path.'/test/'];
yield ['%MEDIA%/test', $media_path.'/test'];
yield ['%MEDIA%', $media_path];
yield ['%FOOTPRINTS%', $footprint_path];
//Footprints 3D are disabled
yield ['%FOOTPRINTS_3D%', null];
//Check that invalid pathes return null
@ -98,65 +99,59 @@ class AttachmentPathResolverTest extends WebTestCase
yield ['%FOOTPRINTS%/0\..\test', null];
}
public function realPathDataProvider(): \Iterator
public static function realPathDataProvider(): \Iterator
{
//We need to do initialization (again), as dataprovider is called before setUp()
self::bootKernel();
$this->projectDir_orig = realpath(self::$kernel->getProjectDir());
$this->projectDir = str_replace('\\', '/', $this->projectDir_orig);
$this->media_path = $this->projectDir.'/public/media';
$this->footprint_path = $this->projectDir.'/public/img/footprints';
yield [$this->media_path.'/test/img.jpg', '%MEDIA%/test/img.jpg'];
yield [$this->media_path.'/test/img.jpg', '%BASE%/data/media/test/img.jpg', true];
yield [$this->footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg'];
yield [$this->footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg', true];
$projectDir_orig = realpath(self::$kernel->getProjectDir());
$projectDir = str_replace('\\', '/', $projectDir_orig);
$media_path = $projectDir.'/public/media';
$footprint_path = $projectDir.'/public/img/footprints';
yield [$media_path.'/test/img.jpg', '%MEDIA%/test/img.jpg'];
yield [$media_path.'/test/img.jpg', '%BASE%/data/media/test/img.jpg', true];
yield [$footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg'];
yield [$footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg', true];
//Every kind of absolute path, that is not based with our placeholder dirs must be invald
yield ['/etc/passwd', null];
yield ['C:\\not\\existing.txt', null];
//More than one placeholder is not allowed
yield [$this->footprint_path.'/test/'.$this->footprint_path, null];
yield [$footprint_path.'/test/'.$footprint_path, null];
//Path must begin with path
yield ['/not/root'.$this->footprint_path, null];
yield ['/not/root'.$footprint_path, null];
}
/**
* @dataProvider placeholderDataProvider
*/
#[DataProvider('placeholderDataProvider')]
public function testPlaceholderToRealPath($param, $expected): void
{
$this->assertSame($expected, $this->service->placeholderToRealPath($param));
}
/**
* @dataProvider realPathDataProvider
*/
#[DataProvider('realPathDataProvider')]
public function testRealPathToPlaceholder($param, $expected, $old_method = false): void
{
$this->assertSame($expected, $this->service->realPathToPlaceholder($param, $old_method));
}
public function germanFootprintPathdDataProvider(): ?\Generator
public static function germanFootprintPathdDataProvider(): ?\Generator
{
self::bootKernel();
$this->projectDir_orig = realpath(self::$kernel->getProjectDir());
$this->projectDir = str_replace('\\', '/', $this->projectDir_orig);
$this->footprint_path = $this->projectDir.'/public/img/footprints';
$projectDir_orig = realpath(self::$kernel->getProjectDir());
$projectDir = str_replace('\\', '/', $projectDir_orig);
$footprint_path = $projectDir.'/public/img/footprints';
yield [$this->footprint_path. '/Active/Diodes/THT/DIODE_P600.png', '%FOOTPRINTS%/Aktiv/Dioden/Bedrahtet/DIODE_P600.png'];
yield [$this->footprint_path . '/Passive/Resistors/THT/Carbon/RESISTOR-CARBON_0207.png', '%FOOTPRINTS%/Passiv/Widerstaende/Bedrahtet/Kohleschicht/WIDERSTAND-KOHLE_0207.png'];
yield [$this->footprint_path . '/Optics/LEDs/THT/LED-GREEN_3MM.png', '%FOOTPRINTS%/Optik/LEDs/Bedrahtet/LED-GRUEN_3MM.png'];
yield [$this->footprint_path . '/Passive/Capacitors/TrimmerCapacitors/TRIMMER_CAPACITOR-RED_TZ03F.png', '%FOOTPRINTS%/Passiv/Kondensatoren/Trimmkondensatoren/TRIMMKONDENSATOR-ROT_TZ03F.png'];
yield [$this->footprint_path . '/Active/ICs/TO/IC_TO126.png', '%FOOTPRINTS%/Aktiv/ICs/TO/IC_TO126.png'];
yield [$this->footprint_path . '/Electromechanics/Switches_Buttons/RotarySwitches/ROTARY_SWITCH_DIP10.png', '%FOOTPRINTS%/Elektromechanik/Schalter_Taster/Drehschalter/DREHSCHALTER_DIP10.png'];
yield [$this->footprint_path . '/Electromechanics/Connectors/DINConnectors/SOCKET_DIN_MAB_4.png', '%FOOTPRINTS%/Elektromechanik/Verbinder/Rundsteckverbinder/BUCHSE_DIN_MAB_4.png'];
yield [$footprint_path. '/Active/Diodes/THT/DIODE_P600.png', '%FOOTPRINTS%/Aktiv/Dioden/Bedrahtet/DIODE_P600.png'];
yield [$footprint_path . '/Passive/Resistors/THT/Carbon/RESISTOR-CARBON_0207.png', '%FOOTPRINTS%/Passiv/Widerstaende/Bedrahtet/Kohleschicht/WIDERSTAND-KOHLE_0207.png'];
yield [$footprint_path . '/Optics/LEDs/THT/LED-GREEN_3MM.png', '%FOOTPRINTS%/Optik/LEDs/Bedrahtet/LED-GRUEN_3MM.png'];
yield [$footprint_path . '/Passive/Capacitors/TrimmerCapacitors/TRIMMER_CAPACITOR-RED_TZ03F.png', '%FOOTPRINTS%/Passiv/Kondensatoren/Trimmkondensatoren/TRIMMKONDENSATOR-ROT_TZ03F.png'];
yield [$footprint_path . '/Active/ICs/TO/IC_TO126.png', '%FOOTPRINTS%/Aktiv/ICs/TO/IC_TO126.png'];
yield [$footprint_path . '/Electromechanics/Switches_Buttons/RotarySwitches/ROTARY_SWITCH_DIP10.png', '%FOOTPRINTS%/Elektromechanik/Schalter_Taster/Drehschalter/DREHSCHALTER_DIP10.png'];
yield [$footprint_path . '/Electromechanics/Connectors/DINConnectors/SOCKET_DIN_MAB_4.png', '%FOOTPRINTS%/Elektromechanik/Verbinder/Rundsteckverbinder/BUCHSE_DIN_MAB_4.png'];
//Leave english pathes untouched
yield [$this->footprint_path . '/Passive/Capacitors/CAPACITOR_CTS_A_15MM.png', '%FOOTPRINTS%/Passive/Capacitors/CAPACITOR_CTS_A_15MM.png'];
yield [$footprint_path . '/Passive/Capacitors/CAPACITOR_CTS_A_15MM.png', '%FOOTPRINTS%/Passive/Capacitors/CAPACITOR_CTS_A_15MM.png'];
}
/**
* @dataProvider germanFootprintPathdDataProvider
*/
#[DataProvider('germanFootprintPathdDataProvider')]
public function testConversionOfGermanFootprintPaths(string $expected, string $input): void
{
$this->assertSame($expected, $this->service->placeholderToRealPath($input));

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Tests\Services\Attachments;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Services\Attachments\AttachmentURLGenerator;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -38,7 +39,7 @@ class AttachmentURLGeneratorTest extends WebTestCase
self::$service = self::getContainer()->get(AttachmentURLGenerator::class);
}
public function dataProvider(): \Iterator
public static function dataProvider(): \Iterator
{
yield ['/public/test.jpg', 'test.jpg'];
yield ['/public/folder/test.jpg', 'folder/test.jpg'];
@ -48,11 +49,11 @@ class AttachmentURLGeneratorTest extends WebTestCase
}
/**
* @dataProvider dataProvider
*
* @param $input
* @param $expected
*/
#[DataProvider('dataProvider')]
public function testTestabsolutePathToAssetPath($input, $expected): void
{
$this->assertSame($expected, static::$service->absolutePathToAssetPath($input, static::PUBLIC_DIR));

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Tests\Services\Attachments;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Services\Attachments\BuiltinAttachmentsFinder;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -43,7 +44,7 @@ class BuiltinAttachmentsFinderTest extends WebTestCase
self::$service = self::getContainer()->get(BuiltinAttachmentsFinder::class);
}
public function dataProvider(): \Iterator
public static function dataProvider(): \Iterator
{
//No value should return empty array
yield ['', [], []];
@ -54,9 +55,7 @@ class BuiltinAttachmentsFinderTest extends WebTestCase
yield ['.txt', [], ['%FOOTPRINTS_3D%/hallo.txt']];
}
/**
* @dataProvider dataProvider
*/
#[DataProvider('dataProvider')]
public function testFind($keyword, $options, $expected): void
{
$value = static::$service->find($keyword, $options, static::$mock_list);

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Tests\Services\Attachments;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Services\Attachments\FileTypeFilterTools;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -35,7 +36,7 @@ class FileTypeFilterToolsTest extends WebTestCase
self::$service = self::getContainer()->get(FileTypeFilterTools::class);
}
public function validateDataProvider(): \Iterator
public static function validateDataProvider(): \Iterator
{
yield ['', true];
//Empty string is valid
@ -54,7 +55,7 @@ class FileTypeFilterToolsTest extends WebTestCase
yield ['.png .jpg .gif', false];
}
public function normalizeDataProvider(): \Iterator
public static function normalizeDataProvider(): \Iterator
{
yield ['', ''];
yield ['.jpeg,.png,.gif', '.jpeg,.png,.gif'];
@ -68,7 +69,7 @@ class FileTypeFilterToolsTest extends WebTestCase
yield ['png, .gif, .png,', '.png,.gif'];
}
public function extensionAllowedDataProvider(): \Iterator
public static function extensionAllowedDataProvider(): \Iterator
{
yield ['', 'txt', true];
yield ['', 'everything_should_match', true];
@ -85,25 +86,20 @@ class FileTypeFilterToolsTest extends WebTestCase
/**
* Test the validateFilterString method.
*
* @dataProvider validateDataProvider
*/
#[DataProvider('validateDataProvider')]
public function testValidateFilterString(string $filter, bool $expected): void
{
$this->assertSame($expected, self::$service->validateFilterString($filter));
}
/**
* @dataProvider normalizeDataProvider
*/
#[DataProvider('normalizeDataProvider')]
public function testNormalizeFilterString(string $filter, string $expected): void
{
$this->assertSame($expected, self::$service->normalizeFilterString($filter));
}
/**
* @dataProvider extensionAllowedDataProvider
*/
#[DataProvider('extensionAllowedDataProvider')]
public function testIsExtensionAllowed(string $filter, string $extension, bool $expected): void
{
$this->assertSame($expected, self::$service->isExtensionAllowed($filter, $extension));

View file

@ -25,11 +25,12 @@ namespace App\Tests\Services;
use App\Entity\Attachments\PartAttachment;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\InfoProviderSystem\BulkInfoProviderImportJob;
use App\Entity\Parts\Category;
use App\Entity\Parts\Part;
use App\Exceptions\EntityNotSupportedException;
use App\Services\Formatters\AmountFormatter;
use App\Services\ElementTypeNameGenerator;
use App\Services\Formatters\AmountFormatter;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ElementTypeNameGeneratorTest extends WebTestCase
@ -50,16 +51,18 @@ class ElementTypeNameGeneratorTest extends WebTestCase
//We only test in english
$this->assertSame('Part', $this->service->getLocalizedTypeLabel(new Part()));
$this->assertSame('Category', $this->service->getLocalizedTypeLabel(new Category()));
$this->assertSame('Bulk info provider import', $this->service->getLocalizedTypeLabel(new BulkInfoProviderImportJob()));
//Test inheritance
$this->assertSame('Attachment', $this->service->getLocalizedTypeLabel(new PartAttachment()));
//Test for class name
$this->assertSame('Part', $this->service->getLocalizedTypeLabel(Part::class));
$this->assertSame('Bulk info provider import', $this->service->getLocalizedTypeLabel(BulkInfoProviderImportJob::class));
//Test exception for unknpwn type
$this->expectException(EntityNotSupportedException::class);
$this->service->getLocalizedTypeLabel(new class() extends AbstractDBElement {
$this->service->getLocalizedTypeLabel(new class () extends AbstractDBElement {
});
}
@ -74,7 +77,7 @@ class ElementTypeNameGeneratorTest extends WebTestCase
//Test exception
$this->expectException(EntityNotSupportedException::class);
$this->service->getTypeNameCombination(new class() extends AbstractNamedDBElement {
$this->service->getTypeNameCombination(new class () extends AbstractNamedDBElement {
public function getIDString(): string
{
return 'Stub';

View file

@ -178,7 +178,9 @@ class PartMergerTest extends KernelTestCase
//But the new lots, should be assigned to the target part and contain the same info
$clone3 = $merged->getPartLots()->get(2);
$clone4 = $merged->getPartLots()->get(3);
$this->assertInstanceOf(PartLot::class, $clone3);
$this->assertSame($merged, $clone3->getPart());
$this->assertInstanceOf(PartLot::class, $clone4);
$this->assertSame($merged, $clone4->getPart());
}

View file

@ -22,9 +22,12 @@ declare(strict_types=1);
*/
namespace App\Tests\Services\ImportExportSystem;
use App\Entity\Parts\Part;
use App\Entity\Parts\Supplier;
use App\Entity\ProjectSystem\Project;
use App\Entity\ProjectSystem\ProjectBOMEntry;
use App\Services\ImportExportSystem\BOMImporter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\File\File;
@ -36,11 +39,17 @@ class BOMImporterTest extends WebTestCase
*/
protected $service;
/**
* @var EntityManagerInterface
*/
protected $entityManager;
protected function setUp(): void
{
//Get a service instance.
self::bootKernel();
$this->service = self::getContainer()->get(BOMImporter::class);
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
}
public function testImportFileIntoProject(): void
@ -119,4 +128,489 @@ class BOMImporterTest extends WebTestCase
$this->service->stringToBOMEntries($input, ['type' => 'kicad_pcbnew']);
}
public function testDetectFields(): void
{
$input = <<<CSV
"Reference","Value","Footprint","Quantity","MPN","Manufacturer","LCSC SPN","Mouser SPN"
CSV;
$fields = $this->service->detectFields($input);
$this->assertIsArray($fields);
$this->assertCount(8, $fields);
$this->assertContains('Reference', $fields);
$this->assertContains('Value', $fields);
$this->assertContains('Footprint', $fields);
$this->assertContains('Quantity', $fields);
$this->assertContains('MPN', $fields);
$this->assertContains('Manufacturer', $fields);
$this->assertContains('LCSC SPN', $fields);
$this->assertContains('Mouser SPN', $fields);
}
public function testDetectFieldsWithQuotes(): void
{
$input = <<<CSV
"Reference","Value","Footprint","Quantity","MPN","Manufacturer","LCSC SPN","Mouser SPN"
CSV;
$fields = $this->service->detectFields($input);
$this->assertIsArray($fields);
$this->assertCount(8, $fields);
$this->assertEquals('Reference', $fields[0]);
$this->assertEquals('Value', $fields[1]);
}
public function testDetectFieldsWithSemicolon(): void
{
$input = <<<CSV
"Reference";"Value";"Footprint";"Quantity";"MPN";"Manufacturer";"LCSC SPN";"Mouser SPN"
CSV;
$fields = $this->service->detectFields($input, ';');
$this->assertIsArray($fields);
$this->assertCount(8, $fields);
$this->assertEquals('Reference', $fields[0]);
$this->assertEquals('Value', $fields[1]);
}
public function testGetAvailableFieldTargets(): void
{
$targets = $this->service->getAvailableFieldTargets();
$this->assertIsArray($targets);
$this->assertArrayHasKey('Designator', $targets);
$this->assertArrayHasKey('Quantity', $targets);
$this->assertArrayHasKey('Value', $targets);
$this->assertArrayHasKey('Package', $targets);
$this->assertArrayHasKey('MPN', $targets);
$this->assertArrayHasKey('Manufacturer', $targets);
$this->assertArrayHasKey('Part-DB ID', $targets);
$this->assertArrayHasKey('Comment', $targets);
// Check structure of a target
$this->assertArrayHasKey('label', $targets['Designator']);
$this->assertArrayHasKey('description', $targets['Designator']);
$this->assertArrayHasKey('required', $targets['Designator']);
$this->assertArrayHasKey('multiple', $targets['Designator']);
$this->assertTrue($targets['Designator']['required']);
$this->assertTrue($targets['Quantity']['required']);
$this->assertFalse($targets['Value']['required']);
}
public function testGetAvailableFieldTargetsWithSuppliers(): void
{
// Create test suppliers
$supplier1 = new Supplier();
$supplier1->setName('LCSC');
$supplier2 = new Supplier();
$supplier2->setName('Mouser');
$this->entityManager->persist($supplier1);
$this->entityManager->persist($supplier2);
$this->entityManager->flush();
$targets = $this->service->getAvailableFieldTargets();
$this->assertArrayHasKey('LCSC SPN', $targets);
$this->assertArrayHasKey('Mouser SPN', $targets);
$this->assertEquals('LCSC SPN', $targets['LCSC SPN']['label']);
$this->assertEquals('Mouser SPN', $targets['Mouser SPN']['label']);
$this->assertFalse($targets['LCSC SPN']['required']);
$this->assertTrue($targets['LCSC SPN']['multiple']);
// Clean up
$this->entityManager->remove($supplier1);
$this->entityManager->remove($supplier2);
$this->entityManager->flush();
}
public function testGetSuggestedFieldMapping(): void
{
$detected_fields = [
'Reference',
'Value',
'Footprint',
'Quantity',
'MPN',
'Manufacturer',
'LCSC',
'Mouser',
'Part-DB ID',
'Comment'
];
$suggestions = $this->service->getSuggestedFieldMapping($detected_fields);
$this->assertIsArray($suggestions);
$this->assertEquals('Designator', $suggestions['Reference']);
$this->assertEquals('Value', $suggestions['Value']);
$this->assertEquals('Package', $suggestions['Footprint']);
$this->assertEquals('Quantity', $suggestions['Quantity']);
$this->assertEquals('MPN', $suggestions['MPN']);
$this->assertEquals('Manufacturer', $suggestions['Manufacturer']);
$this->assertEquals('Part-DB ID', $suggestions['Part-DB ID']);
$this->assertEquals('Comment', $suggestions['Comment']);
}
public function testGetSuggestedFieldMappingWithSuppliers(): void
{
// Create test suppliers
$supplier1 = new Supplier();
$supplier1->setName('LCSC');
$supplier2 = new Supplier();
$supplier2->setName('Mouser');
$this->entityManager->persist($supplier1);
$this->entityManager->persist($supplier2);
$this->entityManager->flush();
$detected_fields = [
'Reference',
'LCSC',
'Mouser',
'lcsc_part',
'mouser_spn'
];
$suggestions = $this->service->getSuggestedFieldMapping($detected_fields);
$this->assertIsArray($suggestions);
$this->assertEquals('Designator', $suggestions['Reference']);
// Note: The exact mapping depends on the pattern matching logic
// We just check that supplier fields are mapped to something
$this->assertArrayHasKey('LCSC', $suggestions);
$this->assertArrayHasKey('Mouser', $suggestions);
$this->assertArrayHasKey('lcsc_part', $suggestions);
$this->assertArrayHasKey('mouser_spn', $suggestions);
// Clean up
$this->entityManager->remove($supplier1);
$this->entityManager->remove($supplier2);
$this->entityManager->flush();
}
public function testValidateFieldMappingValid(): void
{
$field_mapping = [
'Reference' => 'Designator',
'Quantity' => 'Quantity',
'Value' => 'Value'
];
$detected_fields = ['Reference', 'Quantity', 'Value', 'MPN'];
$result = $this->service->validateFieldMapping($field_mapping, $detected_fields);
$this->assertIsArray($result);
$this->assertArrayHasKey('errors', $result);
$this->assertArrayHasKey('warnings', $result);
$this->assertArrayHasKey('is_valid', $result);
$this->assertTrue($result['is_valid']);
$this->assertEmpty($result['errors']);
$this->assertNotEmpty($result['warnings']); // Should warn about unmapped MPN
}
public function testValidateFieldMappingMissingRequired(): void
{
$field_mapping = [
'Value' => 'Value',
'MPN' => 'MPN'
];
$detected_fields = ['Value', 'MPN'];
$result = $this->service->validateFieldMapping($field_mapping, $detected_fields);
$this->assertFalse($result['is_valid']);
$this->assertNotEmpty($result['errors']);
$this->assertContains("Required field 'Designator' is not mapped from any CSV column.", $result['errors']);
$this->assertContains("Required field 'Quantity' is not mapped from any CSV column.", $result['errors']);
}
public function testValidateFieldMappingInvalidTarget(): void
{
$field_mapping = [
'Reference' => 'Designator',
'Quantity' => 'Quantity',
'Value' => 'InvalidTarget'
];
$detected_fields = ['Reference', 'Quantity', 'Value'];
$result = $this->service->validateFieldMapping($field_mapping, $detected_fields);
$this->assertFalse($result['is_valid']);
$this->assertNotEmpty($result['errors']);
$this->assertContains("Invalid target field 'InvalidTarget' for CSV field 'Value'.", $result['errors']);
}
public function testStringToBOMEntriesKiCADSchematic(): void
{
$input = <<<CSV
"Reference","Value","Footprint","Quantity","MPN","Manufacturer","LCSC SPN","Mouser SPN"
"R1,R2","10k","R_0805_2012Metric",2,"CRCW080510K0FKEA","Vishay","C123456","123-M10K"
"C1","100nF","C_0805_2012Metric",1,"CL21A104KOCLRNC","Samsung","C789012","80-CL21A104KOCLRNC"
CSV;
$field_mapping = [
'Reference' => 'Designator',
'Value' => 'Value',
'Footprint' => 'Package',
'Quantity' => 'Quantity',
'MPN' => 'MPN',
'Manufacturer' => 'Manufacturer',
'LCSC SPN' => 'LCSC SPN',
'Mouser SPN' => 'Mouser SPN'
];
$bom_entries = $this->service->stringToBOMEntries($input, [
'type' => 'kicad_schematic',
'field_mapping' => $field_mapping,
'delimiter' => ','
]);
$this->assertContainsOnlyInstancesOf(ProjectBOMEntry::class, $bom_entries);
$this->assertCount(2, $bom_entries);
// Check first entry
$this->assertEquals('R1,R2', $bom_entries[0]->getMountnames());
$this->assertEquals(2.0, $bom_entries[0]->getQuantity());
$this->assertEquals('CRCW080510K0FKEA (R_0805_2012Metric)', $bom_entries[0]->getName());
$this->assertStringContainsString('Value: 10k', $bom_entries[0]->getComment());
$this->assertStringContainsString('MPN: CRCW080510K0FKEA', $bom_entries[0]->getComment());
$this->assertStringContainsString('Manf: Vishay', $bom_entries[0]->getComment());
// Check second entry
$this->assertEquals('C1', $bom_entries[1]->getMountnames());
$this->assertEquals(1.0, $bom_entries[1]->getQuantity());
}
public function testStringToBOMEntriesKiCADSchematicWithPriority(): void
{
$input = <<<CSV
"Reference","Value","MPN1","MPN2","Quantity"
"R1,R2","10k","CRCW080510K0FKEA","","2"
"C1","100nF","","CL21A104KOCLRNC","1"
CSV;
$field_mapping = [
'Reference' => 'Designator',
'Value' => 'Value',
'MPN1' => 'MPN',
'MPN2' => 'MPN',
'Quantity' => 'Quantity'
];
$field_priorities = [
'MPN1' => 1,
'MPN2' => 2
];
$bom_entries = $this->service->stringToBOMEntries($input, [
'type' => 'kicad_schematic',
'field_mapping' => $field_mapping,
'field_priorities' => $field_priorities,
'delimiter' => ','
]);
$this->assertContainsOnlyInstancesOf(ProjectBOMEntry::class, $bom_entries);
$this->assertCount(2, $bom_entries);
// First entry should use MPN1 (higher priority)
$this->assertEquals('CRCW080510K0FKEA', $bom_entries[0]->getName());
// Second entry should use MPN2 (MPN1 is empty)
$this->assertEquals('CL21A104KOCLRNC', $bom_entries[1]->getName());
}
public function testStringToBOMEntriesKiCADSchematicWithPartDBID(): void
{
// Create a test part with required fields
$part = new Part();
$part->setName('Test Part');
$part->setCategory($this->getDefaultCategory($this->entityManager));
$this->entityManager->persist($part);
$this->entityManager->flush();
$input = <<<CSV
"Reference","Value","Part-DB ID","Quantity"
"R1,R2","10k","{$part->getID()}","2"
CSV;
$field_mapping = [
'Reference' => 'Designator',
'Value' => 'Value',
'Part-DB ID' => 'Part-DB ID',
'Quantity' => 'Quantity'
];
$bom_entries = $this->service->stringToBOMEntries($input, [
'type' => 'kicad_schematic',
'field_mapping' => $field_mapping,
'delimiter' => ','
]);
$this->assertContainsOnlyInstancesOf(ProjectBOMEntry::class, $bom_entries);
$this->assertCount(1, $bom_entries);
$this->assertEquals('Test Part', $bom_entries[0]->getName());
$this->assertSame($part, $bom_entries[0]->getPart());
$this->assertStringContainsString("Part-DB ID: {$part->getID()}", $bom_entries[0]->getComment());
// Clean up
$this->entityManager->remove($part);
$this->entityManager->flush();
}
public function testStringToBOMEntriesKiCADSchematicWithInvalidPartDBID(): void
{
$input = <<<CSV
"Reference","Value","Part-DB ID","Quantity"
"R1,R2","10k","99999","2"
CSV;
$field_mapping = [
'Reference' => 'Designator',
'Value' => 'Value',
'Part-DB ID' => 'Part-DB ID',
'Quantity' => 'Quantity'
];
$bom_entries = $this->service->stringToBOMEntries($input, [
'type' => 'kicad_schematic',
'field_mapping' => $field_mapping,
'delimiter' => ','
]);
$this->assertContainsOnlyInstancesOf(ProjectBOMEntry::class, $bom_entries);
$this->assertCount(1, $bom_entries);
$this->assertEquals('10k', $bom_entries[0]->getName()); // Should use Value as name
$this->assertNull($bom_entries[0]->getPart()); // Should not link to part
$this->assertStringContainsString("Part-DB ID: 99999 (NOT FOUND)", $bom_entries[0]->getComment());
}
public function testStringToBOMEntriesKiCADSchematicMergeDuplicates(): void
{
$input = <<<CSV
"Reference","Value","MPN","Quantity"
"R1","10k","CRCW080510K0FKEA","1"
"R2","10k","CRCW080510K0FKEA","1"
CSV;
$field_mapping = [
'Reference' => 'Designator',
'Value' => 'Value',
'MPN' => 'MPN',
'Quantity' => 'Quantity'
];
$bom_entries = $this->service->stringToBOMEntries($input, [
'type' => 'kicad_schematic',
'field_mapping' => $field_mapping,
'delimiter' => ','
]);
$this->assertContainsOnlyInstancesOf(ProjectBOMEntry::class, $bom_entries);
$this->assertCount(1, $bom_entries); // Should merge into one entry
$this->assertEquals('R1,R2', $bom_entries[0]->getMountnames());
$this->assertEquals(2.0, $bom_entries[0]->getQuantity());
$this->assertEquals('CRCW080510K0FKEA', $bom_entries[0]->getName());
}
public function testStringToBOMEntriesKiCADSchematicMissingRequired(): void
{
$input = <<<CSV
"Value","MPN"
"10k","CRCW080510K0FKEA"
CSV;
$field_mapping = [
'Value' => 'Value',
'MPN' => 'MPN'
];
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('Required field "Designator" is missing or empty');
$this->service->stringToBOMEntries($input, [
'type' => 'kicad_schematic',
'field_mapping' => $field_mapping,
'delimiter' => ','
]);
}
public function testStringToBOMEntriesKiCADSchematicQuantityMismatch(): void
{
$input = <<<CSV
"Reference","Value","Quantity"
"R1,R2,R3","10k","2"
CSV;
$field_mapping = [
'Reference' => 'Designator',
'Value' => 'Value',
'Quantity' => 'Quantity'
];
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('Mismatch between quantity and component references');
$this->service->stringToBOMEntries($input, [
'type' => 'kicad_schematic',
'field_mapping' => $field_mapping,
'delimiter' => ','
]);
}
public function testStringToBOMEntriesKiCADSchematicWithBOM(): void
{
// Test with BOM (Byte Order Mark)
$input = "\xEF\xBB\xBF" . <<<CSV
"Reference","Value","Quantity"
"R1,R2","10k","2"
CSV;
$field_mapping = [
'Reference' => 'Designator',
'Value' => 'Value',
'Quantity' => 'Quantity'
];
$bom_entries = $this->service->stringToBOMEntries($input, [
'type' => 'kicad_schematic',
'field_mapping' => $field_mapping,
'delimiter' => ','
]);
$this->assertContainsOnlyInstancesOf(ProjectBOMEntry::class, $bom_entries);
$this->assertCount(1, $bom_entries);
$this->assertEquals('R1,R2', $bom_entries[0]->getMountnames());
}
private function getDefaultCategory(EntityManagerInterface $entityManager)
{
// Get the first available category or create a default one
$categoryRepo = $entityManager->getRepository(\App\Entity\Parts\Category::class);
$categories = $categoryRepo->findAll();
if (empty($categories)) {
// Create a default category if none exists
$category = new \App\Entity\Parts\Category();
$category->setName('Default Category');
$entityManager->persist($category);
$entityManager->flush();
return $category;
}
return $categories[0];
}
}

View file

@ -0,0 +1,349 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 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\ImportExportSystem;
use App\Entity\Parts\Part;
use App\Services\ImportExportSystem\BOMValidationService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @see \App\Services\ImportExportSystem\BOMValidationService
*/
class BOMValidationServiceTest extends WebTestCase
{
private BOMValidationService $validationService;
private EntityManagerInterface $entityManager;
private TranslatorInterface $translator;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->translator = self::getContainer()->get(TranslatorInterface::class);
$this->validationService = new BOMValidationService($this->entityManager, $this->translator);
}
public function testValidateBOMEntryWithValidData(): void
{
$entry = [
'Designator' => 'R1,C2,R3',
'Quantity' => '3',
'MPN' => 'RES-10K',
'Package' => '0603',
'Value' => '10k',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertTrue($result['is_valid']);
$this->assertEmpty($result['errors']);
$this->assertEquals(1, $result['line_number']);
}
public function testValidateBOMEntryWithMissingRequiredFields(): void
{
$entry = [
'MPN' => 'RES-10K',
'Package' => '0603',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertFalse($result['is_valid']);
$this->assertCount(2, $result['errors']);
$this->assertStringContainsString('Designator', (string) $result['errors'][0]);
$this->assertStringContainsString('Quantity', (string) $result['errors'][1]);
}
public function testValidateBOMEntryWithQuantityMismatch(): void
{
$entry = [
'Designator' => 'R1,C2,R3,C4',
'Quantity' => '3',
'MPN' => 'RES-10K',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertFalse($result['is_valid']);
$this->assertCount(1, $result['errors']);
$this->assertStringContainsString('Mismatch between quantity and component references', (string) $result['errors'][0]);
}
public function testValidateBOMEntryWithInvalidQuantity(): void
{
$entry = [
'Designator' => 'R1',
'Quantity' => 'abc',
'MPN' => 'RES-10K',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertFalse($result['is_valid']);
$this->assertGreaterThanOrEqual(1, count($result['errors']));
$this->assertStringContainsString('not a valid number', implode(' ', array_map('strval', $result['errors'])));
}
public function testValidateBOMEntryWithZeroQuantity(): void
{
$entry = [
'Designator' => 'R1',
'Quantity' => '0',
'MPN' => 'RES-10K',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertFalse($result['is_valid']);
$this->assertGreaterThanOrEqual(1, count($result['errors']));
$this->assertStringContainsString('must be greater than 0', implode(' ', array_map('strval', $result['errors'])));
}
public function testValidateBOMEntryWithDuplicateDesignators(): void
{
$entry = [
'Designator' => 'R1,R1,C2',
'Quantity' => '3',
'MPN' => 'RES-10K',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertFalse($result['is_valid']);
$this->assertCount(1, $result['errors']);
$this->assertStringContainsString('Duplicate component references', (string) $result['errors'][0]);
}
public function testValidateBOMEntryWithInvalidDesignatorFormat(): void
{
$entry = [
'Designator' => 'R1,invalid,C2',
'Quantity' => '3',
'MPN' => 'RES-10K',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertTrue($result['is_valid']); // Warnings don't make it invalid
$this->assertCount(1, $result['warnings']);
$this->assertStringContainsString('unusual format', (string) $result['warnings'][0]);
}
public function testValidateBOMEntryWithEmptyDesignator(): void
{
$entry = [
'Designator' => '',
'Quantity' => '1',
'MPN' => 'RES-10K',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertFalse($result['is_valid']);
$this->assertGreaterThanOrEqual(1, count($result['errors']));
$this->assertStringContainsString('Required field "Designator" is missing or empty', implode(' ', array_map('strval', $result['errors'])));
}
public function testValidateBOMEntryWithInvalidPartDBID(): void
{
$entry = [
'Designator' => 'R1',
'Quantity' => '1',
'MPN' => 'RES-10K',
'Part-DB ID' => 'abc',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertFalse($result['is_valid']);
$this->assertGreaterThanOrEqual(1, count($result['errors']));
$this->assertStringContainsString('not a valid number', implode(' ', array_map('strval', $result['errors'])));
}
public function testValidateBOMEntryWithNonExistentPartDBID(): void
{
$entry = [
'Designator' => 'R1',
'Quantity' => '1',
'MPN' => 'RES-10K',
'Part-DB ID' => '999999', // Use very high ID that doesn't exist
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertTrue($result['is_valid']); // Warnings don't make it invalid
$this->assertCount(1, $result['warnings']);
$this->assertStringContainsString('not found in database', (string) $result['warnings'][0]);
}
public function testValidateBOMEntryWithNoComponentName(): void
{
$entry = [
'Designator' => 'R1',
'Quantity' => '1',
'Package' => '0603',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertTrue($result['is_valid']); // Warnings don't make it invalid
$this->assertCount(1, $result['warnings']);
$this->assertStringContainsString('No component name/designation', (string) $result['warnings'][0]);
}
public function testValidateBOMEntryWithLongPackageName(): void
{
$entry = [
'Designator' => 'R1',
'Quantity' => '1',
'MPN' => 'RES-10K',
'Package' => str_repeat('A', 150), // Very long package name
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertTrue($result['is_valid']); // Warnings don't make it invalid
$this->assertCount(1, $result['warnings']);
$this->assertStringContainsString('unusually long', (string) $result['warnings'][0]);
}
public function testValidateBOMEntryWithLibraryPrefix(): void
{
$entry = [
'Designator' => 'R1',
'Quantity' => '1',
'MPN' => 'RES-10K',
'Package' => 'Resistor_SMD:R_0603_1608Metric',
];
$result = $this->validationService->validateBOMEntry($entry, 1);
$this->assertTrue($result['is_valid']);
$this->assertCount(1, $result['info']);
$this->assertStringContainsString('library prefix', $result['info'][0]);
}
public function testValidateBOMEntriesWithMultipleEntries(): void
{
$entries = [
[
'Designator' => 'R1',
'Quantity' => '1',
'MPN' => 'RES-10K',
],
[
'Designator' => 'C1,C2',
'Quantity' => '2',
'MPN' => 'CAP-100nF',
],
];
$result = $this->validationService->validateBOMEntries($entries);
$this->assertTrue($result['is_valid']);
$this->assertEquals(2, $result['total_entries']);
$this->assertEquals(2, $result['valid_entries']);
$this->assertEquals(0, $result['invalid_entries']);
$this->assertCount(2, $result['line_results']);
}
public function testValidateBOMEntriesWithMixedResults(): void
{
$entries = [
[
'Designator' => 'R1',
'Quantity' => '1',
'MPN' => 'RES-10K',
],
[
'Designator' => 'C1,C2',
'Quantity' => '1', // Mismatch
'MPN' => 'CAP-100nF',
],
];
$result = $this->validationService->validateBOMEntries($entries);
$this->assertFalse($result['is_valid']);
$this->assertEquals(2, $result['total_entries']);
$this->assertEquals(1, $result['valid_entries']);
$this->assertEquals(1, $result['invalid_entries']);
$this->assertCount(1, $result['errors']);
}
public function testGetValidationStats(): void
{
$validation_result = [
'total_entries' => 10,
'valid_entries' => 8,
'invalid_entries' => 2,
'errors' => ['Error 1', 'Error 2'],
'warnings' => ['Warning 1'],
'info' => ['Info 1', 'Info 2'],
];
$stats = $this->validationService->getValidationStats($validation_result);
$this->assertEquals(10, $stats['total_entries']);
$this->assertEquals(8, $stats['valid_entries']);
$this->assertEquals(2, $stats['invalid_entries']);
$this->assertEquals(2, $stats['error_count']);
$this->assertEquals(1, $stats['warning_count']);
$this->assertEquals(2, $stats['info_count']);
$this->assertEquals(80.0, $stats['success_rate']);
}
public function testGetErrorMessage(): void
{
$validation_result = [
'is_valid' => false,
'errors' => ['Error 1', 'Error 2'],
'warnings' => ['Warning 1'],
];
$message = $this->validationService->getErrorMessage($validation_result);
$this->assertStringContainsString('Errors:', $message);
$this->assertStringContainsString('• Error 1', $message);
$this->assertStringContainsString('• Error 2', $message);
$this->assertStringContainsString('Warnings:', $message);
$this->assertStringContainsString('• Warning 1', $message);
}
public function testGetErrorMessageWithValidResult(): void
{
$validation_result = [
'is_valid' => true,
'errors' => [],
'warnings' => [],
];
$message = $this->validationService->getErrorMessage($validation_result);
$this->assertEquals('', $message);
}
}

View file

@ -26,6 +26,7 @@ use App\Entity\Parts\Category;
use App\Services\ImportExportSystem\EntityExporter;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;
class EntityExporterTest extends WebTestCase
{
@ -76,7 +77,40 @@ class EntityExporterTest extends WebTestCase
$this->assertSame('application/json', $response->headers->get('Content-Type'));
$this->assertNotEmpty($response->headers->get('Content-Disposition'));
}
public function testExportToExcel(): void
{
$entities = $this->getEntities();
$xlsxData = $this->service->exportEntities($entities, ['format' => 'xlsx', 'level' => 'simple']);
$this->assertNotEmpty($xlsxData);
$tempFile = tempnam(sys_get_temp_dir(), 'test_export') . '.xlsx';
file_put_contents($tempFile, $xlsxData);
$spreadsheet = IOFactory::load($tempFile);
$worksheet = $spreadsheet->getActiveSheet();
$this->assertSame('name', $worksheet->getCell('A1')->getValue());
$this->assertSame('full_name', $worksheet->getCell('B1')->getValue());
$this->assertSame('Enitity 1', $worksheet->getCell('A2')->getValue());
$this->assertSame('Enitity 1', $worksheet->getCell('B2')->getValue());
unlink($tempFile);
}
public function testExportExcelFromRequest(): void
{
$entities = $this->getEntities();
$request = new Request();
$request->request->set('format', 'xlsx');
$request->request->set('level', 'simple');
$response = $this->service->exportEntityFromRequest($entities, $request);
$this->assertSame('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $response->headers->get('Content-Type'));
$this->assertStringContainsString('export_Category_simple.xlsx', $response->headers->get('Content-Disposition'));
}
}

View file

@ -22,6 +22,8 @@ declare(strict_types=1);
namespace App\Tests\Services\ImportExportSystem;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Entity\Attachments\AttachmentType;
use App\Entity\LabelSystem\LabelProfile;
@ -34,10 +36,11 @@ use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\HttpFoundation\File\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/**
* @group DB
*/
#[Group('DB')]
class EntityImporterTest extends WebTestCase
{
/**
@ -199,7 +202,7 @@ EOT;
$this->assertSame($longName, $errors[0]['entity']->getName());
}
public function formatDataProvider(): \Iterator
public static function formatDataProvider(): \Iterator
{
yield ['csv', 'csv'];
yield ['csv', 'CSV'];
@ -207,11 +210,13 @@ EOT;
yield ['json', 'json'];
yield ['yaml', 'yml'];
yield ['yaml', 'YAML'];
yield ['xlsx', 'xlsx'];
yield ['xlsx', 'XLSX'];
yield ['xls', 'xls'];
yield ['xls', 'XLS'];
}
/**
* @dataProvider formatDataProvider
*/
#[DataProvider('formatDataProvider')]
public function testDetermineFormat(string $expected, string $extension): void
{
$this->assertSame($expected, $this->service->determineFormat($extension));
@ -344,4 +349,41 @@ EOT;
$this->assertSame($category, $results[0]->getCategory());
$this->assertSame('test,test2', $results[0]->getTags());
}
public function testImportExcelFileProjects(): void
{
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->setCellValue('A1', 'name');
$worksheet->setCellValue('B1', 'comment');
$worksheet->setCellValue('A2', 'Test Excel 1');
$worksheet->setCellValue('B2', 'Test Excel 1 notes');
$worksheet->setCellValue('A3', 'Test Excel 2');
$worksheet->setCellValue('B3', 'Test Excel 2 notes');
$tempFile = tempnam(sys_get_temp_dir(), 'test_excel') . '.xlsx';
$writer = new Xlsx($spreadsheet);
$writer->save($tempFile);
$file = new File($tempFile);
$errors = [];
$results = $this->service->importFile($file, [
'class' => Project::class,
'format' => 'xlsx',
'csv_delimiter' => ';',
], $errors);
$this->assertCount(2, $results);
$this->assertEmpty($errors);
$this->assertContainsOnlyInstancesOf(Project::class, $results);
$this->assertSame('Test Excel 1', $results[0]->getName());
$this->assertSame('Test Excel 1 notes', $results[0]->getComment());
$this->assertSame('Test Excel 2', $results[1]->getName());
$this->assertSame('Test Excel 2 notes', $results[1]->getComment());
unlink($tempFile);
}
}

View file

@ -0,0 +1,63 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 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\InfoProviderSystem\DTOs;
use App\Services\InfoProviderSystem\DTOs\BulkSearchFieldMappingDTO;
use PHPUnit\Framework\TestCase;
class BulkSearchFieldMappingDTOTest extends TestCase
{
public function testIsSupplierPartNumberField(): void
{
$fieldMapping = new BulkSearchFieldMappingDTO(field: 'reichelt_spn', providers: ['provider1'], priority: 1);
$this->assertTrue($fieldMapping->isSupplierPartNumberField());
$fieldMapping = new BulkSearchFieldMappingDTO(field: 'partNumber', providers: ['provider1'], priority: 1);
$this->assertFalse($fieldMapping->isSupplierPartNumberField());
}
public function testToSerializableArray(): void
{
$fieldMapping = new BulkSearchFieldMappingDTO(field: 'test', providers: ['provider1', 'provider2'], priority: 3);
$array = $fieldMapping->toSerializableArray();
$this->assertIsArray($array);
$this->assertSame([
'field' => 'test',
'providers' => ['provider1', 'provider2'],
'priority' => 3,
], $array);
}
public function testFromSerializableArray(): void
{
$data = [
'field' => 'test',
'providers' => ['provider1', 'provider2'],
'priority' => 3,
];
$fieldMapping = BulkSearchFieldMappingDTO::fromSerializableArray($data);
$this->assertInstanceOf(BulkSearchFieldMappingDTO::class, $fieldMapping);
$this->assertSame('test', $fieldMapping->field);
$this->assertSame(['provider1', 'provider2'], $fieldMapping->providers);
$this->assertSame(3, $fieldMapping->priority);
}
}

View file

@ -0,0 +1,63 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 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\InfoProviderSystem\DTOs;
use App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultsDTO;
use PHPUnit\Framework\TestCase;
class BulkSearchPartResultsDTOTest extends TestCase
{
public function testHasErrors(): void
{
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []);
$this->assertFalse($test->hasErrors());
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], ['error1']);
$this->assertTrue($test->hasErrors());
}
public function testGetErrorCount(): void
{
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []);
$this->assertCount(0, $test->errors);
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], ['error1', 'error2']);
$this->assertCount(2, $test->errors);
}
public function testHasResults(): void
{
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []);
$this->assertFalse($test->hasResults());
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [ $this->createMock(\App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO::class) ], []);
$this->assertTrue($test->hasResults());
}
public function testGetResultCount(): void
{
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [], []);
$this->assertCount(0, $test->searchResults);
$test = new BulkSearchPartResultsDTO($this->createMock(\App\Entity\Parts\Part::class), [
$this->createMock(\App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO::class),
$this->createMock(\App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO::class)
], []);
$this->assertCount(2, $test->searchResults);
}
}

View file

@ -0,0 +1,258 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 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\InfoProviderSystem\DTOs;
use App\Entity\Parts\Part;
use App\Services\InfoProviderSystem\DTOs\BulkSearchResponseDTO;
use App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultDTO;
use App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultsDTO;
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class BulkSearchResponseDTOTest extends KernelTestCase
{
private EntityManagerInterface $entityManager;
private BulkSearchResponseDTO $dummyEmpty;
private BulkSearchResponseDTO $dummy;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->dummyEmpty = new BulkSearchResponseDTO(partResults: []);
$this->dummy = new BulkSearchResponseDTO(partResults: [
new BulkSearchPartResultsDTO(
part: $this->entityManager->find(Part::class, 1),
searchResults: [
new BulkSearchPartResultDTO(
searchResult: new SearchResultDTO(provider_key: "dummy", provider_id: "1234", name: "Test Part", description: "A part for testing"),
sourceField: "mpn", sourceKeyword: "1234", priority: 1
),
new BulkSearchPartResultDTO(
searchResult: new SearchResultDTO(provider_key: "test", provider_id: "test", name: "Test Part2", description: "A part for testing"),
sourceField: "name", sourceKeyword: "1234",
localPart: $this->entityManager->find(Part::class, 2), priority: 2,
),
],
errors: ['Error 1']
)
]);
}
public function testSerializationBackAndForthEmpty(): void
{
$serialized = $this->dummyEmpty->toSerializableRepresentation();
//Ensure that it is json_encodable
$json = json_encode($serialized, JSON_THROW_ON_ERROR);
$this->assertJson($json);
$deserialized = BulkSearchResponseDTO::fromSerializableRepresentation(json_decode($json), $this->entityManager);
$this->assertEquals($this->dummyEmpty, $deserialized);
}
public function testSerializationBackAndForth(): void
{
$serialized = $this->dummy->toSerializableRepresentation();
//Ensure that it is json_encodable
$this->assertJson(json_encode($serialized, JSON_THROW_ON_ERROR));
$deserialized = BulkSearchResponseDTO::fromSerializableRepresentation($serialized, $this->entityManager);
$this->assertEquals($this->dummy, $deserialized);
}
public function testToSerializableRepresentation(): void
{
$serialized = $this->dummy->toSerializableRepresentation();
$expected = array (
0 =>
array (
'part_id' => 1,
'search_results' =>
array (
0 =>
array (
'dto' =>
array (
'provider_key' => 'dummy',
'provider_id' => '1234',
'name' => 'Test Part',
'description' => 'A part for testing',
'category' => NULL,
'manufacturer' => NULL,
'mpn' => NULL,
'preview_image_url' => NULL,
'manufacturing_status' => NULL,
'provider_url' => NULL,
'footprint' => NULL,
),
'source_field' => 'mpn',
'source_keyword' => '1234',
'localPart' => NULL,
'priority' => 1,
),
1 =>
array (
'dto' =>
array (
'provider_key' => 'test',
'provider_id' => 'test',
'name' => 'Test Part2',
'description' => 'A part for testing',
'category' => NULL,
'manufacturer' => NULL,
'mpn' => NULL,
'preview_image_url' => NULL,
'manufacturing_status' => NULL,
'provider_url' => NULL,
'footprint' => NULL,
),
'source_field' => 'name',
'source_keyword' => '1234',
'localPart' => 2,
'priority' => 2,
),
),
'errors' =>
array (
0 => 'Error 1',
),
),
);
$this->assertEquals($expected, $serialized);
}
public function testFromSerializableRepresentation(): void
{
$input = array (
0 =>
array (
'part_id' => 1,
'search_results' =>
array (
0 =>
array (
'dto' =>
array (
'provider_key' => 'dummy',
'provider_id' => '1234',
'name' => 'Test Part',
'description' => 'A part for testing',
'category' => NULL,
'manufacturer' => NULL,
'mpn' => NULL,
'preview_image_url' => NULL,
'manufacturing_status' => NULL,
'provider_url' => NULL,
'footprint' => NULL,
),
'source_field' => 'mpn',
'source_keyword' => '1234',
'localPart' => NULL,
'priority' => 1,
),
1 =>
array (
'dto' =>
array (
'provider_key' => 'test',
'provider_id' => 'test',
'name' => 'Test Part2',
'description' => 'A part for testing',
'category' => NULL,
'manufacturer' => NULL,
'mpn' => NULL,
'preview_image_url' => NULL,
'manufacturing_status' => NULL,
'provider_url' => NULL,
'footprint' => NULL,
),
'source_field' => 'name',
'source_keyword' => '1234',
'localPart' => 2,
'priority' => 2,
),
),
'errors' =>
array (
0 => 'Error 1',
),
),
);
$deserialized = BulkSearchResponseDTO::fromSerializableRepresentation($input, $this->entityManager);
$this->assertEquals($this->dummy, $deserialized);
}
public function testMerge(): void
{
$merged = BulkSearchResponseDTO::merge($this->dummy, $this->dummyEmpty);
$this->assertCount(1, $merged->partResults);
$merged = BulkSearchResponseDTO::merge($this->dummyEmpty, $this->dummyEmpty);
$this->assertCount(0, $merged->partResults);
$merged = BulkSearchResponseDTO::merge($this->dummy, $this->dummy, $this->dummy);
$this->assertCount(3, $merged->partResults);
}
public function testReplaceResultsForPart(): void
{
$newPartResults = new BulkSearchPartResultsDTO(
part: $this->entityManager->find(Part::class, 1),
searchResults: [
new BulkSearchPartResultDTO(
searchResult: new SearchResultDTO(provider_key: "new", provider_id: "new", name: "New Part", description: "A new part"),
sourceField: "mpn", sourceKeyword: "new", priority: 1
)
],
errors: ['New Error']
);
$replaced = $this->dummy->replaceResultsForPart($newPartResults);
$this->assertCount(1, $replaced->partResults);
$this->assertSame($newPartResults, $replaced->partResults[0]);
}
public function testReplaceResultsForPartNotExisting(): void
{
$newPartResults = new BulkSearchPartResultsDTO(
part: $this->entityManager->find(Part::class, 1),
searchResults: [
new BulkSearchPartResultDTO(
searchResult: new SearchResultDTO(provider_key: "new", provider_id: "new", name: "New Part", description: "A new part"),
sourceField: "mpn", sourceKeyword: "new", priority: 1
)
],
errors: ['New Error']
);
$this->expectException(\InvalidArgumentException::class);
$replaced = $this->dummyEmpty->replaceResultsForPart($newPartResults);
}
}

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
*/
namespace App\Tests\Services\InfoProviderSystem\DTOs;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Services\InfoProviderSystem\DTOs\FileDTO;
use PHPUnit\Framework\TestCase;
@ -40,9 +41,7 @@ class FileDTOTest extends TestCase
yield ["test%7Cse", "test|se"];
}
/**
* @dataProvider escapingDataProvider
*/
#[DataProvider('escapingDataProvider')]
public function testURLEscaping(string $expected, string $input): void
{
$fileDTO = new FileDTO( $input);

View file

@ -22,13 +22,14 @@ declare(strict_types=1);
*/
namespace App\Tests\Services\InfoProviderSystem\DTOs;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
use PHPUnit\Framework\TestCase;
class ParameterDTOTest extends TestCase
{
public function parseValueFieldDataProvider(): \Generator
public static function parseValueFieldDataProvider(): \Generator
{
//Text value
yield [
@ -131,7 +132,7 @@ class ParameterDTOTest extends TestCase
];
}
public function parseValueIncludingUnitDataProvider(): \Generator
public static function parseValueIncludingUnitDataProvider(): \Generator
{
//Text value
yield [
@ -234,18 +235,18 @@ class ParameterDTOTest extends TestCase
}
/**
* @dataProvider parseValueFieldDataProvider
* @return void
*/
#[DataProvider('parseValueFieldDataProvider')]
public function testParseValueField(ParameterDTO $expected, string $name, string|float $value, ?string $unit = null, ?string $symbol = null, ?string $group = null)
{
$this->assertEquals($expected, ParameterDTO::parseValueField($name, $value, $unit, $symbol, $group));
}
/**
* @dataProvider parseValueIncludingUnitDataProvider
* @return void
*/
#[DataProvider('parseValueIncludingUnitDataProvider')]
public function testParseValueIncludingUnit(ParameterDTO $expected, string $name, string|float $value, ?string $symbol = null, ?string $group = null)
{
$this->assertEquals($expected, ParameterDTO::parseValueIncludingUnit($name, $value, $symbol, $group));

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
*/
namespace App\Tests\Services\InfoProviderSystem;
use App\Entity\PriceInformations\Currency;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Parts\ManufacturingStatus;
use App\Services\InfoProviderSystem\DTOs\FileDTO;
@ -83,6 +84,7 @@ class DTOtoEntityConverterTest extends WebTestCase
//For non-base currencies, a new currency entity is created
$currency = $entity->getCurrency();
$this->assertInstanceOf(Currency::class, $currency);
$this->assertEquals($dto->currency_iso_code, $currency->getIsoCode());
}

View file

@ -0,0 +1,540 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
* Copyright (C) 2024 Nexrem (https://github.com/meganukebmp)
*
* 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/>.
*/
declare(strict_types=1);
namespace App\Tests\Services\InfoProviderSystem\Providers;
use App\Services\InfoProviderSystem\DTOs\FileDTO;
use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
use App\Services\InfoProviderSystem\DTOs\PriceDTO;
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
use App\Services\InfoProviderSystem\Providers\LCSCProvider;
use App\Services\InfoProviderSystem\Providers\ProviderCapabilities;
use App\Settings\InfoProviderSystem\LCSCSettings;
use App\Tests\SettingsTestHelper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class LCSCProviderTest extends TestCase
{
private LCSCSettings $settings;
private LCSCProvider $provider;
private MockHttpClient $httpClient;
protected function setUp(): void
{
$this->httpClient = new MockHttpClient();
$this->settings = SettingsTestHelper::createSettingsDummy(LCSCSettings::class);
$this->settings->currency = 'USD';
$this->settings->enabled = true;
$this->provider = new LCSCProvider($this->httpClient, $this->settings);
}
public function testGetProviderInfo(): void
{
$info = $this->provider->getProviderInfo();
$this->assertIsArray($info);
$this->assertArrayHasKey('name', $info);
$this->assertArrayHasKey('description', $info);
$this->assertArrayHasKey('url', $info);
$this->assertArrayHasKey('disabled_help', $info);
$this->assertEquals('LCSC', $info['name']);
$this->assertEquals('https://www.lcsc.com/', $info['url']);
}
public function testGetProviderKey(): void
{
$this->assertEquals('lcsc', $this->provider->getProviderKey());
}
public function testIsActiveWhenEnabled(): void
{
//Ensure that the settings are enabled
$this->settings->enabled = true;
$this->assertTrue($this->provider->isActive());
}
public function testIsActiveWhenDisabled(): void
{
//Ensure that the settings are disabled
$this->settings->enabled = false;
$this->assertFalse($this->provider->isActive());
}
public function testGetCapabilities(): void
{
$capabilities = $this->provider->getCapabilities();
$this->assertIsArray($capabilities);
$this->assertContains(ProviderCapabilities::BASIC, $capabilities);
$this->assertContains(ProviderCapabilities::PICTURE, $capabilities);
$this->assertContains(ProviderCapabilities::DATASHEET, $capabilities);
$this->assertContains(ProviderCapabilities::PRICE, $capabilities);
$this->assertContains(ProviderCapabilities::FOOTPRINT, $capabilities);
}
public function testSearchByKeywordWithCCode(): void
{
$mockResponse = new MockResponse(json_encode([
'result' => [
'productCode' => 'C123456',
'productModel' => 'Test Component',
'productIntroEn' => 'Test description',
'brandNameEn' => 'Test Manufacturer',
'encapStandard' => '0603',
'productImageUrl' => 'https://example.com/image.jpg',
'productImages' => ['https://example.com/image1.jpg'],
'productPriceList' => [
['ladder' => 1, 'productPrice' => '0.10', 'currencySymbol' => 'US$']
],
'paramVOList' => [
['paramNameEn' => 'Resistance', 'paramValueEn' => '1kΩ']
],
'pdfUrl' => 'https://example.com/datasheet.pdf',
'weight' => 0.001
]
]));
$this->httpClient->setResponseFactory([$mockResponse]);
$results = $this->provider->searchByKeyword('C123456');
$this->assertIsArray($results);
$this->assertCount(1, $results);
$this->assertInstanceOf(PartDetailDTO::class, $results[0]);
$this->assertEquals('C123456', $results[0]->provider_id);
$this->assertEquals('Test Component', $results[0]->name);
}
public function testSearchByKeywordWithRegularTerm(): void
{
$mockResponse = new MockResponse(json_encode([
'result' => [
'productSearchResultVO' => [
'productList' => [
[
'productCode' => 'C789012',
'productModel' => 'Regular Component',
'productIntroEn' => 'Regular description',
'brandNameEn' => 'Regular Manufacturer',
'encapStandard' => '0805',
'productImageUrl' => 'https://example.com/regular.jpg',
'productImages' => ['https://example.com/regular1.jpg'],
'productPriceList' => [
['ladder' => 10, 'productPrice' => '0.08', 'currencySymbol' => '€']
],
'paramVOList' => [],
'pdfUrl' => null,
'weight' => null
]
]
]
]
]));
$this->httpClient->setResponseFactory([$mockResponse]);
$results = $this->provider->searchByKeyword('resistor');
$this->assertIsArray($results);
$this->assertCount(1, $results);
$this->assertInstanceOf(PartDetailDTO::class, $results[0]);
$this->assertEquals('C789012', $results[0]->provider_id);
$this->assertEquals('Regular Component', $results[0]->name);
}
public function testSearchByKeywordWithTipProduct(): void
{
$mockResponse = new MockResponse(json_encode([
'result' => [
'productSearchResultVO' => [
'productList' => []
],
'tipProductDetailUrlVO' => [
'productCode' => 'C555555'
]
]
]));
$detailResponse = new MockResponse(json_encode([
'result' => [
'productCode' => 'C555555',
'productModel' => 'Tip Component',
'productIntroEn' => 'Tip description',
'brandNameEn' => 'Tip Manufacturer',
'encapStandard' => '1206',
'productImageUrl' => null,
'productImages' => [],
'productPriceList' => [],
'paramVOList' => [],
'pdfUrl' => null,
'weight' => null
]
]));
$this->httpClient->setResponseFactory([$mockResponse, $detailResponse]);
$results = $this->provider->searchByKeyword('special');
$this->assertIsArray($results);
$this->assertCount(1, $results);
$this->assertInstanceOf(PartDetailDTO::class, $results[0]);
$this->assertEquals('C555555', $results[0]->provider_id);
$this->assertEquals('Tip Component', $results[0]->name);
}
public function testSearchByKeywordsBatch(): void
{
$mockResponse1 = new MockResponse(json_encode([
'result' => [
'productCode' => 'C123456',
'productModel' => 'Batch Component 1',
'productIntroEn' => 'Batch description 1',
'brandNameEn' => 'Batch Manufacturer',
'encapStandard' => '0603',
'productImageUrl' => null,
'productImages' => [],
'productPriceList' => [],
'paramVOList' => [],
'pdfUrl' => null,
'weight' => null
]
]));
$mockResponse2 = new MockResponse(json_encode([
'result' => [
'productSearchResultVO' => [
'productList' => [
[
'productCode' => 'C789012',
'productModel' => 'Batch Component 2',
'productIntroEn' => 'Batch description 2',
'brandNameEn' => 'Batch Manufacturer',
'encapStandard' => '0805',
'productImageUrl' => null,
'productImages' => [],
'productPriceList' => [],
'paramVOList' => [],
'pdfUrl' => null,
'weight' => null
]
]
]
]
]));
$this->httpClient->setResponseFactory([$mockResponse1, $mockResponse2]);
$results = $this->provider->searchByKeywordsBatch(['C123456', 'resistor']);
$this->assertIsArray($results);
$this->assertArrayHasKey('C123456', $results);
$this->assertArrayHasKey('resistor', $results);
$this->assertCount(1, $results['C123456']);
$this->assertCount(1, $results['resistor']);
$this->assertEquals('C123456', $results['C123456'][0]->provider_id);
$this->assertEquals('C789012', $results['resistor'][0]->provider_id);
}
public function testGetDetails(): void
{
$mockResponse = new MockResponse(json_encode([
'result' => [
'productCode' => 'C123456',
'productModel' => 'Detailed Component',
'productIntroEn' => 'Detailed description',
'brandNameEn' => 'Detailed Manufacturer',
'encapStandard' => '0603',
'productImageUrl' => 'https://example.com/detail.jpg',
'productImages' => ['https://example.com/detail1.jpg'],
'productPriceList' => [
['ladder' => 1, 'productPrice' => '0.10', 'currencySymbol' => 'US$'],
['ladder' => 10, 'productPrice' => '0.08', 'currencySymbol' => 'US$']
],
'paramVOList' => [
['paramNameEn' => 'Resistance', 'paramValueEn' => '1kΩ'],
['paramNameEn' => 'Tolerance', 'paramValueEn' => '1%']
],
'pdfUrl' => 'https://example.com/datasheet.pdf',
'weight' => 0.001
]
]));
$this->httpClient->setResponseFactory([$mockResponse]);
$result = $this->provider->getDetails('C123456');
$this->assertInstanceOf(PartDetailDTO::class, $result);
$this->assertEquals('C123456', $result->provider_id);
$this->assertEquals('Detailed Component', $result->name);
$this->assertEquals('Detailed description', $result->description);
$this->assertEquals('Detailed Manufacturer', $result->manufacturer);
$this->assertEquals('0603', $result->footprint);
$this->assertEquals('https://www.lcsc.com/product-detail/C123456.html', $result->provider_url);
$this->assertCount(1, $result->images);
$this->assertCount(2, $result->parameters);
$this->assertCount(1, $result->vendor_infos);
$this->assertEquals('0.001', $result->mass);
}
public function testGetDetailsWithNoResults(): void
{
$mockResponse = new MockResponse(json_encode([
'result' => [
'productSearchResultVO' => [
'productList' => []
]
]
]));
$this->httpClient->setResponseFactory([$mockResponse]);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('No part found with ID INVALID');
$this->provider->getDetails('INVALID');
}
public function testGetDetailsWithMultipleResults(): void
{
$mockResponse = new MockResponse(json_encode([
'result' => [
'productSearchResultVO' => [
'productList' => [
[
'productCode' => 'C123456',
'productModel' => 'Component 1',
'productIntroEn' => 'Description 1',
'brandNameEn' => 'Manufacturer 1',
'encapStandard' => '0603',
'productImageUrl' => null,
'productImages' => [],
'productPriceList' => [],
'paramVOList' => [],
'pdfUrl' => null,
'weight' => null
],
[
'productCode' => 'C789012',
'productModel' => 'Component 2',
'productIntroEn' => 'Description 2',
'brandNameEn' => 'Manufacturer 2',
'encapStandard' => '0805',
'productImageUrl' => null,
'productImages' => [],
'productPriceList' => [],
'paramVOList' => [],
'pdfUrl' => null,
'weight' => null
]
]
]
]
]));
$this->httpClient->setResponseFactory([$mockResponse]);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Multiple parts found with ID ambiguous');
$this->provider->getDetails('ambiguous');
}
public function testSanitizeFieldPrivateMethod(): void
{
$reflection = new \ReflectionClass($this->provider);
$method = $reflection->getMethod('sanitizeField');
$method->setAccessible(true);
$this->assertNull($method->invokeArgs($this->provider, [null]));
$this->assertEquals('Clean text', $method->invokeArgs($this->provider, ['Clean text']));
$this->assertEquals('Text without tags', $method->invokeArgs($this->provider, ['<b>Text</b> without <i>tags</i>']));
}
public function testGetUsedCurrencyPrivateMethod(): void
{
$reflection = new \ReflectionClass($this->provider);
$method = $reflection->getMethod('getUsedCurrency');
$method->setAccessible(true);
$this->assertEquals('USD', $method->invokeArgs($this->provider, ['US$']));
$this->assertEquals('USD', $method->invokeArgs($this->provider, ['$']));
$this->assertEquals('EUR', $method->invokeArgs($this->provider, ['€']));
$this->assertEquals('GBP', $method->invokeArgs($this->provider, ['£']));
$this->assertEquals('USD', $method->invokeArgs($this->provider, ['UNKNOWN'])); // fallback to configured currency
}
public function testGetProductShortURLPrivateMethod(): void
{
$reflection = new \ReflectionClass($this->provider);
$method = $reflection->getMethod('getProductShortURL');
$method->setAccessible(true);
$result = $method->invokeArgs($this->provider, ['C123456']);
$this->assertEquals('https://www.lcsc.com/product-detail/C123456.html', $result);
}
public function testGetProductDatasheetsPrivateMethod(): void
{
$reflection = new \ReflectionClass($this->provider);
$method = $reflection->getMethod('getProductDatasheets');
$method->setAccessible(true);
$result = $method->invokeArgs($this->provider, [null]);
$this->assertIsArray($result);
$this->assertEmpty($result);
$result = $method->invokeArgs($this->provider, ['https://example.com/datasheet.pdf']);
$this->assertIsArray($result);
$this->assertCount(1, $result);
$this->assertInstanceOf(FileDTO::class, $result[0]);
}
public function testGetProductImagesPrivateMethod(): void
{
$reflection = new \ReflectionClass($this->provider);
$method = $reflection->getMethod('getProductImages');
$method->setAccessible(true);
$result = $method->invokeArgs($this->provider, [null]);
$this->assertIsArray($result);
$this->assertEmpty($result);
$result = $method->invokeArgs($this->provider, [['https://example.com/image1.jpg', 'https://example.com/image2.jpg']]);
$this->assertIsArray($result);
$this->assertCount(2, $result);
$this->assertInstanceOf(FileDTO::class, $result[0]);
$this->assertInstanceOf(FileDTO::class, $result[1]);
}
public function testAttributesToParametersPrivateMethod(): void
{
$reflection = new \ReflectionClass($this->provider);
$method = $reflection->getMethod('attributesToParameters');
$method->setAccessible(true);
$attributes = [
['paramNameEn' => 'Resistance', 'paramValueEn' => '1kΩ'],
['paramNameEn' => 'Tolerance', 'paramValueEn' => '1%'],
['paramNameEn' => 'Empty', 'paramValueEn' => ''],
['paramNameEn' => 'Dash', 'paramValueEn' => '-']
];
$result = $method->invokeArgs($this->provider, [$attributes]);
$this->assertIsArray($result);
$this->assertCount(2, $result); // Only non-empty values
$this->assertInstanceOf(ParameterDTO::class, $result[0]);
$this->assertInstanceOf(ParameterDTO::class, $result[1]);
}
public function testPricesToVendorInfoPrivateMethod(): void
{
$reflection = new \ReflectionClass($this->provider);
$method = $reflection->getMethod('pricesToVendorInfo');
$method->setAccessible(true);
$prices = [
['ladder' => 1, 'productPrice' => '0.10', 'currencySymbol' => 'US$'],
['ladder' => 10, 'productPrice' => '0.08', 'currencySymbol' => 'US$']
];
$result = $method->invokeArgs($this->provider, ['C123456', 'https://example.com', $prices]);
$this->assertIsArray($result);
$this->assertCount(1, $result);
$this->assertInstanceOf(PurchaseInfoDTO::class, $result[0]);
$this->assertEquals('LCSC', $result[0]->distributor_name);
$this->assertEquals('C123456', $result[0]->order_number);
$this->assertCount(2, $result[0]->prices);
}
public function testCategoryBuilding(): void
{
$mockResponse = new MockResponse(json_encode([
'result' => [
'productCode' => 'C123456',
'productModel' => 'Test Component',
'productIntroEn' => 'Test description',
'brandNameEn' => 'Test Manufacturer',
'parentCatalogName' => 'Electronic Components',
'catalogName' => 'Resistors (SMT)',
'encapStandard' => '0603',
'productImageUrl' => null,
'productImages' => [],
'productPriceList' => [],
'paramVOList' => [],
'pdfUrl' => null,
'weight' => null
]
]));
$this->httpClient->setResponseFactory([$mockResponse]);
$result = $this->provider->getDetails('C123456');
$this->assertEquals('Electronic Components -> Resistors (SMT)', $result->category);
}
public function testEmptyFootprintHandling(): void
{
$mockResponse = new MockResponse(json_encode([
'result' => [
'productCode' => 'C123456',
'productModel' => 'Test Component',
'productIntroEn' => 'Test description',
'brandNameEn' => 'Test Manufacturer',
'encapStandard' => '-',
'productImageUrl' => null,
'productImages' => [],
'productPriceList' => [],
'paramVOList' => [],
'pdfUrl' => null,
'weight' => null
]
]));
$this->httpClient->setResponseFactory([$mockResponse]);
$result = $this->provider->getDetails('C123456');
$this->assertNull($result->footprint);
}
public function testSearchByKeywordsBatchWithEmptyKeywords(): void
{
$result = $this->provider->searchByKeywordsBatch([]);
$this->assertIsArray($result);
$this->assertEmpty($result);
}
public function testSearchByKeywordsBatchWithException(): void
{
$mockResponse = new MockResponse('', ['http_code' => 500]);
$this->httpClient->setResponseFactory([$mockResponse]);
$results = $this->provider->searchByKeywordsBatch(['error']);
$this->assertIsArray($results);
$this->assertArrayHasKey('error', $results);
$this->assertEmpty($results['error']);
}
}

View file

@ -41,6 +41,8 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem\BarcodeScanner;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use App\Entity\LabelSystem\LabelSupportedElement;
use App\Services\LabelSystem\BarcodeScanner\BarcodeRedirector;
use App\Services\LabelSystem\BarcodeScanner\BarcodeSourceType;
@ -66,10 +68,8 @@ final class BarcodeRedirectorTest extends KernelTestCase
yield [new LocalBarcodeScanResult(LabelSupportedElement::STORELOCATION, 1, BarcodeSourceType::INTERNAL), '/en/store_location/1/parts'];
}
/**
* @dataProvider urlDataProvider
* @group DB
*/
#[DataProvider('urlDataProvider')]
#[Group('DB')]
public function testGetRedirectURL(LocalBarcodeScanResult $scanResult, string $url): void
{
$this->assertSame($url, $this->service->getRedirectURL($scanResult));

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem\BarcodeScanner;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\LabelSystem\LabelSupportedElement;
use App\Services\LabelSystem\BarcodeScanner\BarcodeScanHelper;
use App\Services\LabelSystem\BarcodeScanner\BarcodeScanResultInterface;
@ -140,17 +141,13 @@ class BarcodeScanHelperTest extends WebTestCase
yield [''];
}
/**
* @dataProvider dataProvider
*/
#[DataProvider('dataProvider')]
public function testNormalizeBarcodeContent(BarcodeScanResultInterface $expected, string $input): void
{
$this->assertEquals($expected, $this->service->scanBarcodeContent($input));
}
/**
* @dataProvider invalidDataProvider
*/
#[DataProvider('invalidDataProvider')]
public function testInvalidFormats(string $input): void
{
$this->expectException(\InvalidArgumentException::class);

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -17,7 +20,6 @@
* 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\Services\LabelSystem\BarcodeScanner\EIGP114BarcodeScanResult;
@ -51,7 +53,7 @@ class EIGP114BarcodeScanResultTest extends TestCase
'4L' => 'US',
'13Z' => 'Digi-Key',
]);
$this->assertEquals('digikey', $barcode->guessBarcodeVendor());
$this->assertSame('digikey', $barcode->guessBarcodeVendor());
//Mouser barcode:
$barcode = new EIGP114BarcodeScanResult([
@ -64,7 +66,7 @@ class EIGP114BarcodeScanResultTest extends TestCase
'1V' => 'Mouser',
]);
$this->assertEquals('mouser', $barcode->guessBarcodeVendor());
$this->assertSame('mouser', $barcode->guessBarcodeVendor());
//Farnell barcode:
$barcode = new EIGP114BarcodeScanResult([
@ -77,7 +79,7 @@ class EIGP114BarcodeScanResultTest extends TestCase
'3P' => 'Farnell',
]);
$this->assertEquals('element14', $barcode->guessBarcodeVendor());
$this->assertSame('element14', $barcode->guessBarcodeVendor());
}
public function testIsFormat06Code(): void
@ -102,7 +104,7 @@ class EIGP114BarcodeScanResultTest extends TestCase
public function testParseFormat06Code(): void
{
$barcode = EIGP114BarcodeScanResult::parseFormat06Code("[)>\x1E06\x1DP596-777A1-ND\x1D1PXAF4444\x1DQ3\x1D10D1452\x1D1TBF1103\x1D4LUS\x1E\x04");
$this->assertEquals([
$this->assertSame([
'P' => '596-777A1-ND',
'1P' => 'XAF4444',
'Q' => '3',
@ -123,32 +125,32 @@ class EIGP114BarcodeScanResultTest extends TestCase
'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);
$this->assertSame('596-777A1-ND', $barcode->customerPartNumber);
$this->assertSame('XAF4444', $barcode->supplierPartNumber);
$this->assertSame(3, $barcode->quantity);
$this->assertSame('1452', $barcode->alternativeDateCode);
$this->assertSame('BF1103', $barcode->lotCode);
$this->assertSame('US', $barcode->countryOfOrigin);
}
public function testDigikeyParsing(): void
{
$barcode = EIGP114BarcodeScanResult::parseFormat06Code("[)>\x1e06\x1dPQ1045-ND\x1d1P364019-01\x1d30PQ1045-ND\x1dK12432 TRAVIS FOSS P\x1d1K85732873\x1d10K103332956\x1d9D231013\x1d1TQJ13P\x1d11K1\x1d4LTW\x1dQ3\x1d11ZPICK\x1d12Z7360988\x1d13Z999999\x1d20Z0000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
$this->assertEquals('digikey', $barcode->guessBarcodeVendor());
$this->assertSame('digikey', $barcode->guessBarcodeVendor());
$this->assertEquals('Q1045-ND', $barcode->customerPartNumber);
$this->assertEquals('364019-01', $barcode->supplierPartNumber);
$this->assertEquals(3, $barcode->quantity);
$this->assertEquals('231013', $barcode->dateCode);
$this->assertEquals('QJ13P', $barcode->lotCode);
$this->assertEquals('TW', $barcode->countryOfOrigin);
$this->assertEquals('Q1045-ND', $barcode->digikeyPartNumber);
$this->assertEquals('85732873', $barcode->digikeySalesOrderNumber);
$this->assertEquals('103332956', $barcode->digikeyInvoiceNumber);
$this->assertEquals('PICK', $barcode->digikeyLabelType);
$this->assertEquals('7360988', $barcode->digikeyPartID);
$this->assertEquals('999999', $barcode->digikeyNA);
$this->assertEquals('0000000000000000000000000000000000000000000000000000000000000000000000000000000000000', $barcode->digikeyPadding);
$this->assertSame('Q1045-ND', $barcode->customerPartNumber);
$this->assertSame('364019-01', $barcode->supplierPartNumber);
$this->assertSame(3, $barcode->quantity);
$this->assertSame('231013', $barcode->dateCode);
$this->assertSame('QJ13P', $barcode->lotCode);
$this->assertSame('TW', $barcode->countryOfOrigin);
$this->assertSame('Q1045-ND', $barcode->digikeyPartNumber);
$this->assertSame('85732873', $barcode->digikeySalesOrderNumber);
$this->assertSame('103332956', $barcode->digikeyInvoiceNumber);
$this->assertSame('PICK', $barcode->digikeyLabelType);
$this->assertSame('7360988', $barcode->digikeyPartID);
$this->assertSame('999999', $barcode->digikeyNA);
$this->assertSame('0000000000000000000000000000000000000000000000000000000000000000000000000000000000000', $barcode->digikeyPadding);
}
}

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem\Barcodes;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Entity\Parts\StorageLocation;
@ -57,31 +58,27 @@ class BarcodeContentGeneratorTest extends KernelTestCase
$this->service = self::getContainer()->get(BarcodeContentGenerator::class);
}
public function Barcode1DDataProvider(): \Iterator
public static function Barcode1DDataProvider(): \Iterator
{
yield ['P0000', Part::class];
yield ['L0000', PartLot::class];
yield ['S0000', StorageLocation::class];
}
public function Barcode2DDataProvider(): \Iterator
public static function Barcode2DDataProvider(): \Iterator
{
yield ['/scan/part/0', Part::class];
yield ['/scan/lot/0', PartLot::class];
yield ['/scan/location/0', StorageLocation::class];
}
/**
* @dataProvider Barcode1DDataProvider
*/
#[DataProvider('Barcode1DDataProvider')]
public function testGet1DBarcodeContent(string $expected, string $class): void
{
$this->assertSame($expected, $this->service->get1DBarcodeContent(new $class()));
}
/**
* @dataProvider Barcode2DDataProvider
*/
#[DataProvider('Barcode2DDataProvider')]
public function testGetURLContent(string $expected, string $class): void
{
$url = $this->service->getURLContent(new $class());

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Base\AbstractDBElement;
use App\Entity\LabelSystem\LabelOptions;
use App\Entity\LabelSystem\LabelSupportedElement;
@ -70,9 +71,7 @@ class LabelGeneratorTest extends WebTestCase
yield [LabelSupportedElement::STORELOCATION, StorageLocation::class];
}
/**
* @dataProvider supportsDataProvider
*/
#[DataProvider('supportsDataProvider')]
public function testSupports(LabelSupportedElement $type, string $class): void
{
$options = new LabelOptions();

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Services\LabelSystem\LabelTextReplacer;
@ -70,7 +71,7 @@ class LabelTextReplacerTest extends WebTestCase
$this->target->setComment('P Comment');
}
public function handlePlaceholderDataProvider(): \Iterator
public static function handlePlaceholderDataProvider(): \Iterator
{
yield ['Part 1', '[[NAME]]'];
yield ['P Description', '[[DESCRIPTION]]'];
@ -82,7 +83,7 @@ class LabelTextReplacerTest extends WebTestCase
yield ['Test [[NAME]]', 'Test [[NAME]]', 'Test [[NAME]]'];
}
public function replaceDataProvider(): \Iterator
public static function replaceDataProvider(): \Iterator
{
yield ['Part 1', '[[NAME]]'];
yield ['TestPart 1', 'Test[[NAME]]'];
@ -94,17 +95,13 @@ class LabelTextReplacerTest extends WebTestCase
yield ['TEST[[ ]]TEST', 'TEST[[ ]]TEST'];
}
/**
* @dataProvider handlePlaceholderDataProvider
*/
#[DataProvider('handlePlaceholderDataProvider')]
public function testHandlePlaceholder(string $expected, string $input): void
{
$this->assertSame($expected, $this->service->handlePlaceholder($input, $this->target));
}
/**
* @dataProvider replaceDataProvider
*/
#[DataProvider('replaceDataProvider')]
public function testReplace(string $expected, string $input): void
{
$this->assertSame($expected, $this->service->replace($input, $this->target));

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Base\AbstractDBElement;
use App\Services\LabelSystem\PlaceholderProviders\AbstractDBElementProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -63,14 +64,12 @@ class AbstractElementProviderTest extends WebTestCase
};
}
public function dataProvider(): \Iterator
public static function dataProvider(): \Iterator
{
yield ['123', '[[ID]]'];
}
/**
* @dataProvider dataProvider
*/
#[DataProvider('dataProvider')]
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Parts\Part;
use App\Services\LabelSystem\PlaceholderProviders\GlobalProviders;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -61,15 +62,13 @@ class GlobalProvidersTest extends WebTestCase
$this->target = new Part();
}
public function dataProvider(): \Iterator
public static function dataProvider(): \Iterator
{
yield ['Part-DB', '[[INSTALL_NAME]]'];
yield ['anonymous', '[[USERNAME]]'];
}
/**
* @dataProvider dataProvider
*/
#[DataProvider('dataProvider')]
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Contracts\NamedElementInterface;
use App\Services\LabelSystem\PlaceholderProviders\NamedElementProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -66,14 +67,12 @@ class NamedElementProviderTest extends WebTestCase
};
}
public function dataProvider(): \Iterator
public static function dataProvider(): \Iterator
{
yield ['This is my Name', '[[NAME]]'];
}
/**
* @dataProvider dataProvider
*/
#[DataProvider('dataProvider')]
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Entity\Parts\StorageLocation;
@ -85,7 +86,7 @@ class PartLotProviderTest extends WebTestCase
$this->target->setOwner($user);
}
public function dataProvider(): \Iterator
public static function dataProvider(): \Iterator
{
yield ['unknown', '[[LOT_ID]]'];
yield ['Lot description', '[[LOT_NAME]]'];
@ -101,9 +102,7 @@ class PartLotProviderTest extends WebTestCase
yield ['user', '[[OWNER_USERNAME]]'];
}
/**
* @dataProvider dataProvider
*/
#[DataProvider('dataProvider')]
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));

View file

@ -41,6 +41,8 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Parts\ManufacturingStatus;
use Doctrine\ORM\EntityManager;
use App\Entity\Parts\Category;
@ -50,9 +52,7 @@ use App\Services\LabelSystem\PlaceholderProviders\PartProvider;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @group DB
*/
#[Group('DB')]
class PartProviderTest extends WebTestCase
{
/**
@ -87,7 +87,7 @@ class PartProviderTest extends WebTestCase
$this->target->setComment('<b>Bold</b> *Italic*');
}
public function dataProvider(): \Iterator
public static function dataProvider(): \Iterator
{
yield ['Node 2.1', '[[CATEGORY]]'];
yield ['Node 2 → Node 2.1', '[[CATEGORY_FULL]]'];
@ -105,9 +105,7 @@ class PartProviderTest extends WebTestCase
yield ['Bold Italic', '[[COMMENT_T]]'];
}
/**
* @dataProvider dataProvider
*/
#[DataProvider('dataProvider')]
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Contracts\TimeStampableInterface;
use App\Services\LabelSystem\PlaceholderProviders\GlobalProviders;
use App\Services\LabelSystem\PlaceholderProviders\TimestampableElementProvider;
@ -59,33 +60,34 @@ class TimestampableElementProviderTest extends WebTestCase
protected function setUp(): void
{
self::bootKernel();
\Locale::setDefault('en');
\Locale::setDefault('en_US');
$this->service = self::getContainer()->get(TimestampableElementProvider::class);
$this->target = new class() implements TimeStampableInterface {
$this->target = new class () implements TimeStampableInterface {
public function getLastModified(): ?DateTime
{
return new \DateTime('2000-01-01');
return new DateTime('2000-01-01');
}
public function getAddedDate(): ?DateTime
{
return new \DateTime('2000-01-01');
return new DateTime('2000-01-01');
}
};
}
public function dataProvider(): \Iterator
public static function dataProvider(): \Iterator
{
\Locale::setDefault('en');
yield ['1/1/00, 12:00 AM', '[[LAST_MODIFIED]]'];
yield ['1/1/00, 12:00 AM', '[[CREATION_DATE]]'];
\Locale::setDefault('en_US');
// Use IntlDateFormatter like the actual service does
$formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::SHORT, \IntlDateFormatter::SHORT);
$expectedFormat = $formatter->format(new DateTime('2000-01-01'));
yield [$expectedFormat, '[[LAST_MODIFIED]]'];
yield [$expectedFormat, '[[CREATION_DATE]]'];
}
/**
* @dataProvider dataProvider
*/
#[DataProvider('dataProvider')]
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));
}
}
}

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\LabelSystem;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\LabelSystem\LabelOptions;
use App\Entity\LabelSystem\LabelProcessMode;
use App\Entity\LabelSystem\LabelSupportedElement;
@ -61,7 +62,7 @@ class SandboxedTwigFactoryTest extends WebTestCase
$this->service = self::getContainer()->get(SandboxedTwigFactory::class);
}
public function twigDataProvider(): \Iterator
public static function twigDataProvider(): \Iterator
{
yield [' {% for i in range(1, 3) %}
{{ part.id }}
@ -94,7 +95,7 @@ class SandboxedTwigFactoryTest extends WebTestCase
'];
}
public function twigNotAllowedDataProvider(): \Iterator
public static function twigNotAllowedDataProvider(): \Iterator
{
yield ['{% block test %} {% endblock %}'];
yield ['{% deprecated test %}'];
@ -103,9 +104,7 @@ class SandboxedTwigFactoryTest extends WebTestCase
yield ['{{ part.setCategory(null) }}'];
}
/**
* @dataProvider twigDataProvider
*/
#[DataProvider('twigDataProvider')]
public function testTwigFeatures(string $twig): void
{
$options = new LabelOptions();
@ -123,9 +122,7 @@ class SandboxedTwigFactoryTest extends WebTestCase
$this->assertIsString($str);
}
/**
* @dataProvider twigNotAllowedDataProvider
*/
#[DataProvider('twigNotAllowedDataProvider')]
public function testTwigForbidden(string $twig): void
{
$this->expectException(SecurityError::class);

View file

@ -23,23 +23,22 @@ declare(strict_types=1);
namespace App\Tests\Services\LogSystem;
use App\Services\LogSystem\EventCommentNeededHelper;
use App\Services\LogSystem\EventCommentType;
use App\Settings\SystemSettings\HistorySettings;
use App\Tests\SettingsTestHelper;
use PHPUnit\Framework\TestCase;
class EventCommentNeededHelperTest extends TestCase
{
public function testIsCommentNeeded(): void
{
$service = new EventCommentNeededHelper(['part_edit', 'part_create']);
$this->assertTrue($service->isCommentNeeded('part_edit'));
$this->assertTrue($service->isCommentNeeded('part_create'));
$this->assertFalse($service->isCommentNeeded('part_delete'));
$this->assertFalse($service->isCommentNeeded('part_stock_operation'));
}
$settings = SettingsTestHelper::createSettingsDummy(HistorySettings::class);
$settings->enforceComments = [EventCommentType::PART_CREATE, EventCommentType::PART_EDIT];
public function testIsCommentNeededInvalidTypeException(): void
{
$service = new EventCommentNeededHelper(['part_edit', 'part_create']);
$this->expectException(\InvalidArgumentException::class);
$service->isCommentNeeded('this_is_not_valid');
$service = new EventCommentNeededHelper($settings);
$this->assertTrue($service->isCommentNeeded(EventCommentType::PART_CREATE));
$this->assertTrue($service->isCommentNeeded(EventCommentType::PART_EDIT));
$this->assertFalse($service->isCommentNeeded(EventCommentType::DATASTRUCTURE_EDIT));
$this->assertFalse($service->isCommentNeeded(EventCommentType::PART_STOCK_OPERATION));
}
}

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -17,7 +20,6 @@
* 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\LogSystem;
use App\Entity\LogSystem\ElementEditedLogEntry;
@ -45,7 +47,7 @@ class TimeTravelTest extends KernelTestCase
$undeletedCategory = $this->service->undeleteEntity(Category::class, 100);
$this->assertInstanceOf(Category::class, $undeletedCategory);
$this->assertEquals(100, $undeletedCategory->getId());
$this->assertSame(100, $undeletedCategory->getId());
}
public function testApplyEntry(): void
@ -63,8 +65,8 @@ class TimeTravelTest extends KernelTestCase
$this->service->applyEntry($category, $logEntry);
$this->assertEquals('Old Category', $category->getName());
$this->assertEquals('Old Comment', $category->getComment());
$this->assertSame('Old Category', $category->getName());
$this->assertSame('Old Comment', $category->getComment());
}
public function testRevertEntityToTimestamp(): void

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Tests\Services\Misc;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Services\Misc\FAIconGenerator;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -41,7 +42,7 @@ class FAIconGeneratorTest extends WebTestCase
$this->service = self::getContainer()->get(FAIconGenerator::class);
}
public function fileExtensionDataProvider(): \Iterator
public static function fileExtensionDataProvider(): \Iterator
{
yield ['pdf', 'fa-file-pdf'];
yield ['jpeg','fa-file-image'];
@ -95,9 +96,7 @@ class FAIconGeneratorTest extends WebTestCase
yield ['fgd', 'fa-file'];
}
/**
* @dataProvider fileExtensionDataProvider
*/
#[DataProvider('fileExtensionDataProvider')]
public function testFileExtensionToFAType(string $ext, string $expected): void
{
$this->assertSame($expected, $this->service->fileExtensionToFAType($ext), 'Failed for extension .'.$ext);

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\Misc;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Services\Misc\RangeParser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -57,7 +58,7 @@ class RangeParserTest extends WebTestCase
$this->service = self::getContainer()->get(RangeParser::class);
}
public function dataProvider(): \Iterator
public static function dataProvider(): \Iterator
{
yield [[], ''];
yield [[], ' '];
@ -81,7 +82,7 @@ class RangeParserTest extends WebTestCase
yield [[], '1, 2, test', true];
}
public function validDataProvider(): \Iterator
public static function validDataProvider(): \Iterator
{
yield [true, ''];
yield [true, ' '];
@ -96,9 +97,7 @@ class RangeParserTest extends WebTestCase
yield [false, '1, 2 test'];
}
/**
* @dataProvider dataProvider
*/
#[DataProvider('dataProvider')]
public function testParse(array $expected, string $input, bool $must_throw = false): void
{
if ($must_throw) {
@ -109,9 +108,7 @@ class RangeParserTest extends WebTestCase
}
}
/**
* @dataProvider validDataProvider
*/
#[DataProvider('validDataProvider')]
public function testIsValidRange(bool $expected, string $input): void
{
$this->assertSame($expected, $this->service->isValidRange($input));

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Tests\Services\Parameters;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Parameters\AbstractParameter;
use App\Services\Parameters\ParameterExtractor;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -56,7 +57,7 @@ class ParameterExtractorTest extends WebTestCase
$this->service = self::getContainer()->get(ParameterExtractor::class);
}
public function emptyDataProvider(): \Iterator
public static function emptyDataProvider(): \Iterator
{
yield [''];
yield [' '];
@ -69,9 +70,7 @@ class ParameterExtractorTest extends WebTestCase
yield ['A [link](https://demo.part-db.de) should not be matched'];
}
/**
* @dataProvider emptyDataProvider
*/
#[DataProvider('emptyDataProvider')]
public function testShouldReturnEmpty(string $input): void
{
$this->assertEmpty($this->service->extractParameters($input));

View file

@ -51,8 +51,7 @@ class PartsTableActionHandlerTest extends WebTestCase
foreach ($formats as $format) {
$action = "export_{$format}";
$result = $this->service->handleAction($action, $selected_parts, '1', '/test');
$result = $this->service->handleAction($action, $selected_parts, '1', '/test');
$this->assertInstanceOf(RedirectResponse::class, $result);
$this->assertStringContainsString('parts/export', $result->getTargetUrl());
$this->assertStringContainsString("format={$format}", $result->getTargetUrl());

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Tests\Services\Parts;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\Parts\Part;
use App\Entity\PriceInformations\Orderdetail;
use App\Entity\PriceInformations\Pricedetail;
@ -43,7 +44,7 @@ class PricedetailHelperTest extends WebTestCase
$this->service = self::getContainer()->get(PricedetailHelper::class);
}
public function maxDiscountAmountDataProvider(): ?\Generator
public static function maxDiscountAmountDataProvider(): ?\Generator
{
$part = new Part();
yield [$part, null, 'Part without any orderdetails failed!'];
@ -81,9 +82,7 @@ class PricedetailHelperTest extends WebTestCase
yield [$part, 10.0, 'Part with multiple orderdetails failed'];
}
/**
* @dataProvider maxDiscountAmountDataProvider
*/
#[DataProvider('maxDiscountAmountDataProvider')]
public function testGetMaxDiscountAmount(Part $part, ?float $expected_result, string $message): void
{
$this->assertSame($expected_result, $this->service->getMaxDiscountAmount($part), $message);

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Tests\Services\Trees;
use PHPUnit\Framework\Attributes\Group;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Parts\Category;
use App\Helpers\Trees\TreeViewNode;
@ -29,9 +30,7 @@ use App\Services\Trees\TreeViewGenerator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @group DB
*/
#[Group('DB')]
class TreeViewGeneratorTest extends WebTestCase
{
protected $em;

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Tests\Services\UserSystem;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\PermissionData;
use App\Entity\UserSystem\User;
@ -91,7 +92,7 @@ class PermissionManagerTest extends WebTestCase
$this->group->method('getParent')->willReturn($parent_group);
}
public function getPermissionNames(): \Iterator
public static function getPermissionNames(): \Iterator
{
//List some permission names
yield ['parts'];
@ -101,9 +102,7 @@ class PermissionManagerTest extends WebTestCase
yield ['tools'];
}
/**
* @dataProvider getPermissionNames
*/
#[DataProvider('getPermissionNames')]
public function testListOperationsForPermission($perm_name): void
{
$arr = $this->service->listOperationsForPermission($perm_name);

View file

@ -90,9 +90,9 @@ class PermissionSchemaUpdaterTest extends WebTestCase
//Do an upgrade and afterward the move, add, and withdraw permissions should be set to ALLOW
self::assertTrue($this->service->upgradeSchema($user, 1));
self::assertEquals(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('parts_stock', 'move'));
self::assertEquals(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('parts_stock', 'add'));
self::assertEquals(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('parts_stock', 'withdraw'));
self::assertSame(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('parts_stock', 'move'));
self::assertSame(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('parts_stock', 'add'));
self::assertSame(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('parts_stock', 'withdraw'));
}
public function testUpgradeSchemaToVersion2(): void
@ -106,9 +106,9 @@ class PermissionSchemaUpdaterTest extends WebTestCase
//After the upgrade all operations should be available under the name "projects" with the same values
self::assertTrue($this->service->upgradeSchema($user, 2));
self::assertEquals(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('projects', 'read'));
self::assertEquals(PermissionData::INHERIT, $user->getPermissions()->getPermissionValue('projects', 'edit'));
self::assertEquals(PermissionData::DISALLOW, $user->getPermissions()->getPermissionValue('projects', 'delete'));
self::assertSame(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('projects', 'read'));
self::assertSame(PermissionData::INHERIT, $user->getPermissions()->getPermissionValue('projects', 'edit'));
self::assertSame(PermissionData::DISALLOW, $user->getPermissions()->getPermissionValue('projects', 'delete'));
}
public function testUpgradeSchemaToVersion3(): void

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Tests\Services\UserSystem\TFA;
use PHPUnit\Framework\Attributes\DataProvider;
use App\Services\UserSystem\TFA\BackupCodeGenerator;
use PHPUnit\Framework\TestCase;
use RuntimeException;
@ -46,7 +47,7 @@ class BackupCodeGeneratorTest extends TestCase
new BackupCodeGenerator(4, 10);
}
public function codeLengthDataProvider(): \Iterator
public static function codeLengthDataProvider(): \Iterator
{
yield [6];
yield [8];
@ -54,25 +55,21 @@ class BackupCodeGeneratorTest extends TestCase
yield [16];
}
/**
* @dataProvider codeLengthDataProvider
*/
#[DataProvider('codeLengthDataProvider')]
public function testGenerateSingleCode(int $code_length): void
{
$generator = new BackupCodeGenerator($code_length, 10);
$this->assertMatchesRegularExpression("/^([a-f0-9]){{$code_length}}\$/", $generator->generateSingleCode());
}
public function codeCountDataProvider(): \Iterator
public static function codeCountDataProvider(): \Iterator
{
yield [2];
yield [8];
yield [10];
}
/**
* @dataProvider codeCountDataProvider
*/
#[DataProvider('codeCountDataProvider')]
public function testGenerateCodeSet(int $code_count): void
{
$generator = new BackupCodeGenerator(8, $code_count);