2019-11-09 16:14:57 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-12-18 17:28:42 +01:00
|
|
|
namespace App\Tests\Services\ImportExportSystem;
|
2019-11-09 16:14:57 +01:00
|
|
|
|
|
|
|
use App\Entity\Attachments\AttachmentType;
|
2023-03-14 00:17:13 +01:00
|
|
|
use App\Entity\Parts\Category;
|
|
|
|
use App\Entity\Parts\Part;
|
2023-01-28 23:24:45 +01:00
|
|
|
use App\Entity\UserSystem\User;
|
2022-12-18 17:28:42 +01:00
|
|
|
use App\Services\ImportExportSystem\EntityImporter;
|
2019-11-09 16:14:57 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
2023-03-14 00:17:13 +01:00
|
|
|
use Symfony\Component\Validator\ConstraintViolation;
|
2019-11-09 16:14:57 +01:00
|
|
|
|
2019-12-27 15:20:06 +01:00
|
|
|
/**
|
|
|
|
* @group DB
|
|
|
|
*/
|
2019-11-09 16:14:57 +01:00
|
|
|
class EntityImporterTest extends WebTestCase
|
|
|
|
{
|
|
|
|
/**
|
2023-03-16 23:32:12 +01:00
|
|
|
* @var EntityImporter
|
2019-11-09 16:14:57 +01:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
protected function setUp(): void
|
2019-11-09 16:14:57 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
//Get an service instance.
|
|
|
|
self::bootKernel();
|
2022-12-18 19:45:04 +01:00
|
|
|
$this->service = self::getContainer()->get(EntityImporter::class);
|
2019-11-09 16:14:57 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testMassCreationResults(): void
|
2019-11-09 16:14:57 +01:00
|
|
|
{
|
|
|
|
$errors = [];
|
|
|
|
$results = $this->service->massCreation('', AttachmentType::class, null, $errors);
|
|
|
|
$this->assertEmpty($results);
|
|
|
|
$this->assertEmpty($errors);
|
|
|
|
|
|
|
|
$errors = [];
|
2023-01-28 23:24:45 +01:00
|
|
|
$lines = "Test 1\nTest 2 \nTest 3";
|
2019-11-09 16:14:57 +01:00
|
|
|
$results = $this->service->massCreation($lines, AttachmentType::class, null, $errors);
|
|
|
|
$this->assertCount(0, $errors);
|
|
|
|
$this->assertCount(3, $results);
|
|
|
|
//Check type
|
|
|
|
$this->assertInstanceOf(AttachmentType::class, $results[0]);
|
|
|
|
//Check names
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame('Test 1', $results[0]->getName());
|
|
|
|
$this->assertSame('Test 2', $results[1]->getName());
|
2019-11-09 16:14:57 +01:00
|
|
|
//Check parent
|
|
|
|
$this->assertNull($results[0]->getMasterPictureAttachment());
|
|
|
|
|
|
|
|
$parent = new AttachmentType();
|
|
|
|
$results = $this->service->massCreation($lines, AttachmentType::class, $parent, $errors);
|
|
|
|
$this->assertCount(3, $results);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($parent, $results[0]->getParent());
|
2019-11-09 16:14:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:24:45 +01:00
|
|
|
public function testNonStructuralClass(): void
|
|
|
|
{
|
|
|
|
$input = <<<EOT
|
|
|
|
Test1
|
|
|
|
Test1.1
|
|
|
|
Test2
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
$results = $this->service->massCreation($input, User::class, null, $errors);
|
|
|
|
|
|
|
|
//Import must not fail, even with non-structural classes
|
|
|
|
$this->assertCount(3, $results);
|
|
|
|
$this->assertCount(0, $errors);
|
|
|
|
|
|
|
|
$this->assertSame('Test1', $results[0]->getName());
|
|
|
|
$this->assertSame('Test1.1', $results[1]->getName());
|
|
|
|
$this->assertSame('Test2', $results[2]->getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMassCreationNested(): void
|
|
|
|
{
|
|
|
|
$input = <<<EOT
|
|
|
|
Test 1
|
|
|
|
Test 1.1
|
|
|
|
Test 1.1.1
|
|
|
|
Test 1.1.2
|
|
|
|
Test 1.2
|
|
|
|
Test 1.2.1
|
|
|
|
Test 2
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
$parent = new AttachmentType();
|
|
|
|
$results = $this->service->massCreation($input, AttachmentType::class, $parent, $errors);
|
|
|
|
|
|
|
|
//We have 7 elements, an now errros
|
|
|
|
$this->assertCount(0, $errors);
|
|
|
|
$this->assertCount(7, $results);
|
|
|
|
|
|
|
|
$element1 = $results[0];
|
|
|
|
$element11 = $results[1];
|
|
|
|
$element111 = $results[2];
|
|
|
|
$element112 = $results[3];
|
|
|
|
$element12 = $results[4];
|
|
|
|
$element121 = $results[5];
|
|
|
|
$element2 = $results[6];
|
|
|
|
|
|
|
|
$this->assertSame('Test 1', $element1->getName());
|
|
|
|
$this->assertSame('Test 1.1', $element11->getName());
|
|
|
|
$this->assertSame('Test 1.1.1', $element111->getName());
|
|
|
|
$this->assertSame('Test 1.1.2', $element112->getName());
|
|
|
|
$this->assertSame('Test 1.2', $element12->getName());
|
|
|
|
$this->assertSame('Test 1.2.1', $element121->getName());
|
|
|
|
$this->assertSame('Test 2', $element2->getName());
|
|
|
|
|
|
|
|
//Check parents
|
|
|
|
$this->assertSame($parent, $element1->getParent());
|
|
|
|
$this->assertSame($element1, $element11->getParent());
|
|
|
|
$this->assertSame($element11, $element111->getParent());
|
|
|
|
$this->assertSame($element11, $element112->getParent());
|
|
|
|
$this->assertSame($element1, $element12->getParent());
|
|
|
|
$this->assertSame($element12, $element121->getParent());
|
|
|
|
$this->assertSame($parent, $element2->getParent());
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testMassCreationErrors(): void
|
2019-11-09 16:14:57 +01:00
|
|
|
{
|
|
|
|
$errors = [];
|
|
|
|
//Node 1 and Node 2 are created in datafixtures, so their attemp to create them again must fail.
|
2023-01-28 23:24:45 +01:00
|
|
|
$lines = "Test 1\nNode 1\nNode 2";
|
2019-11-09 16:14:57 +01:00
|
|
|
$results = $this->service->massCreation($lines, AttachmentType::class, null, $errors);
|
|
|
|
$this->assertCount(1, $results);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame('Test 1', $results[0]->getName());
|
2019-11-09 16:14:57 +01:00
|
|
|
$this->assertCount(2, $errors);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame('Node 1', $errors[0]['entity']->getName());
|
2019-11-09 16:14:57 +01:00
|
|
|
}
|
2023-03-12 21:43:40 +01:00
|
|
|
|
|
|
|
public function formatDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['csv', 'csv'],
|
|
|
|
['csv', 'CSV'],
|
|
|
|
['xml', 'Xml'],
|
|
|
|
['json', 'json'],
|
|
|
|
['yaml', 'yml'],
|
|
|
|
['yaml', 'YAML'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider formatDataProvider
|
|
|
|
*/
|
|
|
|
public function testDetermineFormat(string $expected, string $extension): void
|
|
|
|
{
|
|
|
|
$this->assertSame($expected, $this->service->determineFormat($extension));
|
|
|
|
}
|
2023-03-14 00:17:13 +01:00
|
|
|
|
|
|
|
public function testImportStringParts(): void
|
|
|
|
{
|
|
|
|
$input = <<<EOT
|
|
|
|
name,description,notes,manufacturer
|
|
|
|
Test 1,Test 1 description,Test 1 notes,Test 1 manufacturer
|
|
|
|
Test 2,Test 2 description,Test 2 notes,Test 2 manufacturer
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
$category = new Category();
|
|
|
|
$category->setName('Test category');
|
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
$results = $this->service->importString($input, [
|
|
|
|
'class' => Part::class,
|
|
|
|
'format' => 'csv',
|
|
|
|
'csv_delimiter' => ',',
|
|
|
|
'create_unknown_datastructures' => true,
|
|
|
|
'part_category' => $category,
|
|
|
|
], $errors);
|
|
|
|
|
|
|
|
$this->assertCount(2, $results);
|
|
|
|
//No errors must be present
|
|
|
|
$this->assertEmpty($errors);
|
|
|
|
$this->assertContainsOnlyInstancesOf(Part::class, $results);
|
|
|
|
|
|
|
|
$this->assertSame('Test 1', $results[0]->getName());
|
|
|
|
$this->assertSame('Test 1 description', $results[0]->getDescription());
|
|
|
|
$this->assertSame('Test 1 notes', $results[0]->getComment());
|
|
|
|
$this->assertSame('Test 1 manufacturer', $results[0]->getManufacturer()->getName());
|
|
|
|
$this->assertSame($category, $results[0]->getCategory());
|
|
|
|
|
|
|
|
$this->assertSame('Test 2', $results[1]->getName());
|
|
|
|
$this->assertSame('Test 2 description', $results[1]->getDescription());
|
|
|
|
$this->assertSame('Test 2 notes', $results[1]->getComment());
|
|
|
|
$this->assertSame('Test 2 manufacturer', $results[1]->getManufacturer()->getName());
|
|
|
|
$this->assertSame($category, $results[1]->getCategory());
|
|
|
|
|
|
|
|
$input = <<<EOT
|
|
|
|
[{"name":"Test 1","description":"Test 1 description","notes":"Test 1 notes","manufacturer":"Test 1 manufacturer", "tags": "test,test2"},{"name":"Test 2","description":"Test 2 description","notes":"Test 2 notes","manufacturer":"Test 2 manufacturer", "manufacturing_status": "invalid"}]
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
$results = $this->service->importString($input, [
|
|
|
|
'class' => Part::class,
|
|
|
|
'format' => 'json',
|
|
|
|
'create_unknown_datastructures' => true,
|
|
|
|
'part_category' => $category,
|
|
|
|
], $errors);
|
|
|
|
|
|
|
|
//We have 2 elements, but one is invalid
|
|
|
|
$this->assertCount(1, $results);
|
|
|
|
$this->assertCount(1, $errors);
|
|
|
|
$this->assertContainsOnlyInstancesOf(Part::class, $results);
|
|
|
|
|
|
|
|
//Check the format of the error
|
|
|
|
$error = reset($errors);
|
|
|
|
$this->assertInstanceOf(Part::class, $error['entity']);
|
|
|
|
$this->assertSame('Test 2', $error['entity']->getName());
|
|
|
|
$this->assertContainsOnlyInstancesOf(ConstraintViolation::class, $error['violations']);
|
|
|
|
//Element name must be element name
|
|
|
|
$this->assertArrayHasKey('Test 2', $errors);
|
|
|
|
|
|
|
|
//Check the valid element
|
|
|
|
$this->assertSame('Test 1', $results[0]->getName());
|
|
|
|
$this->assertSame('Test 1 description', $results[0]->getDescription());
|
|
|
|
$this->assertSame('Test 1 notes', $results[0]->getComment());
|
|
|
|
$this->assertSame('Test 1 manufacturer', $results[0]->getManufacturer()->getName());
|
|
|
|
$this->assertSame($category, $results[0]->getCategory());
|
|
|
|
$this->assertSame('test,test2', $results[0]->getTags());
|
|
|
|
}
|
2020-01-04 20:24:09 +01:00
|
|
|
}
|