Added tests for PCBnew BOM type

This commit is contained in:
Jan Böhmer 2023-03-16 23:32:12 +01:00
parent bd2559c37b
commit e444388517
4 changed files with 129 additions and 2 deletions

View file

@ -32,6 +32,8 @@ use App\Services\ImportExportSystem\BOMImporter;
use App\Services\ProjectSystem\ProjectBuildHelper; use App\Services\ProjectSystem\ProjectBuildHelper;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use League\Csv\Exception;
use League\Csv\SyntaxError;
use Omines\DataTablesBundle\DataTableFactory; use Omines\DataTablesBundle\DataTableFactory;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -190,6 +192,8 @@ class ProjectController extends AbstractController
} }
} catch (\UnexpectedValueException $e) { } catch (\UnexpectedValueException $e) {
$this->addFlash('error', t('project.bom_import.flash.invalid_file', ['%message%' => $e->getMessage()])); $this->addFlash('error', t('project.bom_import.flash.invalid_file', ['%message%' => $e->getMessage()]));
} catch (SyntaxError $e) {
$this->addFlash('error', t('project.bom_import.flash.invalid_file', ['%message%' => $e->getMessage()]));
} }
} }

View file

@ -45,7 +45,7 @@ class BOMImporter
{ {
} }
public function configureOptions(OptionsResolver $resolver): OptionsResolver protected function configureOptions(OptionsResolver $resolver): OptionsResolver
{ {
$resolver->setRequired('type'); $resolver->setRequired('type');
$resolver->setAllowedValues('type', ['kicad_pcbnew']); $resolver->setAllowedValues('type', ['kicad_pcbnew']);

View file

@ -0,0 +1,123 @@
<?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)
*
* 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\ProjectSystem\Project;
use App\Entity\ProjectSystem\ProjectBOMEntry;
use App\Services\ImportExportSystem\BOMImporter;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\File\File;
class BOMImporterTest extends WebTestCase
{
/**
* @var BOMImporter
*/
protected $service;
protected function setUp(): void
{
parent::setUp();
//Get an service instance.
self::bootKernel();
$this->service = self::getContainer()->get(BOMImporter::class);
}
public function testImportFileIntoProject(): void
{
$inpute = $input = <<<CSV
"ID";"Bezeichner";"Footprint";"Stückzahl";"Bezeichnung";"Anbieter und Referenz";
1;"R19,R17";"R_0805_2012Metric_Pad1.20x1.40mm_HandSolder";2;"4.7k";Test;;
2;"D1";"D_DO-41_SOD81_P10.16mm_Horizontal";1;"1N5059";;;
3;"J3,J5";"JST_XH_B5B-XH-AM_1x05_P2.50mm_Vertical";2;"DISPLAY";;;
4;"C6";"CP_Radial_D6.3mm_P2.50mm";1;"47uF";;;
CSV;
$file = $this->createMock(File::class);
$file->method('getContent')->willReturn($input);
$project = new Project();
$this->assertCount(0, $project->getBOMEntries());
$bom_entries = $this->service->importFileIntoProject($file, $project, ['type' => 'kicad_pcbnew']);
$this->assertContainsOnlyInstancesOf(ProjectBOMEntry::class, $bom_entries);
$this->assertCount(4, $bom_entries);
//Check that the BOM entries are added to the project
$this->assertCount(4, $project->getBOMEntries());
}
public function testStringToBOMEntriesKiCADPCB(): void
{
//Test for german input
$input = <<<CSV
"ID";"Bezeichner";"Footprint";"Stückzahl";"Bezeichnung";"Anbieter und Referenz";
1;"R19,R17";"R_0805_2012Metric_Pad1.20x1.40mm_HandSolder";2;"4.7k";Test;;
2;"D1";"D_DO-41_SOD81_P10.16mm_Horizontal";1;"1N5059";;;
3;"J3,J5";"JST_XH_B5B-XH-AM_1x05_P2.50mm_Vertical";2;"DISPLAY";;;
4;"C6";"CP_Radial_D6.3mm_P2.50mm";1;"47uF";;;
CSV;
$bom = $this->service->stringToBOMEntries($input, ['type' => 'kicad_pcbnew']);
$this->assertContainsOnlyInstancesOf(ProjectBOMEntry::class, $bom);
$this->assertCount(4, $bom);
$this->assertEquals('R19,R17', $bom[0]->getMountnames());
$this->assertEquals(2.0, $bom[0]->getQuantity());
$this->assertSame('4.7k (R_0805_2012Metric_Pad1.20x1.40mm_HandSolder)', $bom[0]->getName());
$this->assertSame('Test', $bom[0]->getComment());
//Test for english input
$input = <<<CSV
"Id";"Designator";"Package";"Quantity";"Designation";"Supplier and ref";
1;"R19,R17";"R_0805_2012Metric_Pad1.20x1.40mm_HandSolder";2;"4.7k";Test;;
2;"D1";"D_DO-41_SOD81_P10.16mm_Horizontal";1;"1N5059";;;
3;"J3,J5";"JST_XH_B5B-XH-AM_1x05_P2.50mm_Vertical";2;"DISPLAY";;;
4;"C6";"CP_Radial_D6.3mm_P2.50mm";1;"47uF";;;
CSV;
$bom = $this->service->stringToBOMEntries($input, ['type' => 'kicad_pcbnew']);
$this->assertContainsOnlyInstancesOf(ProjectBOMEntry::class, $bom);
$this->assertCount(4, $bom);
$this->assertEquals('R19,R17', $bom[0]->getMountnames());
$this->assertEquals(2.0, $bom[0]->getQuantity());
$this->assertSame('4.7k (R_0805_2012Metric_Pad1.20x1.40mm_HandSolder)', $bom[0]->getName());
$this->assertSame('Test', $bom[0]->getComment());
}
public function testStringToBOMEntriesKiCADPCBError(): void
{
$input = <<<CSV
"ID";"Test";
1;"R19,R17";"R_0805_2012Metric_Pad1.20x1.40mm_HandSolder";2;"4.7k";Test;;
CSV;
$this->expectException(\UnexpectedValueException::class);
$this->service->stringToBOMEntries($input, ['type' => 'kicad_pcbnew']);
}
}

View file

@ -37,7 +37,7 @@ use Symfony\Component\Validator\ConstraintViolation;
class EntityImporterTest extends WebTestCase class EntityImporterTest extends WebTestCase
{ {
/** /**
* @var AmountFormatter * @var EntityImporter
*/ */
protected $service; protected $service;