瀏覽代碼

Add InitializedObjectConstructor as a test double for DoctrineObjectConstructor.

Add tests to BaseSerializationTest:
* testNull when expecting types
* testNumerics when expecting numeric types
* testDateTime
* testDeserializingNull when using InitializedObjectConstructor

Add content for blog_post_unauthored and date_time:
* JsonSerializationTest
* xml/blog_post_unauthored.xml
* xml/date_time.xml
* yml/blog_post_unauthored.yml
* yml/date_time.yml

So, from the phpunit test results, we see:
* fails when serializing a simple DateTime
* error when deserialing a null when expecting a DateTime
* error: no support for the "float" alias for "double"
* error when deserializing: a null value will not overwrite a non-null property in the initialized object

Conflicts:

	Tests/Serializer/JsonSerializationTest.php
Anthon Pang 12 年之前
父節點
當前提交
94ea6b8ce8

+ 45 - 0
Tests/Fixtures/InitializedObjectConstructor.php

@@ -0,0 +1,45 @@
+<?php
+
+/*
+ * Copyright 2011 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\SerializerBundle\Tests\Fixtures;
+
+use Doctrine\Common\Collections\ArrayCollection;
+
+use JMS\SerializerBundle\Metadata\ClassMetadata;
+
+use JMS\SerializerBundle\Serializer\Construction\ObjectConstructorInterface;
+use JMS\SerializerBundle\Serializer\VisitorInterface;
+
+use JMS\SerializerBundle\Tests\Fixtures\Author;
+
+class InitializedObjectConstructor implements ObjectConstructorInterface
+{
+    public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type)
+    {
+        $post = new \StdClass;
+        $post->title = 'This is a nice title.';
+        $post->author = new Author('Foo Bar');
+        $post->createdAt = new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC'));
+        $post->comments = new ArrayCollection();
+        $post->published = false;
+
+        $post->comments->add(new \StdClass);
+
+        return $post;
+    }
+}

+ 75 - 8
Tests/Serializer/BaseSerializationTest.php

@@ -57,6 +57,7 @@ use JMS\SerializerBundle\Tests\Fixtures\GroupsObject;
 use JMS\SerializerBundle\Tests\Fixtures\InvalidGroupsObject;
 use JMS\SerializerBundle\Tests\Fixtures\IndexedCommentsBlogPost;
 use JMS\SerializerBundle\Tests\Fixtures\InlineParent;
+use JMS\SerializerBundle\Tests\Fixtures\InitializedObjectConstructor;
 use JMS\SerializerBundle\Tests\Fixtures\Log;
 use JMS\SerializerBundle\Tests\Fixtures\ObjectWithLifecycleCallbacks;
 use JMS\SerializerBundle\Tests\Fixtures\ObjectWithVersionedVirtualProperties;
@@ -105,15 +106,30 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         $this->serializer->setSerializeNull(false);
     }
 
-    public function testNull()
+    /**
+     * @dataProvider getTypes
+     */
+    public function testNull($type)
     {
-        $this->assertEquals($this->getContent('null'), $this->serialize(null));
+        $this->assertEquals($this->getContent('null'), $this->serialize(null), $type);
 
         if ($this->hasDeserializer()) {
-            $this->assertEquals(null, $this->deserialize($this->getContent('null'), 'NULL'));
+            $this->assertEquals(null, $this->deserialize($this->getContent('null'), $type));
         }
     }
 
+    public function getTypes()
+    {
+        return array(
+            array('NULL'),
+            array('integer'),
+            array('double'),
+            array('float'),
+            array('string'),
+            array('DateTime'),
+        );
+    }
+
     public function testString()
     {
         $this->assertEquals($this->getContent('string'), $this->serialize('foo'));
@@ -143,21 +159,23 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
     /**
      * @dataProvider getNumerics
      */
-    public function testNumerics($key, $value)
+    public function testNumerics($key, $value, $type)
     {
         $this->assertEquals($this->getContent($key), $this->serialize($value));
 
         if ($this->hasDeserializer()) {
-            $this->assertEquals($value, $this->deserialize($this->getContent($key), is_double($value) ? 'double' : 'integer'));
+            $this->assertEquals($value, $this->deserialize($this->getContent($key), $type));
         }
     }
 
     public function getNumerics()
     {
         return array(
-            array('integer', 1),
-            array('float', 4.533),
-            array('float_trailing_zero', 1.0),
+            array('integer', 1, 'integer'),
+            array('float', 4.533, 'double'),
+            array('float', 4.533, 'float'),
+            array('float_trailing_zero', 1.0, 'double'),
+            array('float_trailing_zero', 1.0, 'float'),
         );
     }
 
@@ -225,6 +243,29 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($this->getContent('array_mixed'), $this->serialize(array('foo', 1, true, new SimpleObject('foo', 'bar'), array(1, 3, true))));
     }
 
