Added possibility to create nested structures of elements using Mass Import

This commit is contained in:
Jan Böhmer 2023-01-28 23:24:45 +01:00
parent 22950f2476
commit 07f95bc6ea
7 changed files with 126 additions and 12 deletions

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Tests\Services\ImportExportSystem;
use App\Entity\Attachments\AttachmentType;
use App\Entity\UserSystem\User;
use App\Services\Formatters\AmountFormatter;
use App\Services\ImportExportSystem\EntityImporter;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -54,7 +55,7 @@ class EntityImporterTest extends WebTestCase
$this->assertEmpty($errors);
$errors = [];
$lines = "Test 1 \n Test 2 \n Test 3";
$lines = "Test 1\nTest 2 \nTest 3";
$results = $this->service->massCreation($lines, AttachmentType::class, null, $errors);
$this->assertCount(0, $errors);
$this->assertCount(3, $results);
@ -72,11 +73,78 @@ class EntityImporterTest extends WebTestCase
$this->assertSame($parent, $results[0]->getParent());
}
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());
}
public function testMassCreationErrors(): void
{
$errors = [];
//Node 1 and Node 2 are created in datafixtures, so their attemp to create them again must fail.
$lines = "Test 1 \n Node 1 \n Node 2";
$lines = "Test 1\nNode 1\nNode 2";
$results = $this->service->massCreation($lines, AttachmentType::class, null, $errors);
$this->assertCount(1, $results);
$this->assertSame('Test 1', $results[0]->getName());