Bläddra i källkod

fixed DateTime issue and created tests
deserialization of named DateTime array from XML currently fails!

Jens Hassler 11 år sedan
förälder
incheckning
af8bcbbf2c

+ 2 - 1
src/JMS/Serializer/GenericSerializationVisitor.php

@@ -99,7 +99,8 @@ abstract class GenericSerializationVisitor extends AbstractVisitor
         }
 
         foreach ($data as $k => $v) {
-            $v = $this->navigator->accept($v, isset($type['params'][1]) ? $type['params'][1] : null, $context);
+            $typeArray = isset($type['params'][0]) ? (isset($type['params'][1]) && is_array($type['params'][1]) ? $type['params'][1] : $type['params'][0]) : null;
+            $v = $this->navigator->accept($v, $typeArray, $context);
 
             if (null === $v && (!is_string($k) || !$context->shouldSerializeNull())) {
                 continue;

+ 2 - 1
src/JMS/Serializer/XmlSerializationVisitor.php

@@ -171,7 +171,8 @@ class XmlSerializationVisitor extends AbstractVisitor
                 $entryNode->setAttribute($keyAttributeName, (string) $k);
             }
 
-            if (null !== $node = $this->navigator->accept($v, isset($type['params'][1]) ? $type['params'][1] : null, $context)) {
+            $typeArray = isset($type['params'][0]) ? (isset($type['params'][1]) && is_array($type['params'][1]) ? $type['params'][1] : $type['params'][0]) : null;
+            if (null !== $node = $this->navigator->accept($v, $typeArray, $context)) {
                 $this->currentNode->appendChild($node);
             }
 

+ 2 - 1
src/JMS/Serializer/YamlSerializationVisitor.php

@@ -96,7 +96,8 @@ class YamlSerializationVisitor extends AbstractVisitor
 
             $this->writer->indent();
 
-            if (null !== $v = $this->navigator->accept($v, null, $context)) {
+            $typeArray = isset($type['params'][0]) ? (isset($type['params'][1]) && is_array($type['params'][1]) ? $type['params'][1] : $type['params'][0]) : null;
+            if (null !== $v = $this->navigator->accept($v, $typeArray, $context)) {
                 $this->writer
                     ->rtrim(false)
                     ->writeln(' '.$v)

+ 68 - 0
tests/JMS/Serializer/Tests/Fixtures/DateTimeArraysObject.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * DateTimeArraysObject.php
+ * 
+ * @author Jens Hassler <lukey@skytrek.de>
+ * @since  12/2013
+ */
+ 
+namespace JMS\Serializer\Tests\Fixtures;
+
+use JMS\Serializer\Annotation\Type;
+use JMS\Serializer\Annotation\XmlMap;
+use JMS\Serializer\Annotation\XmlList;
+use JMS\Serializer\Annotation\XmlKeyValuePairs;
+
+
+class DateTimeArraysObject
+{
+    /**
+     * @var \DateTime[]
+     * @Type("array<DateTime>")
+     */
+    private $arrayWithDefaultDateTime;
+
+    /**
+     * @var \DateTime[]
+     * @Type("array<DateTime<'d.m.Y H:i:s'>>")
+     */
+    private $arrayWithFormattedDateTime;
+
+    /**
+     * @var \DateTime[]
+     * @Type("array<string,DateTime<'d.m.Y H:i:s'>>")
+     * @XmlKeyValuePairs
+     */
+    private $namedArrayWithFormattedDate;
+
+    function __construct($arrayWithDefaultDateTime, $arrayWithFormattedDateTime, $namedArrayWithFormattedDate)
+    {
+        $this->arrayWithDefaultDateTime    = $arrayWithDefaultDateTime;
+        $this->arrayWithFormattedDateTime  = $arrayWithFormattedDateTime;
+        $this->namedArrayWithFormattedDate = $namedArrayWithFormattedDate;
+    }
+
+    /**
+     * @return \DateTime[]
+     */
+    public function getArrayWithDefaultDateTime()
+    {
+        return $this->arrayWithDefaultDateTime;
+    }
+
+    /**
+     * @return \DateTime[]
+     */
+    public function getArrayWithFormattedDateTime()
+    {
+        return $this->arrayWithFormattedDateTime;
+    }
+
+    /**
+     * @return \DateTime[]
+     */
+    public function getNamedArrayWithFormattedDate()
+    {
+        return $this->namedArrayWithFormattedDate;
+    }
+}

+ 29 - 0
tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php

@@ -23,6 +23,7 @@ use JMS\Serializer\DeserializationContext;
 use JMS\Serializer\GraphNavigator;
 use JMS\Serializer\Handler\PhpCollectionHandler;
 use JMS\Serializer\SerializationContext;
+use JMS\Serializer\Tests\Fixtures\DateTimeArraysObject;
 use JMS\Serializer\Tests\Fixtures\Discriminator\Car;
 use JMS\Serializer\Tests\Fixtures\InlineChildEmpty;
 use JMS\Serializer\Tests\Fixtures\Tree;
@@ -254,6 +255,34 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         }
     }
 
+    public function testDateTimeArrays()
+    {
+        $data = array(
+            new \DateTime('2047-01-01 12:47:47', new \DateTimeZone('UTC')),
+            new \DateTime('2013-12-05 00:00:00', new \DateTimeZone('UTC'))
+        );
+
+        $object = new DateTimeArraysObject($data, $data, array('testdate1' => $data[0], 'testdate2' => $data[1]));
+        $serializedObject = $this->serialize( $object );
+
+        $this->assertEquals($this->getContent('array_datetimes_object'), $serializedObject);
+
+        if ($this->hasDeserializer()) {
+            /** @var DateTimeArraysObject $deserializedObject */
+            $deserializedObject = $this->deserialize($this->getContent('array_datetimes_object'), 'Jms\Serializer\Tests\Fixtures\DateTimeArraysObject');
+
+            /** deserialized object has a default timezone set depending on user's timezone settings. That's why we manually set the UTC timezone on the DateTime objects. */
+            foreach ($deserializedObject->getArrayWithDefaultDateTime() as $dateTime)
+                $dateTime->setTimezone(new \DateTimeZone('UTC'));
+            foreach ($deserializedObject->getArrayWithFormattedDateTime() as $dateTime)
+                $dateTime->setTimezone(new \DateTimeZone('UTC'));
+            foreach ($deserializedObject->getNamedArrayWithFormattedDate() as $dateTime)
+                $dateTime->setTimezone(new \DateTimeZone('UTC'));
+
+            $this->assertEquals($object, $deserializedObject);
+        }
+    }
+
     public function testArrayMixed()
     {
         $this->assertEquals($this->getContent('array_mixed'), $this->serialize(array('foo', 1, true, new SimpleObject('foo', 'bar'), array(1, 3, true))));

+ 1 - 5
tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php

@@ -49,6 +49,7 @@ 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['array_datetimes_object'] = '{"array_with_default_date_time":["2047-01-01T12:47:47+0000","2013-12-05T00:00:00+0000"],"array_with_formatted_date_time":["01.01.2047 12:47:47","05.12.2013 00:00:00"],"named_array_with_formatted_date":{"testdate1":"01.01.2047 12:47:47","testdate2":"05.12.2013 00:00:00"}}';
             $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}';
@@ -175,11 +176,6 @@ class JsonSerializationTest extends BaseSerializationTest
         $this->assertEquals('{}', $this->serialize(new Author(null)));
     }
 
-    public function testSerializeArrayWithEmptyObject()
-    {
-        $this->assertEquals('{"0":{}}', $this->serialize(array(new \stdClass())));
-    }
-
     protected function getFormat()
     {
         return 'json';

+ 15 - 0
tests/JMS/Serializer/Tests/Serializer/xml/array_datetimes_object.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result>
+  <array_with_default_date_time>
+    <entry><![CDATA[2047-01-01T12:47:47+0000]]></entry>
+    <entry><![CDATA[2013-12-05T00:00:00+0000]]></entry>
+  </array_with_default_date_time>
+  <array_with_formatted_date_time>
+    <entry><![CDATA[01.01.2047 12:47:47]]></entry>
+    <entry><![CDATA[05.12.2013 00:00:00]]></entry>
+  </array_with_formatted_date_time>
+  <named_array_with_formatted_date>
+    <testdate1><![CDATA[01.01.2047 12:47:47]]></testdate1>
+    <testdate2><![CDATA[05.12.2013 00:00:00]]></testdate2>
+  </named_array_with_formatted_date>
+</result>

+ 9 - 0
tests/JMS/Serializer/Tests/Serializer/yml/array_datetimes_object.yml

@@ -0,0 +1,9 @@
+array_with_default_date_time:
+    - '2047-01-01T12:47:47+0000'
+    - '2013-12-05T00:00:00+0000'
+array_with_formatted_date_time:
+    - '01.01.2047 12:47:47'
+    - '05.12.2013 00:00:00'
+named_array_with_formatted_date:
+    testdate1: '01.01.2047 12:47:47'
+    testdate2: '05.12.2013 00:00:00'