소스 검색

Test for looking up the namespace in relation of current node.

Problem exposed by @marcospassos in https://github.com/schmittjoh/serializer/pull/58#discussion_r6226688
Antonio J. García Lagar 11 년 전
부모
커밋
a7ac071de7

+ 7 - 1
tests/JMS/Serializer/Tests/Fixtures/BlogPost.php

@@ -102,10 +102,16 @@ class BlogPost
      */
     private $author;
 
-    public function __construct($title, Author $author, \DateTime $createdAt)
+    /**
+     * @Type("JMS\Serializer\Tests\Fixtures\Publisher")
+     */
+    private $publisher;
+
+    public function __construct($title, Author $author, \DateTime $createdAt, Publisher $publisher)
     {
         $this->title = $title;
         $this->author = $author;
+        $this->publisher = $publisher;
         $this->published = false;
         $this->comments = new ArrayCollection();
         $this->comments2 = new Sequence();

+ 2 - 1
tests/JMS/Serializer/Tests/Fixtures/InitializedBlogPostConstructor.php

@@ -27,6 +27,7 @@ use JMS\Serializer\Construction\ObjectConstructorInterface;
 use JMS\Serializer\VisitorInterface;
 
 use JMS\Serializer\Tests\Fixtures\Author;
+use JMS\Serializer\Tests\Fixtures\Publisher;
 use JMS\Serializer\Construction\UnserializeObjectConstructor;
 
 class InitializedBlogPostConstructor extends UnserializeObjectConstructor
@@ -37,6 +38,6 @@ class InitializedBlogPostConstructor extends UnserializeObjectConstructor
             return parent::construct($visitor, $metadata, $data, $type);
         }
 
-        return new BlogPost('This is a nice title.', new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')));
+        return new BlogPost('This is a nice title.', new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), new Publisher('Bar Foo'));
     }
 }

+ 49 - 0
tests/JMS/Serializer/Tests/Fixtures/Publisher.php

@@ -0,0 +1,49 @@
+<?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\SerializedName;
+use JMS\Serializer\Annotation\Type;
+use JMS\Serializer\Annotation\XmlRoot;
+use JMS\Serializer\Annotation\XmlNamespace;
+use JMS\Serializer\Annotation\XmlElement;
+
+/**
+ * @XmlRoot("publisher")
+ * @XmlNamespace(uri="http://example.com/namespace2", prefix="ns2")
+ */
+class Publisher
+{
+    /**
+     * @Type("string")
+     * @XmlElement(namespace="http://example.com/namespace2")
+     * @SerializedName("pub_name")
+     */
+    private $name;
+
+    public function __construct($name)
+    {
+        $this->name = $name;
+    }
+
+    public function getName()
+    {
+        return $this->name;
+    }
+}

+ 4 - 4
tests/JMS/Serializer/Tests/Fixtures/SimpleSubClassObject.php

@@ -2,13 +2,13 @@
 
 /*
  * 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.
@@ -30,7 +30,7 @@ use JMS\Serializer\Annotation\XmlElement;
 class SimpleSubClassObject
     extends SimpleClassObject
 {
-   
+
     /**
      * @Type("string")
      * @XmlElement(namespace="http://better.foo.example.org")

+ 4 - 2
tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php

@@ -53,6 +53,7 @@ use JMS\Serializer\Tests\Fixtures\AccessorOrderChild;
 use JMS\Serializer\Tests\Fixtures\AccessorOrderParent;
 use JMS\Serializer\Tests\Fixtures\AccessorOrderMethod;
 use JMS\Serializer\Tests\Fixtures\Author;
+use JMS\Serializer\Tests\Fixtures\Publisher;
 use JMS\Serializer\Tests\Fixtures\AuthorList;
 use JMS\Serializer\Tests\Fixtures\AuthorReadOnly;
 use JMS\Serializer\Tests\Fixtures\BlogPost;
@@ -290,7 +291,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
 
     public function testBlogPost()
     {
-        $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')));
+        $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), new Publisher('Bar Foo'));
         $post->addComment($comment = new Comment($author, 'foo'));
 
         $this->assertEquals($this->getContent('blog_post'), $this->serialize($post));
@@ -312,9 +313,10 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         $objectConstructor = new InitializedBlogPostConstructor();
         $this->serializer = new Serializer($this->factory, $this->handlerRegistry, $objectConstructor, $this->serializationVisitors, $this->deserializationVisitors, $this->dispatcher);
 
-        $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')));
+        $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), new Publisher('Bar Foo'));
 
         $this->setField($post, 'author', null);
+        $this->setField($post, 'publisher', null);
 
         $this->assertEquals($this->getContent('blog_post_unauthored'), $this->serialize($post, SerializationContext::create()->setSerializeNull(true)));
 

+ 2 - 2
tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php

@@ -49,8 +49,8 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['array_floats'] = '[1.34,3,6.42]';
             $outputs['array_objects'] = '[{"foo":"foo","moo":"bar","camel_case":"boo"},{"foo":"baz","moo":"boo","camel_case":"boo"}]';
             $outputs['array_mixed'] = '["foo",1,true,{"foo":"foo","moo":"bar","camel_case":"boo"},[1,3,true]]';
-            $outputs['blog_post'] = '{"id":"what_a_nice_id","title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"etag":"1edf9bf60a32d89afbb85b2be849e3ceed5f5b10","comments":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"comments2":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"metadata":{"foo":"bar"},"author":{"full_name":"Foo Bar"}}';
-            $outputs['blog_post_unauthored'] = '{"id":"what_a_nice_id","title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"etag":"1edf9bf60a32d89afbb85b2be849e3ceed5f5b10","comments":[],"comments2":[],"metadata":{"foo":"bar"},"author":null}';
+            $outputs['blog_post'] = '{"id":"what_a_nice_id","title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"etag":"1edf9bf60a32d89afbb85b2be849e3ceed5f5b10","comments":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"comments2":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"metadata":{"foo":"bar"},"author":{"full_name":"Foo Bar"},"publisher":{"pub_name":"Bar Foo"}}';
+            $outputs['blog_post_unauthored'] = '{"id":"what_a_nice_id","title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"etag":"1edf9bf60a32d89afbb85b2be849e3ceed5f5b10","comments":[],"comments2":[],"metadata":{"foo":"bar"},"author":null,"publisher":null}';
             $outputs['price'] = '{"price":3}';
             $outputs['currency_aware_price'] = '{"currency":"EUR","amount":2.34}';
             $outputs['order'] = '{"cost":{"price":12.34}}';

+ 3 - 0
tests/JMS/Serializer/Tests/Serializer/xml/blog_post.xml

@@ -20,4 +20,7 @@
   <atom:author>
     <full_name><![CDATA[Foo Bar]]></full_name>
   </atom:author>
+  <publisher xmlns:ns2="http://example.com/namespace2">
+    <ns2:pub_name><![CDATA[Bar Foo]]></ns2:pub_name>
+  </publisher>
 </blog-post>

+ 2 - 0
tests/JMS/Serializer/Tests/Serializer/yml/blog_post.yml

@@ -17,3 +17,5 @@ metadata:
     foo: bar
 author:
     full_name: 'Foo Bar'
+publisher:
+    pub_name: 'Bar Foo'

+ 1 - 0
tests/JMS/Serializer/Tests/Serializer/yml/blog_post_unauthored.yml

@@ -6,3 +6,4 @@ etag: 1edf9bf60a32d89afbb85b2be849e3ceed5f5b10
 metadata:
     foo: bar
 author: null
+publisher: null