Skip to content

Commit

Permalink
test serializing entity that uses Discriminator and extends some base…
Browse files Browse the repository at this point in the history
… model class #878
  • Loading branch information
kevin committed Mar 20, 2020
1 parent 25a829b commit 2f5073b
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/Fixtures/Discriminator/Serialization/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace JMS\Serializer\Tests\Fixtures\Discriminator\Serialization;

use JMS\Serializer\Annotation as JMS;
use ReflectionClass;
use ReflectionException;

abstract class Entity
{
/**
* @JMS\Type("int")
* @JMS\Groups({"base"})
* @var int
*/
public $id;

/**
* Entity constructor.
* @param int $id
*/
public function __construct($id)
{
$this->id = $id;
}

/**
* @JMS\VirtualProperty()
* @JMS\SerializedName("entityName")
* @JMS\Groups({"entity.identification"})
* @throws ReflectionException
*/
public function getEntityName(): string {
$reflect = new ReflectionClass($this);
return $reflect->getShortName();
}
}
28 changes: 28 additions & 0 deletions tests/Fixtures/Discriminator/Serialization/ExtendedUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace JMS\Serializer\Tests\Fixtures\Discriminator\Serialization;

use JMS\Serializer\Annotation as JMS;

class ExtendedUser extends User
{
/**
* @JMS\Type("string")
* @JMS\Groups({"base"})
* @var string
*/
public $extendAttribute;

/**
* ExtendedUser constructor.
*
* @param int $id
* @param string $name
* @param string $description
* @param string $extendAttribute
*/
public function __construct($id, $name, $description, $extendAttribute)
{
parent::__construct($id, $name, $description);
$this->extendAttribute = $extendAttribute;
}
}
49 changes: 49 additions & 0 deletions tests/Fixtures/Discriminator/Serialization/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php


namespace JMS\Serializer\Tests\Fixtures\Discriminator\Serialization;

use JMS\Serializer\Annotation as JMS;

/**
* Class User
*
* @package JMS\Serializer\Tests\Fixtures\Discriminator\Serialization
* @JMS\Discriminator(field = "entityName",
* groups={"entity.identification"},
* map = {
* "User": "JMS\Serializer\Tests\Fixtures\Discriminator\Serialization\User",
* "ExtendedUser": "JMS\Serializer\Tests\Fixtures\Discriminator\Serialization\ExtendedUser"
* })
*/
class User extends Entity
{
/**
* @JMS\Type("string")
* @JMS\Groups({"base"})
* @var string
*/
public $name;
/**
* @JMS\Type("string")
* @JMS\Groups({"base"})
* @var string
*/
public $description;

/**
* User constructor.
*
* @param $id
* @param string $name
* @param string $description
*/
public function __construct($id, $name, $description)
{
parent::__construct($id);
$this->name = $name;
$this->description = $description;
}


}
28 changes: 28 additions & 0 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Serializer/xml/user_discriminator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<entry><![CDATA[User]]></entry>
</result>
9 changes: 9 additions & 0 deletions tests/Serializer/xml/user_discriminator_array.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<entry>
<entry><![CDATA[User]]></entry>
</entry>
<entry>
<entry><![CDATA[ExtendedUser]]></entry>
</entry>
</result>
4 changes: 4 additions & 0 deletions tests/Serializer/xml/user_discriminator_extended.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<entry><![CDATA[ExtendedUser]]></entry>
</result>

0 comments on commit 2f5073b

Please sign in to comment.