From 2f5073b7571c9a03b2eda69e19cf8ae0a8cdf79d Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 20 Mar 2020 13:23:58 +0100 Subject: [PATCH] test serializing entity that uses Discriminator and extends some base model class #878 --- .../Discriminator/Serialization/Entity.php | 37 ++++++++++++++ .../Serialization/ExtendedUser.php | 28 +++++++++++ .../Discriminator/Serialization/User.php | 49 +++++++++++++++++++ tests/Serializer/BaseSerializationTest.php | 28 +++++++++++ tests/Serializer/JsonSerializationTest.php | 3 ++ tests/Serializer/xml/user_discriminator.xml | 4 ++ .../xml/user_discriminator_array.xml | 9 ++++ .../xml/user_discriminator_extended.xml | 4 ++ 8 files changed, 162 insertions(+) create mode 100644 tests/Fixtures/Discriminator/Serialization/Entity.php create mode 100644 tests/Fixtures/Discriminator/Serialization/ExtendedUser.php create mode 100644 tests/Fixtures/Discriminator/Serialization/User.php create mode 100644 tests/Serializer/xml/user_discriminator.xml create mode 100644 tests/Serializer/xml/user_discriminator_array.xml create mode 100644 tests/Serializer/xml/user_discriminator_extended.xml diff --git a/tests/Fixtures/Discriminator/Serialization/Entity.php b/tests/Fixtures/Discriminator/Serialization/Entity.php new file mode 100644 index 000000000..ad464c383 --- /dev/null +++ b/tests/Fixtures/Discriminator/Serialization/Entity.php @@ -0,0 +1,37 @@ +id = $id; + } + + /** + * @JMS\VirtualProperty() + * @JMS\SerializedName("entityName") + * @JMS\Groups({"entity.identification"}) + * @throws ReflectionException + */ + public function getEntityName(): string { + $reflect = new ReflectionClass($this); + return $reflect->getShortName(); + } +} diff --git a/tests/Fixtures/Discriminator/Serialization/ExtendedUser.php b/tests/Fixtures/Discriminator/Serialization/ExtendedUser.php new file mode 100644 index 000000000..7b6bef1b0 --- /dev/null +++ b/tests/Fixtures/Discriminator/Serialization/ExtendedUser.php @@ -0,0 +1,28 @@ +extendAttribute = $extendAttribute; + } +} diff --git a/tests/Fixtures/Discriminator/Serialization/User.php b/tests/Fixtures/Discriminator/Serialization/User.php new file mode 100644 index 000000000..cdb92fb2c --- /dev/null +++ b/tests/Fixtures/Discriminator/Serialization/User.php @@ -0,0 +1,49 @@ +name = $name; + $this->description = $description; + } + + +} diff --git a/tests/Serializer/BaseSerializationTest.php b/tests/Serializer/BaseSerializationTest.php index a343e8b75..f1d02a72f 100644 --- a/tests/Serializer/BaseSerializationTest.php +++ b/tests/Serializer/BaseSerializationTest.php @@ -51,6 +51,8 @@ use JMS\Serializer\Tests\Fixtures\Discriminator\ImagePost; use JMS\Serializer\Tests\Fixtures\Discriminator\Moped; use JMS\Serializer\Tests\Fixtures\Discriminator\Post; +use JMS\Serializer\Tests\Fixtures\Discriminator\Serialization\ExtendedUser; +use JMS\Serializer\Tests\Fixtures\Discriminator\Serialization\User; use JMS\Serializer\Tests\Fixtures\DiscriminatorGroup\Car as DiscriminatorGroupCar; use JMS\Serializer\Tests\Fixtures\ExclusionStrategy\AlwaysExcludeExclusionStrategy; use JMS\Serializer\Tests\Fixtures\FirstClassListCollection; @@ -1312,6 +1314,32 @@ public function testPolymorphicObjectsWithGroup() ); } + /** + * Test serializing entity that uses Discriminator and extends some base model class + */ + public function testDiscrimatorObjects() + { + $u1 = new User(5, 'userName', 'userDesc'); + $u2 = new ExtendedUser(5, 'userName', 'userDesc', 'extednedContent'); + $arr = new ArrayCollection([$u1, $u2]); + $context = SerializationContext::create()->setGroups(['entity.identification']); + + self::assertEquals( + $this->getContent('user_discriminator_extended'), + $this->serialize($u2, $context) + ); + + self::assertEquals( + $this->getContent('user_discriminator'), + $this->serialize($u1, $context) + ); + + self::assertEquals( + $this->getContent('user_discriminator_array'), + $this->serialize($arr, $context) + ); + } + /** * @group polymorphic */ diff --git a/tests/Serializer/JsonSerializationTest.php b/tests/Serializer/JsonSerializationTest.php index 09f88ddfe..e7709c6e7 100644 --- a/tests/Serializer/JsonSerializationTest.php +++ b/tests/Serializer/JsonSerializationTest.php @@ -125,6 +125,9 @@ protected function getContent($key) $outputs['array_iterator'] = '{"iterator":{"foo":"bar","bar":"foo"}}'; $outputs['generator'] = '{"generator":{"foo":"bar","bar":"foo"}}'; $outputs['ParentNoMetadataChildObject'] = '{"bar":"John"}'; + $outputs['user_discriminator_array'] = '[{"entityName": "User"},{"entityName": "ExtendedUser"}]'; + $outputs['user_discriminator'] = '{"entityName": "User"}'; + $outputs['user_discriminator_extended'] = '{"entityName": "ExtendedUser"}'; } if (!isset($outputs[$key])) { diff --git a/tests/Serializer/xml/user_discriminator.xml b/tests/Serializer/xml/user_discriminator.xml new file mode 100644 index 000000000..37ad181ce --- /dev/null +++ b/tests/Serializer/xml/user_discriminator.xml @@ -0,0 +1,4 @@ + + + + diff --git a/tests/Serializer/xml/user_discriminator_array.xml b/tests/Serializer/xml/user_discriminator_array.xml new file mode 100644 index 000000000..59c89e7e4 --- /dev/null +++ b/tests/Serializer/xml/user_discriminator_array.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/tests/Serializer/xml/user_discriminator_extended.xml b/tests/Serializer/xml/user_discriminator_extended.xml new file mode 100644 index 000000000..6abf8c07d --- /dev/null +++ b/tests/Serializer/xml/user_discriminator_extended.xml @@ -0,0 +1,4 @@ + + + +