diff --git a/src/Symfony/Component/Serializer/Annotation/Alias.php b/src/Symfony/Component/Serializer/Annotation/Alias.php new file mode 100644 index 0000000000000..9de4128816ee4 --- /dev/null +++ b/src/Symfony/Component/Serializer/Annotation/Alias.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Annotation; + +use Symfony\Component\Serializer\Exception\InvalidArgumentException; + +/** + * Annotation class for @Alias(). + * + * @Annotation + * @Target({"PROPERTY", "METHOD"}) + * + * @author Kévin Dunglas + */ +class Alias +{ + /** + * @var string + */ + private $name; + + /** + * @param array $data + * + * @throws InvalidArgumentException + */ + public function __construct(array $data) + { + if (!isset($data['value']) || !$data['value']) { + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', get_class($this))); + } + + if (!is_string($data['value'])) { + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string.', get_class($this))); + } + + $this->name = $data['value']; + } + + /** + * Gets name. + * + * @return string + */ + public function getName() + { + return $this->name; + } +} diff --git a/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php b/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php index 7a1d3db94a809..8b0f6dba00c5c 100644 --- a/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php +++ b/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php @@ -36,6 +36,15 @@ class AttributeMetadata implements AttributeMetadataInterface */ public $groups = array(); + /** + * @var string|null + * + * @internal This property is public in order to reduce the size of the + * class' serialized representation. Do not access it. Use + * {@link getAlias()} instead. + */ + public $alias; + /** * Constructs a metadata for the given attribute. * @@ -72,6 +81,22 @@ public function getGroups() return $this->groups; } + /** + * {@inheritdoc} + */ + public function setAlias($alias) + { + $this->alias = $alias; + } + + /** + * {@inheritdoc} + */ + public function getAlias() + { + return $this->alias; + } + /** * {@inheritdoc} */ @@ -80,6 +105,10 @@ public function merge(AttributeMetadataInterface $attributeMetadata) foreach ($attributeMetadata->getGroups() as $group) { $this->addGroup($group); } + + if (!$this->alias) { + $this->alias = $attributeMetadata->getAlias(); + } } /** @@ -89,6 +118,6 @@ public function merge(AttributeMetadataInterface $attributeMetadata) */ public function __sleep() { - return array('name', 'groups'); + return array('name', 'groups', 'alias'); } } diff --git a/src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php b/src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php index 0701a58b56257..05669890cb04d 100644 --- a/src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php +++ b/src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php @@ -41,6 +41,20 @@ public function addGroup($group); */ public function getGroups(); + /** + * Sets the normalization alias of this attribute. + * + * @param string|null $alias + */ + public function setAlias($alias); + + /** + * Gets the normalization alias of this attribute. + * + * @return string|null + */ + public function getAlias(); + /** * Merges an {@see AttributeMetadataInterface} with in the current one. * diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php index 801c317767ac0..c85319856ffcb 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Serializer\Mapping\Loader; use Doctrine\Common\Annotations\Reader; +use Symfony\Component\Serializer\Annotation\Alias; use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Exception\MappingException; use Symfony\Component\Serializer\Mapping\AttributeMetadata; @@ -55,13 +56,17 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) } if ($property->getDeclaringClass()->name === $className) { - foreach ($this->reader->getPropertyAnnotations($property) as $groups) { - if ($groups instanceof Groups) { - foreach ($groups->getGroups() as $group) { + foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { + if ($annotation instanceof Groups) { + foreach ($annotation->getGroups() as $group) { $attributesMetadata[$property->name]->addGroup($group); } } + if ($annotation instanceof Alias) { + $attributesMetadata[$property->name]->setAlias($annotation->getName()); + } + $loaded = true; } } @@ -70,25 +75,29 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) foreach ($reflectionClass->getMethods() as $method) { if ($method->getDeclaringClass()->name === $className) { foreach ($this->reader->getMethodAnnotations($method) as $groups) { + if (!preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) { + continue; + } + + $attributeName = lcfirst($matches[2]); + + if (isset($attributesMetadata[$attributeName])) { + $attributeMetadata = $attributesMetadata[$attributeName]; + } else { + $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName); + $classMetadata->addAttributeMetadata($attributeMetadata); + } + if ($groups instanceof Groups) { - if (preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) { - $attributeName = lcfirst($matches[2]); - - if (isset($attributesMetadata[$attributeName])) { - $attributeMetadata = $attributesMetadata[$attributeName]; - } else { - $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName); - $classMetadata->addAttributeMetadata($attributeMetadata); - } - - foreach ($groups->getGroups() as $group) { - $attributeMetadata->addGroup($group); - } - } else { - throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name)); + foreach ($groups->getGroups() as $group) { + $attributeMetadata->addGroup($group); } } + if ($annotation instanceof Alias) { + $attributeMetadata[$property->name]->setAlias($annotation->getName()); + } + $loaded = true; } } diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php index 0da2f7d690ff6..f7c77fbecd409 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php @@ -62,6 +62,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) foreach ($attribute->group as $group) { $attributeMetadata->addGroup((string) $group); } + + if ($alias = (string) $attribute->alias) { + $attributeMetadata->setAlias($alias); + } } return true; diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php index 1c5d5e82e63f0..23b6275760373 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php @@ -78,6 +78,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) $attributeMetadata->addGroup($group); } } + + if (isset($data['alias'])) { + $attributeMetadata->setAlias($data['alias']); + } } } diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd b/src/Symfony/Component/Serializer/Mapping/Loader/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd index cd5a9a9f0df82..19b0bb8f0df1d 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd +++ b/src/Symfony/Component/Serializer/Mapping/Loader/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd @@ -44,11 +44,14 @@ - + + + + diff --git a/src/Symfony/Component/Serializer/NameConverter/AliasNameConverter.php b/src/Symfony/Component/Serializer/NameConverter/AliasNameConverter.php new file mode 100644 index 0000000000000..4f96c31fcf647 --- /dev/null +++ b/src/Symfony/Component/Serializer/NameConverter/AliasNameConverter.php @@ -0,0 +1,80 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\NameConverter; + +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; + +/** + * Converts name of properties using the alias specified in the attribute metadata. + * + * @author Kévin Dunglas + */ +class AliasNameConverter implements NameConverterInterface +{ + /** + * @var ClassMetadataFactoryInterface + */ + private $classMetadataFactory; + + public function __construct(ClassMetadataFactoryInterface $classMetadataFactory) + { + $this->classMetadataFactory = $classMetadataFactory; + } + + /** + * {@inheritdoc} + */ + public function normalize($object, $propertyName) + { + $attributesMetadata = $this->getAttributesMetadata($object); + + if (isset($attributesMetadata[$propertyName]) && $alias = $attributesMetadata[$propertyName]->getAlias()) { + return $alias; + } + + return $propertyName; + } + + /** + * {@inheritdoc} + */ + public function denormalize($object, $propertyName) + { + $attributesMetadata = $this->getAttributesMetadata($object); + + // TODO: cache that + foreach ($attributesMetadata as $attributeMetadata) { + $alias = $attributeMetadata->getAlias(); + if ($propertyName === $alias) { + return $alias; + } + } + + return $propertyName; + } + + /** + * Gets attributes metadata. + * + * @param string|object $class + * + * @return array + */ + private function getAttributesMetadata($class) + { + if (is_object($class)) { + $class = get_class($class); + } + + return $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata(); + } +} diff --git a/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php b/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php index 27f4eee59a71a..96383199599eb 100644 --- a/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php +++ b/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Serializer\NameConverter; /** - * CamelCase to Underscore name converter. + * Converts names of properties from CamelCase to Underscore. * * @author Kévin Dunglas */ @@ -40,7 +40,7 @@ public function __construct(array $attributes = null, $lowerCamelCase = true) /** * {@inheritdoc} */ - public function normalize($propertyName) + public function normalize($object, $propertyName) { if (null === $this->attributes || in_array($propertyName, $this->attributes)) { $snakeCasedName = ''; @@ -63,7 +63,7 @@ public function normalize($propertyName) /** * {@inheritdoc} */ - public function denormalize($propertyName) + public function denormalize($object, $propertyName) { $camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) { return ('.' === $match[1] ? '_' : '').strtoupper($match[2]); diff --git a/src/Symfony/Component/Serializer/NameConverter/NameConverterInterface.php b/src/Symfony/Component/Serializer/NameConverter/NameConverterInterface.php index 306e6541218e0..bedf8c16bcb52 100644 --- a/src/Symfony/Component/Serializer/NameConverter/NameConverterInterface.php +++ b/src/Symfony/Component/Serializer/NameConverter/NameConverterInterface.php @@ -21,16 +21,20 @@ interface NameConverterInterface /** * Converts a property name to its normalized value. * - * @param string $propertyName + * @param string|object $class + * @param string $propertyName + * * @return string */ - public function normalize($propertyName); + public function normalize($class, $propertyName); /** * Converts a property name to its denormalized value. * - * @param string $propertyName + * @param string|object $class + * @param string $propertyName + * * @return string */ - public function denormalize($propertyName); + public function denormalize($class, $propertyName); } diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 78c82ff990a10..eadcdd8c6f90d 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -230,11 +230,11 @@ protected function handleCircularReference($object) * * @return string */ - protected function formatAttribute($attributeName) + protected function formatAttribute($object, $attributeName) { @trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED); - return $this->nameConverter ? $this->nameConverter->normalize($attributeName) : $attributeName; + return $this->nameConverter ? $this->nameConverter->normalize($object, $attributeName) : $attributeName; } /** @@ -320,7 +320,7 @@ protected function instantiateObject(array $data, $class, array &$context, \Refl $params = array(); foreach ($constructorParameters as $constructorParameter) { $paramName = $constructorParameter->name; - $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName; + $key = $this->nameConverter ? $this->nameConverter->denormalize($class, $paramName) : $paramName; $allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes); $ignored = in_array($paramName, $this->ignoredAttributes); diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index 9e9eda150f662..643c52725f616 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -79,7 +79,7 @@ public function normalize($object, $format = null, array $context = array()) } if ($this->nameConverter) { - $attributeName = $this->nameConverter->normalize($attributeName); + $attributeName = $this->nameConverter->normalize($object, $attributeName); } $attributes[$attributeName] = $attributeValue; @@ -104,7 +104,7 @@ public function denormalize($data, $class, $format = null, array $context = arra foreach ($normalizedData as $attribute => $value) { if ($this->nameConverter) { - $attribute = $this->nameConverter->denormalize($attribute); + $attribute = $this->nameConverter->denormalize($object, $attribute); } $allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes); diff --git a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php index ba84ac717f075..cda4ffcbddf61 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php @@ -112,7 +112,7 @@ public function normalize($object, $format = null, array $context = array()) } if ($this->nameConverter) { - $attribute = $this->nameConverter->normalize($attribute); + $attribute = $this->nameConverter->normalize($object, $attribute); } $data[$attribute] = $attributeValue; @@ -142,7 +142,7 @@ public function denormalize($data, $class, $format = null, array $context = arra foreach ($normalizedData as $attribute => $value) { if ($this->nameConverter) { - $attribute = $this->nameConverter->denormalize($attribute); + $attribute = $this->nameConverter->denormalize($object, $attribute); } $allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes); diff --git a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php index dbe54e4024579..ff49872cf873e 100644 --- a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php @@ -78,7 +78,7 @@ public function normalize($object, $format = null, array $context = array()) $propertyName = $property->name; if ($this->nameConverter) { - $propertyName = $this->nameConverter->normalize($propertyName); + $propertyName = $this->nameConverter->normalize($object, $propertyName); } $attributes[$propertyName] = $attributeValue; @@ -102,7 +102,7 @@ public function denormalize($data, $class, $format = null, array $context = arra foreach ($data as $propertyName => $value) { if ($this->nameConverter) { - $propertyName = $this->nameConverter->denormalize($propertyName); + $propertyName = $this->nameConverter->denormalize($object, $propertyName); } $allowed = $allowedAttributes === false || in_array($propertyName, $allowedAttributes); diff --git a/src/Symfony/Component/Serializer/Tests/Annotation/AliasTest.php b/src/Symfony/Component/Serializer/Tests/Annotation/AliasTest.php new file mode 100644 index 0000000000000..f5d9501069fed --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Annotation/AliasTest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Annotation; + +use Symfony\Component\Serializer\Annotation\Alias; +use Symfony\Component\Serializer\Exception\InvalidArgumentException; + +/** + * @author Kévin Dunglas + */ +class AliasTest extends \PHPUnit_Framework_TestCase +{ + /** + * @expectedException InvalidArgumentException + */ + public function testEmptyParameter() + { + new Alias(array('value' => '')); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testNotAStringGroupsParameter() + { + new Alias(array('value' => array())); + } + + public function testGroupsParameters() + { + $validData = 'a'; + + $alias = new Alias(array('value' => $validData)); + $this->assertEquals($validData, $alias->getName()); + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/MappedDummy.php similarity index 89% rename from src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummy.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/MappedDummy.php index 37bfa7eb3f075..d306e23fd0d8b 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummy.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/MappedDummy.php @@ -11,15 +11,17 @@ namespace Symfony\Component\Serializer\Tests\Fixtures; +use Symfony\Component\Serializer\Annotation\Alias; use Symfony\Component\Serializer\Annotation\Groups; /** * @author Kévin Dunglas */ -class GroupDummy extends GroupDummyParent implements GroupDummyInterface +class MappedDummy extends MappedDummyParent implements MappedDummyInterface { /** * @Groups({"a"}) + * @Alias("myAlias") */ private $foo; /** diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyInterface.php b/src/Symfony/Component/Serializer/Tests/Fixtures/MappedDummyInterface.php similarity index 94% rename from src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyInterface.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/MappedDummyInterface.php index a60629e6dd509..76bd73c65538b 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyInterface.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/MappedDummyInterface.php @@ -16,7 +16,7 @@ /** * @author Kévin Dunglas */ -interface GroupDummyInterface +interface MappedDummyInterface { /** * @Groups({"a", "name_converter"}) diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyParent.php b/src/Symfony/Component/Serializer/Tests/Fixtures/MappedDummyParent.php similarity index 97% rename from src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyParent.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/MappedDummyParent.php index dd24233993b9b..72c7bc2a47b11 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyParent.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/MappedDummyParent.php @@ -16,7 +16,7 @@ /** * @author Kévin Dunglas */ -class GroupDummyParent +class MappedDummyParent { /** * @Groups({"a"}) diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml index 6e95aaf72118b..ea5dfb47dc740 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml @@ -4,7 +4,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"> - + group1 group2 @@ -12,6 +12,7 @@ group2 + myBar diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml index e855ea472b3d6..cc0f1431f2618 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml @@ -1,6 +1,7 @@ -Symfony\Component\Serializer\Tests\Fixtures\GroupDummy: +Symfony\Component\Serializer\Tests\Fixtures\MappedDummy: attributes: foo: groups: ['group1', 'group2'] bar: groups: ['group2'] + alias: 'myBar' diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/AttributeMetadataTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/AttributeMetadataTest.php index f22746bc5de75..b3075809a66ce 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/AttributeMetadataTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/AttributeMetadataTest.php @@ -49,9 +49,24 @@ public function testMerge() $attributeMetadata2 = new AttributeMetadata('a2'); $attributeMetadata2->addGroup('a'); $attributeMetadata2->addGroup('c'); + $attributeMetadata2->setAlias('alias'); $attributeMetadata1->merge($attributeMetadata2); $this->assertEquals(array('a', 'b', 'c'), $attributeMetadata1->getGroups()); + $this->assertEquals('alias', $attributeMetadata1->getAlias()); + } + + public function testMergeWithExistingAlias() + { + $attributeMetadata1 = new AttributeMetadata('a1'); + $attributeMetadata1->setAlias('a1'); + + $attributeMetadata2 = new AttributeMetadata('a2'); + $attributeMetadata2->setAlias('a2'); + + $attributeMetadata1->merge($attributeMetadata2); + + $this->assertEquals('a1', $attributeMetadata1->getAlias()); } } diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/ClassMetadataTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/ClassMetadataTest.php index 90017580ac703..629c17b788d23 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/ClassMetadataTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/ClassMetadataTest.php @@ -62,4 +62,21 @@ public function testMerge() $this->assertEquals(array('a1' => $ac1), $classMetadata2->getAttributesMetadata()); } + + public function testSerialize() + { + $classMetadata = new ClassMetadata('a'); + + $a1 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface'); + $a1->method('getName')->willReturn('b1'); + + $a2 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface'); + $a2->method('getName')->willReturn('b2'); + + $classMetadata->addAttributeMetadata($a1); + $classMetadata->addAttributeMetadata($a2); + + $serialized = serialize($classMetadata); + $this->assertEquals($classMetadata, unserialize($serialized)); + } } diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php index 484d062f22375..d71e8b495bbf6 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php @@ -38,14 +38,14 @@ public function testInterface() public function testLoadClassMetadataReturnsTrueIfSuccessful() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MappedDummy'); $this->assertTrue($this->loader->loadClassMetadata($classMetadata)); } public function testLoadClassMetadata() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MappedDummy'); $this->loader->loadClassMetadata($classMetadata); $this->assertEquals(TestClassMetadataFactory::createClassMetadata(), $classMetadata); @@ -53,8 +53,8 @@ public function testLoadClassMetadata() public function testLoadClassMetadataAndMerge() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); - $parentClassMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyParent'); + $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MappedDummy'); + $parentClassMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MappedDummyParent'); $this->loader->loadClassMetadata($parentClassMetadata); $classMetadata->merge($parentClassMetadata); diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php index 6b468ff18189c..51d12ea0243e9 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php @@ -32,7 +32,7 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->loader = new XmlFileLoader(__DIR__.'/../../Fixtures/serialization.xml'); - $this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MappedDummy'); } public function testInterface() diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php index 72d146f9f5224..3aa5331237e63 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php @@ -32,7 +32,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->loader = new YamlFileLoader(__DIR__.'/../../Fixtures/serialization.yml'); - $this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MappedDummy'); } public function testInterface() diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php b/src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php index f435d36f744d2..ae77769acc4b5 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php @@ -21,10 +21,11 @@ class TestClassMetadataFactory { public static function createClassMetadata($withParent = false, $withInterface = false) { - $expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MappedDummy'); $foo = new AttributeMetadata('foo'); $foo->addGroup('a'); + $foo->setAlias('myAlias'); $expected->addAttributeMetadata($foo); $bar = new AttributeMetadata('bar'); @@ -66,7 +67,7 @@ public static function createClassMetadata($withParent = false, $withInterface = public static function createXmlCLassMetadata() { - $expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MappedDummy'); $foo = new AttributeMetadata('foo'); $foo->addGroup('group1'); @@ -75,6 +76,7 @@ public static function createXmlCLassMetadata() $bar = new AttributeMetadata('bar'); $bar->addGroup('group2'); + $bar->setAlias('myBar'); $expected->addAttributeMetadata($bar); return $expected; diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index 92051863d598a..91ceeb2976044 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -21,7 +21,7 @@ use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; -use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy; +use Symfony\Component\Serializer\Tests\Fixtures\MappedDummy; class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase { @@ -234,7 +234,7 @@ public function testGroupsNormalize() $this->normalizer = new GetSetMethodNormalizer($classMetadataFactory); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFoo('foo'); $obj->setBar('bar'); $obj->setFooBar('fooBar'); @@ -262,7 +262,7 @@ public function testGroupsDenormalize() $this->normalizer = new GetSetMethodNormalizer($classMetadataFactory); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFoo('foo'); $toNormalize = array('foo' => 'foo', 'bar' => 'bar'); @@ -292,7 +292,7 @@ public function testGroupsNormalizeWithNameConverter() $this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFooBar('@dunglas'); $obj->setSymfony('@coopTilleuls'); $obj->setCoopTilleuls('les-tilleuls.coop'); @@ -313,7 +313,7 @@ public function testGroupsDenormalizeWithNameConverter() $this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFooBar('@dunglas'); $obj->setSymfony('@coopTilleuls'); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 7d9da645c4f4e..0e581365dff6c 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -21,7 +21,7 @@ use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; -use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy; +use Symfony\Component\Serializer\Tests\Fixtures\MappedDummy; /** * @author Kévin Dunglas @@ -182,7 +182,7 @@ public function testGroupsNormalize() $this->normalizer = new ObjectNormalizer($classMetadataFactory); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFoo('foo'); $obj->setBar('bar'); $obj->setFooBar('fooBar'); @@ -210,7 +210,7 @@ public function testGroupsDenormalize() $this->normalizer = new ObjectNormalizer($classMetadataFactory); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFoo('foo'); $toNormalize = array('foo' => 'foo', 'bar' => 'bar'); @@ -240,7 +240,7 @@ public function testGroupsNormalizeWithNameConverter() $this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFooBar('@dunglas'); $obj->setSymfony('@coopTilleuls'); $obj->setCoopTilleuls('les-tilleuls.coop'); @@ -261,7 +261,7 @@ public function testGroupsDenormalizeWithNameConverter() $this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFooBar('@dunglas'); $obj->setSymfony('@coopTilleuls'); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php index 62e6d5d925ca3..e40130df8d2f9 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php @@ -18,7 +18,7 @@ use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\SerializerInterface; -use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy; +use Symfony\Component\Serializer\Tests\Fixtures\MappedDummy; use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy; use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder; @@ -204,7 +204,7 @@ public function testGroupsNormalize() $this->normalizer = new PropertyNormalizer($classMetadataFactory); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFoo('foo'); $obj->setBar('bar'); $obj->setFooBar('fooBar'); @@ -231,7 +231,7 @@ public function testGroupsDenormalize() $this->normalizer = new PropertyNormalizer($classMetadataFactory); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFoo('foo'); $toNormalize = array('foo' => 'foo', 'bar' => 'bar'); @@ -261,7 +261,7 @@ public function testGroupsNormalizeWithNameConverter() $this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFooBar('@dunglas'); $obj->setSymfony('@coopTilleuls'); $obj->setCoopTilleuls('les-tilleuls.coop'); @@ -282,7 +282,7 @@ public function testGroupsDenormalizeWithNameConverter() $this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); $this->normalizer->setSerializer($this->serializer); - $obj = new GroupDummy(); + $obj = new MappedDummy(); $obj->setFooBar('@dunglas'); $obj->setSymfony('@coopTilleuls');