diff --git a/src/Serializer/PartNormalizer.php b/src/Serializer/PartNormalizer.php index 0a513283..dbad5aa2 100644 --- a/src/Serializer/PartNormalizer.php +++ b/src/Serializer/PartNormalizer.php @@ -99,7 +99,7 @@ class PartNormalizer implements NormalizerInterface, DenormalizerInterface, Cach $this->normalizeKeys($data); //Empty IPN should be null, or we get a constraint error - if ($data['ipn'] === '') { + if (isset($data['ipn']) && $data['ipn'] === '') { $data['ipn'] = null; } diff --git a/tests/Services/ImportExportSystem/EntityImporterTest.php b/tests/Services/ImportExportSystem/EntityImporterTest.php index 57f8784e..7f386e6b 100644 --- a/tests/Services/ImportExportSystem/EntityImporterTest.php +++ b/tests/Services/ImportExportSystem/EntityImporterTest.php @@ -23,10 +23,13 @@ declare(strict_types=1); namespace App\Tests\Services\ImportExportSystem; use App\Entity\Attachments\AttachmentType; +use App\Entity\Parts\Category; +use App\Entity\Parts\Part; use App\Entity\UserSystem\User; use App\Services\Formatters\AmountFormatter; use App\Services\ImportExportSystem\EntityImporter; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\Validator\ConstraintViolation; /** * @group DB @@ -171,4 +174,75 @@ EOT; { $this->assertSame($expected, $this->service->determineFormat($extension)); } + + public function testImportStringParts(): void + { + $input = <<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 = <<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()); + } }