Przeglądaj źródła

More tests for xml namespaces related metadata

Antonio J. García Lagar 12 lat temu
rodzic
commit
cb33cbe998

+ 0 - 7
tests/JMS/Serializer/Tests/Fixtures/ObjectWithXmlNamespaces.php

@@ -19,17 +19,10 @@
 namespace JMS\Serializer\Tests\Fixtures;
 
 use JMS\Serializer\Annotation\Type;
-use JMS\Serializer\Annotation\SerializedName;
-use JMS\Serializer\Annotation\XmlMap;
 use JMS\Serializer\Annotation\XmlRoot;
 use JMS\Serializer\Annotation\XmlNamespace;
 use JMS\Serializer\Annotation\XmlElement;
 use JMS\Serializer\Annotation\XmlAttribute;
-use JMS\Serializer\Annotation\XmlList;
-use JMS\Serializer\Annotation\Groups;
-use Doctrine\Common\Collections\ArrayCollection;
-use PhpCollection\Map;
-use PhpCollection\Sequence;
 
 /**
  * @XmlRoot("test-object")

+ 42 - 0
tests/JMS/Serializer/Tests/Metadata/Driver/BaseDriverTest.php

@@ -168,6 +168,48 @@ abstract class BaseDriverTest extends \PHPUnit_Framework_TestCase
         $this->assertNull($m->discriminatorFieldName);
         $this->assertEquals(array(), $m->discriminatorMap);
     }
+    
+    public function testLoadXmlObjectWithNamespacesMetadata()
+    {
+        $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces'));
+        $this->assertNotNull($m);
+        $this->assertEquals('test-object', $m->xmlRootName);
+        $this->assertCount(3, $m->xmlNamespaces);
+        $this->assertArrayHasKey('', $m->xmlNamespaces);
+        $this->assertEquals('http://example.com/namespace', $m->xmlNamespaces['']);
+        $this->assertArrayHasKey('gd', $m->xmlNamespaces);
+        $this->assertEquals('http://schemas.google.com/g/2005', $m->xmlNamespaces['gd']);
+        $this->assertArrayHasKey('atom', $m->xmlNamespaces);
+        $this->assertEquals('http://www.w3.org/2005/Atom', $m->xmlNamespaces['atom']);
+        
+        $p = new PropertyMetadata($m->name, 'title');
+        $p->type = array('name' => 'string', 'params' => array());
+        $p->xmlNamespace = "http://purl.org/dc/elements/1.1/";
+        $this->assertEquals($p, $m->propertyMetadata['title']);
+        
+        $p = new PropertyMetadata($m->name, 'createdAt');
+        $p->type = array('name' => 'DateTime', 'params' => array());
+        $p->xmlAttribute = true;
+        $this->assertEquals($p, $m->propertyMetadata['createdAt']);
+
+        $p = new PropertyMetadata($m->name, 'etag');
+        $p->type = array('name' => 'string', 'params' => array());
+        $p->xmlAttribute = true;
+        $p->xmlNamespace = "http://schemas.google.com/g/2005";
+        $this->assertEquals($p, $m->propertyMetadata['etag']);
+        
+        $p = new PropertyMetadata($m->name, 'author');
+        $p->type = array('name' => 'string', 'params' => array());
+        $p->xmlAttribute = false;
+        $p->xmlNamespace = "http://www.w3.org/2005/Atom";
+        $this->assertEquals($p, $m->propertyMetadata['author']);
+        
+        $p = new PropertyMetadata($m->name, 'language');
+        $p->type = array('name' => 'string', 'params' => array());
+        $p->xmlAttribute = true;
+        $p->xmlNamespace = "http://purl.org/dc/elements/1.1/";
+        $this->assertEquals($p, $m->propertyMetadata['language']);
+    }
 
     public function testMaxDepth()
     {

+ 43 - 0
tests/JMS/Serializer/Tests/Metadata/Driver/php/ObjectWithXmlNamespaces.php

@@ -0,0 +1,43 @@
+<?php
+
+use JMS\Serializer\Metadata\ClassMetadata;
+use JMS\Serializer\Metadata\PropertyMetadata;
+
+$metadata = new ClassMetadata('JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces');
+$metadata->xmlRootName = 'test-object';
+
+$metadata->registerNamespace('http://example.com/namespace');
+$metadata->registerNamespace('http://schemas.google.com/g/2005', 'gd');
+$metadata->registerNamespace('http://www.w3.org/2005/Atom', 'atom');
+
+$pMetadata = new PropertyMetadata('JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces', 'title');
+$pMetadata->setType('string');
+$pMetadata->xmlNamespace = "http://purl.org/dc/elements/1.1/";
+$metadata->addPropertyMetadata($pMetadata);
+
+$pMetadata = new PropertyMetadata('JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces', 'createdAt');
+$pMetadata->setType('DateTime');
+$pMetadata->xmlAttribute = true;
+$metadata->addPropertyMetadata($pMetadata);
+
+$pMetadata = new PropertyMetadata('JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces', 'etag');
+$pMetadata->setType('string');
+$pMetadata->xmlAttribute = true;
+$pMetadata->xmlNamespace = 'http://schemas.google.com/g/2005';
+$metadata->addPropertyMetadata($pMetadata);
+
+$pMetadata = new PropertyMetadata('JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces', 'author');
+$pMetadata->setType('string');
+$pMetadata->xmlNamespace  = 'http://www.w3.org/2005/Atom';
+$metadata->addPropertyMetadata($pMetadata);
+
+$pMetadata = new PropertyMetadata('JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces', 'language');
+$pMetadata->setType('string');
+$pMetadata->xmlAttribute = true;
+$pMetadata->xmlNamespace = 'http://purl.org/dc/elements/1.1/';
+$metadata->addPropertyMetadata($pMetadata);
+
+
+$metadata->addPropertyMetadata($pMetadata);
+
+return $metadata;

+ 13 - 0
tests/JMS/Serializer/Tests/Metadata/Driver/xml/ObjectWithXmlNamespaces.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<serializer>
+    <class name="JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces" xml-root-name="test-object">
+        <xml-namespace uri="http://example.com/namespace"/>
+        <xml-namespace prefix="gd" uri="http://schemas.google.com/g/2005"/>
+        <xml-namespace prefix="atom" uri="http://www.w3.org/2005/Atom"/>
+        <property name="title" type="string" xml-namespace="http://purl.org/dc/elements/1.1/"/>
+        <property name="createdAt" xml-attribute="true" type="DateTime"/>
+        <property name="etag" type="string" xml-attribute="true" xml-namespace="http://schemas.google.com/g/2005"/>
+        <property name="author" type="string" xml-namespace="http://www.w3.org/2005/Atom"/>
+        <property name="language" type="string" xml-attribute="true" xml-namespace="http://purl.org/dc/elements/1.1/"/>
+    </class>
+</serializer>

+ 24 - 0
tests/JMS/Serializer/Tests/Metadata/Driver/yml/ObjectWithXmlNamespaces.yml

@@ -0,0 +1,24 @@
+JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces:
+    xml_root_name: test-object
+    xml_namespaces:
+        "": http://example.com/namespace
+        gd: http://schemas.google.com/g/2005
+        atom: http://www.w3.org/2005/Atom
+    properties:
+        title:
+            type: string
+            xml_namespace: http://purl.org/dc/elements/1.1/
+        createdAt:
+            type: DateTime
+            xml_attribute: true
+        etag:
+            type: string
+            xml_attribute: true
+            xml_namespace: http://schemas.google.com/g/2005
+        author:
+            type: string
+            xml_namespace: http://www.w3.org/2005/Atom
+        language:
+            type: string
+            xml_attribute: true
+            xml_namespace: http://purl.org/dc/elements/1.1/