+    /**
+     * @dataProvider getDateTime
+     */
+    public function testDateTime($key, $value, $type)
+    {
+        $this->assertEquals($this->getContent($key), $this->serialize($value, $type));
+
+        if ($this->hasDeserializer()) {
+            $deserialized = $this->deserialize($this->getContent($key), $type);
+
+            $this->assertTrue(is_object($deserialized));
+            $this->assertEquals(get_class($value), get_class($deserialized));
+            $this->assertEquals($value->getTimestamp(), $deserialized->getTimestamp());
+        }
+    }
+
+    public function getDateTime()
+    {
+        return array(
+            array('date_time', new \DateTime('2011-08-30 00:00', new \DateTimeZone('UTC')), 'DateTime'),
+        );
+    }
+
     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')));
@@ -242,6 +283,32 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         }
     }
 
+    public function testDeserializingNull()
+    {
+        if (get_class($this) === 'JMS\SerializerBundle\Tests\Serializer\XmlSerializationTest') {
+            $this->markTestSkipped('Deserializing null not working in XML.');
+        }
+
+        $objectConstructor = new InitializedObjectConstructor();
+        $this->serializer = new Serializer($this->factory, $this->handlerRegistry, $objectConstructor, $this->dispatcher, null, $this->serializationVisitors, $this->deserializationVisitors);
+        $this->serializer->setSerializeNull(true);
+
+        $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')));
+
+        $this->setField($post, 'author', null);
+
+        $this->assertEquals($this->getContent('blog_post_unauthored'), $this->serialize($post));
+
+        if ($this->hasDeserializer()) {
+            $deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post));
+            $this->assertEquals('2011-07-30T00:00:00+0000', $this->getField($deserialized, 'createdAt')->format(\DateTime::ISO8601));
+            $this->assertAttributeEquals('This is a nice title.', 'title', $deserialized);
+            $this->assertAttributeSame(false, 'published', $deserialized);
+            $this->assertAttributeEquals(new ArrayCollection(), 'comments', $deserialized);
+            $this->assertEquals(null, $this->getField($deserialized, 'author'));
+        }
+    }
+
     public function testReadOnly()
     {
         $author = new AuthorReadOnly(123, 'Ruud Kamphuis');

+ 8 - 4
Tests/Serializer/JsonSerializationTest.php

@@ -18,13 +18,15 @@
 
 namespace JMS\SerializerBundle\Tests\Serializer;
 
-use JMS\SerializerBundle\Serializer\VisitorInterface;
-use JMS\SerializerBundle\Serializer\GraphNavigator;
-use JMS\SerializerBundle\Serializer\EventDispatcher\EventSubscriberInterface;
+use JMS\SerializerBundle\Exception\RuntimeException;
+
 use JMS\SerializerBundle\Serializer\EventDispatcher\Event;
+use JMS\SerializerBundle\Serializer\EventDispatcher\EventSubscriberInterface;
+use JMS\SerializerBundle\Serializer\GraphNavigator;
+use JMS\SerializerBundle\Serializer\VisitorInterface;
+
 use JMS\SerializerBundle\Tests\Fixtures\Author;
 use JMS\SerializerBundle\Tests\Fixtures\AuthorList;
-use JMS\SerializerBundle\Exception\RuntimeException;
 
 class JsonSerializationTest extends BaseSerializationTest
 {
@@ -49,6 +51,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $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'] = '{"title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"comments":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"author":{"full_name":"Foo Bar"}}';
+            $outputs['blog_post_unauthored'] = '{"title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"comments":[],"author":null}';
             $outputs['price'] = '{"price":3}';
             $outputs['currency_aware_price'] = '{"currency":"EUR","amount":2.34}';
             $outputs['order'] = '{"cost":{"price":12.34}}';
@@ -81,6 +84,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['hash_empty'] = '{"hash":{}}';
             $outputs['object_when_null'] = '{"text":"foo"}';
             $outputs['object_when_null_and_serialized'] = '{"author":null,"text":"foo"}';
+            $outputs['date_time'] = '"2011-08-30T00:00:00+0000"';
         }
 
         if (!isset($outputs[$key])) {

+ 5 - 0
Tests/Serializer/xml/blog_post_unauthored.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<blog-post created_at="2011-07-30T00:00:00+0000" is_published="false">
+  <title><![CDATA[This is a nice title.]]></title>
+  <author xsi:nil="true"/>
+</blog-post>

+ 2 - 0
Tests/Serializer/xml/date_time.xml

@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result><![CDATA[2011-08-30T00:00:00+0000]]></result>

+ 4 - 0
Tests/Serializer/yml/blog_post_unauthored.yml

@@ -0,0 +1,4 @@
+title: 'This is a nice title.'
+created_at: '2011-07-30T00:00:00+0000'
+is_published: false
+author: null

+ 1 - 0
Tests/Serializer/yml/date_time.yml

@@ -0,0 +1 @@
+'2011-08-30T00:00:00+0000'