. */ namespace App\Tests\Serializer; use PHPUnit\Framework\Attributes\Group; use App\Entity\Parts\Category; use App\Serializer\StructuralElementDenormalizer; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; final class StructuralElementDenormalizerTest extends WebTestCase { /** @var StructuralElementDenormalizer */ protected $service; protected function setUp(): void { //Get a service instance. self::bootKernel(); $this->service = self::getContainer()->get(StructuralElementDenormalizer::class); //We need to inject the serializer into the normalizer, as we use it directly $serializer = self::getContainer()->get('serializer'); $this->service->setDenormalizer($serializer); } public function testSupportsDenormalization(): void { $this->assertFalse($this->service->supportsDenormalization('doesnt_matter', Category::class, 'json', ['groups' => ['import']])); $this->assertFalse($this->service->supportsDenormalization(['name' => 'Test'], Category::class, 'json', ['groups' => ['simple']])); //Denormalizer should only be active, when we use the import function and partdb_import is set $this->assertFalse($this->service->supportsDenormalization(['name' => 'Test'], Category::class, 'json', ['groups' => ['import']])); $this->assertTrue($this->service->supportsDenormalization(['name' => 'Test'], Category::class, 'json', ['groups' => ['import'], 'partdb_import' => true])); } #[Group('DB')] public function testDenormalize(): void { //Check that we retrieve DB elements via the name $data = ['name' => 'Node 1']; $result = $this->service->denormalize($data, Category::class, 'json', ['groups' => ['import']]); $this->assertInstanceOf(Category::class, $result); $this->assertSame('Node 1', $result->getName()); $this->assertNotNull($result->getID()); //ID should be set, because we retrieved the element from the DB //Check that we can retrieve nested DB elements $data = ['name' => 'Node 1.1', 'parent' => ['name' => 'Node 1']]; $result = $this->service->denormalize($data, Category::class, 'json', ['groups' => ['import']]); $this->assertInstanceOf(Category::class, $result); $this->assertSame('Node 1.1', $result->getName()); $this->assertSame('Node 1', $result->getParent()->getName()); $this->assertNotNull($result->getID()); //ID should be set, because we retrieved the element from the DB $this->assertNotNull($result->getParent()->getID()); //ID should be set, because we retrieved the element from the DB //Check that we can create new elements $data = ['name' => 'New Node 1.1', 'parent' => ['name' => 'New Node 1']]; $result = $this->service->denormalize($data, Category::class, 'json', ['groups' => ['import']]); $this->assertInstanceOf(Category::class, $result); $this->assertSame('New Node 1.1', $result->getName()); $this->assertSame('New Node 1', $result->getParent()->getName()); $this->assertNull($result->getID()); //ID should be null, because we created a new element //Check that when we retrieve this element again, we get the same instance $result2 = $this->service->denormalize($data, Category::class, 'json', ['groups' => ['import']]); $this->assertSame($result, $result2); } public function testDenormalizeViaChildren(): void { $data = ['name' => 'Node', 'children' => [ ['name' => 'A', 'children' => [['name' => '1'], ['name' => '2']]], ['name' => 'B', 'children' => [['name' => '1'], ['name' => '2']]], ['name' => 'C', 'children' => [['name' => '1'], ['name' => '2'], ['name' => '3']]], ] ]; $result = $this->service->denormalize($data, Category::class, 'json', ['groups' => ['import', 'include_children']]); $this->assertInstanceOf(Category::class, $result); $this->assertCount(3, $result->getChildren()); $this->assertSame('A', $result->getChildren()[0]->getName()); $this->assertSame('B', $result->getChildren()[1]->getName()); $this->assertSame('C', $result->getChildren()[2]->getName()); //Parents should be set correctly $this->assertSame($result, $result->getChildren()[0]->getParent()); $this->assertSame($result, $result->getChildren()[1]->getParent()); $this->assertSame($result, $result->getChildren()[2]->getParent()); $this->assertCount(2, $result->getChildren()[0]->getChildren()); $this->assertSame('1', $result->getChildren()[0]->getChildren()[0]->getName()); $this->assertSame('2', $result->getChildren()[0]->getChildren()[1]->getName()); //Parents should be set correctly $this->assertSame($result->getChildren()[0], $result->getChildren()[0]->getChildren()[0]->getParent()); $this->assertSame($result->getChildren()[0], $result->getChildren()[0]->getChildren()[1]->getParent()); $this->assertCount(2, $result->getChildren()[1]->getChildren()); $this->assertSame('1', $result->getChildren()[1]->getChildren()[0]->getName()); $this->assertSame('2', $result->getChildren()[1]->getChildren()[1]->getName()); //Must be different instances than the children of A, because we create new elements for the same path, if we don't have them in the DB $this->assertNotSame($result->getChildren()[0]->getChildren()[0], $result->getChildren()[1]->getChildren()[0]); } }