Ver código fonte

Test XmlNamespace annotations inheritance

Antonio J. García Lagar 12 anos atrás
pai
commit
da54557c34

+ 1 - 0
src/JMS/Serializer/Metadata/ClassMetadata.php

@@ -139,6 +139,7 @@ class ClassMetadata extends MergeableClassMetadata
         $this->postSerializeMethods = array_merge($this->postSerializeMethods, $object->postSerializeMethods);
         $this->postDeserializeMethods = array_merge($this->postDeserializeMethods, $object->postDeserializeMethods);
         $this->xmlRootName = $object->xmlRootName;
+        $this->xmlNamespaces = array_merge($this->xmlNamespaces, $object->xmlNamespaces);
 
         // Handler methods are taken from the outer class completely.
         $this->handlerCallbacks = $object->handlerCallbacks;

+ 43 - 0
tests/JMS/Serializer/Tests/Fixtures/SimpleClassObject.php

@@ -0,0 +1,43 @@
+<?php
+
+/*
+ * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace JMS\Serializer\Tests\Fixtures;
+
+use JMS\Serializer\Annotation\XmlNamespace;
+use JMS\Serializer\Annotation\Type;
+use JMS\Serializer\Annotation\XmlAttribute;
+use JMS\Serializer\Annotation\XmlElement;
+
+/**
+ * @XmlNamespace(prefix="foo", uri="http://foo.example.org");
+ */
+class SimpleClassObject
+{
+    /**
+     * @Type("string")
+     * @XmlAttribute(namespace="http://foo.example.org")
+     */
+    public $foo;
+    
+    /**
+     * @Type("string")
+     * @XmlElement(namespace="http://foo.example.org")
+     */
+    public $bar;
+
+}

+ 40 - 0
tests/JMS/Serializer/Tests/Fixtures/SimpleSubClassObject.php

@@ -0,0 +1,40 @@
+<?php
+
+/*
+ * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace JMS\Serializer\Tests\Fixtures;
+
+use JMS\Serializer\Annotation\XmlNamespace;
+use JMS\Serializer\Annotation\Type;
+use JMS\Serializer\Annotation\XmlAttribute;
+use JMS\Serializer\Annotation\XmlElement;
+
+/**
+ * @XmlNamespace(prefix="old_foo", uri="http://foo.example.org");
+ * @XmlNamespace(prefix="foo", uri="http://better.foo.example.org");
+ */
+class SimpleSubClassObject
+    extends SimpleClassObject
+{
+   
+    /**
+     * @Type("string")
+     * @XmlElement(namespace="http://better.foo.example.org")
+     */
+    public $moo;
+
+}

+ 18 - 0
tests/JMS/Serializer/Tests/Serializer/XmlSerializationTest.php

@@ -32,6 +32,8 @@ use JMS\Serializer\Tests\Fixtures\ObjectWithVirtualXmlProperties;
 use JMS\Serializer\Tests\Fixtures\ObjectWithXmlKeyValuePairs;
 use JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces;
 use JMS\Serializer\Tests\Fixtures\Input;
+use JMS\Serializer\Tests\Fixtures\SimpleClassObject;
+use JMS\Serializer\Tests\Fixtures\SimpleSubClassObject;
 
 class XmlSerializationTest extends BaseSerializationTest
 {
@@ -203,6 +205,22 @@ class XmlSerializationTest extends BaseSerializationTest
 
     }
     
+    public function testXmlNamespacesInheritance()
+    {
+        $object = new SimpleClassObject();
+        $object->foo = 'foo';
+        $object->bar = 'bar';
+        
+        $this->assertEquals($this->getContent('simple_class_object'), $this->serialize($object));
+        
+        $childObject = new SimpleSubClassObject();
+        $childObject->foo = 'foo';
+        $childObject->bar = 'bar';
+        $childObject->moo = 'moo';
+        
+        $this->assertEquals($this->getContent('simple_subclass_object'), $this->serialize($childObject));
+    }
+    
     private function xpathFirstToString(\SimpleXMLElement $xml, $xpath)
     {
         $nodes = $xml->xpath($xpath);

+ 4 - 0
tests/JMS/Serializer/Tests/Serializer/xml/simple_class_object.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result xmlns:foo="http://foo.example.org" foo:foo="foo">
+  <foo:bar><![CDATA[bar]]></foo:bar>
+</result>

+ 5 - 0
tests/JMS/Serializer/Tests/Serializer/xml/simple_subclass_object.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result xmlns:foo="http://better.foo.example.org" xmlns:old_foo="http://foo.example.org" old_foo:foo="foo">
+  <old_foo:bar><![CDATA[bar]]></old_foo:bar>
+  <foo:moo><![CDATA[moo]]></foo:moo>
+</result>