Prechádzať zdrojové kódy

refactored element type extraction line to separate function
refactored tests to exclude deserializing XmlKeyValuePairs

Jens Hassler 11 rokov pred
rodič
commit
d8ab7262be

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

@@ -99,8 +99,7 @@ abstract class GenericSerializationVisitor extends AbstractVisitor
         }
         }
 
 
         foreach ($data as $k => $v) {
         foreach ($data as $k => $v) {
-            $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);
+            $v = $this->navigator->accept($v, $this->getElementType($type), $context);
 
 
             if (null === $v && (!is_string($k) || !$context->shouldSerializeNull())) {
             if (null === $v && (!is_string($k) || !$context->shouldSerializeNull())) {
                 continue;
                 continue;
@@ -112,6 +111,20 @@ abstract class GenericSerializationVisitor extends AbstractVisitor
         return $rs;
         return $rs;
     }
     }
 
 
+    /**
+     * @param array $typeArray
+     */
+    protected function getElementType($typeArray)
+    {
+        if (false === isset($typeArray['params'][0]))
+            return null;
+
+        if (isset($typeArray['params'][1]) && is_array($typeArray['params'][1]))
+            return $typeArray['params'][1];
+        else
+            return $typeArray['params'][0];
+    }
+
     public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context)
     public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context)
     {
     {
         if (null === $this->root) {
         if (null === $this->root) {

+ 15 - 20
tests/JMS/Serializer/Tests/Fixtures/DateTimeArraysObject.php

@@ -1,9 +1,18 @@
 <?php
 <?php
-/**
- * DateTimeArraysObject.php
- * 
- * @author Jens Hassler <lukey@skytrek.de>
- * @since  12/2013
+/*
+ * 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;
 namespace JMS\Serializer\Tests\Fixtures;
@@ -28,18 +37,11 @@ class DateTimeArraysObject
      */
      */
     private $arrayWithFormattedDateTime;
     private $arrayWithFormattedDateTime;
 
 
-    /**
-     * @var \DateTime[]
-     * @Type("array<string,DateTime<'d.m.Y H:i:s'>>")
-     * @XmlKeyValuePairs
-     */
-    private $namedArrayWithFormattedDate;
 
 
-    function __construct($arrayWithDefaultDateTime, $arrayWithFormattedDateTime, $namedArrayWithFormattedDate)
+    function __construct($arrayWithDefaultDateTime, $arrayWithFormattedDateTime)
     {
     {
         $this->arrayWithDefaultDateTime    = $arrayWithDefaultDateTime;
         $this->arrayWithDefaultDateTime    = $arrayWithDefaultDateTime;
         $this->arrayWithFormattedDateTime  = $arrayWithFormattedDateTime;
         $this->arrayWithFormattedDateTime  = $arrayWithFormattedDateTime;
-        $this->namedArrayWithFormattedDate = $namedArrayWithFormattedDate;
     }
     }
 
 
     /**
     /**
@@ -58,11 +60,4 @@ class DateTimeArraysObject
         return $this->arrayWithFormattedDateTime;
         return $this->arrayWithFormattedDateTime;
     }
     }
 
 
-    /**
-     * @return \DateTime[]
-     */
-    public function getNamedArrayWithFormattedDate()
-    {
-        return $this->namedArrayWithFormattedDate;
-    }
 }
 }

+ 47 - 0
tests/JMS/Serializer/Tests/Fixtures/NamedDateTimeArraysObject.php

@@ -0,0 +1,47 @@
+<?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\Type;
+use JMS\Serializer\Annotation\XmlMap;
+use JMS\Serializer\Annotation\XmlList;
+use JMS\Serializer\Annotation\XmlKeyValuePairs;
+
+
+class NamedDateTimeArraysObject
+{
+    /**
+     * @var \DateTime[]
+     * @Type("array<string,DateTime<'d.m.Y H:i:s'>>")
+     * @XmlKeyValuePairs
+     */
+    private $namedArrayWithFormattedDate;
+
+    function __construct($namedArrayWithFormattedDate)
+    {
+        $this->namedArrayWithFormattedDate = $namedArrayWithFormattedDate;
+    }
+
+    /**
+     * @return \DateTime[]
+     */
+    public function getNamedArrayWithFormattedDate()
+    {
+        return $this->namedArrayWithFormattedDate;
+    }
+}

+ 30 - 1
tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php

@@ -26,6 +26,7 @@ use JMS\Serializer\SerializationContext;
 use JMS\Serializer\Tests\Fixtures\DateTimeArraysObject;
 use JMS\Serializer\Tests\Fixtures\DateTimeArraysObject;
 use JMS\Serializer\Tests\Fixtures\Discriminator\Car;
 use JMS\Serializer\Tests\Fixtures\Discriminator\Car;
 use JMS\Serializer\Tests\Fixtures\InlineChildEmpty;
 use JMS\Serializer\Tests\Fixtures\InlineChildEmpty;
+use JMS\Serializer\Tests\Fixtures\NamedDateTimeArraysObject;
 use JMS\Serializer\Tests\Fixtures\Tree;
 use JMS\Serializer\Tests\Fixtures\Tree;
 use PhpCollection\Sequence;
 use PhpCollection\Sequence;
 use Symfony\Component\Form\FormFactoryBuilder;
 use Symfony\Component\Form\FormFactoryBuilder;
@@ -255,6 +256,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         }
         }
     }
     }
 
 
+
     public function testDateTimeArrays()
     public function testDateTimeArrays()
     {
     {
         $data = array(
         $data = array(
@@ -262,7 +264,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
             new \DateTime('2013-12-05 00:00:00', 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]));
+        $object = new DateTimeArraysObject($data, $data);
         $serializedObject = $this->serialize( $object );
         $serializedObject = $this->serialize( $object );
 
 
         $this->assertEquals($this->getContent('array_datetimes_object'), $serializedObject);
         $this->assertEquals($this->getContent('array_datetimes_object'), $serializedObject);
@@ -276,6 +278,33 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
                 $dateTime->setTimezone(new \DateTimeZone('UTC'));
                 $dateTime->setTimezone(new \DateTimeZone('UTC'));
             foreach ($deserializedObject->getArrayWithFormattedDateTime() as $dateTime)
             foreach ($deserializedObject->getArrayWithFormattedDateTime() as $dateTime)
                 $dateTime->setTimezone(new \DateTimeZone('UTC'));
                 $dateTime->setTimezone(new \DateTimeZone('UTC'));
+
+            $this->assertEquals($object, $deserializedObject);
+        }
+    }
+
+    public function testNamedDateTimeArrays()
+    {
+        $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 NamedDateTimeArraysObject(array('testdate1' => $data[0], 'testdate2' => $data[1]));
+        $serializedObject = $this->serialize( $object );
+
+        $this->assertEquals($this->getContent('array_named_datetimes_object'), $serializedObject);
+
+        if ($this->hasDeserializer()) {
+
+            // skip XML deserialization
+            if ($this->getFormat() === 'xml')
+                return;
+
+            /** @var DateTimeArraysObject $deserializedObject */
+            $deserializedObject = $this->deserialize($this->getContent('array_named_datetimes_object'), 'Jms\Serializer\Tests\Fixtures\NamedDateTimeArraysObject');
+
+            /** 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->getNamedArrayWithFormattedDate() as $dateTime)
             foreach ($deserializedObject->getNamedArrayWithFormattedDate() as $dateTime)
                 $dateTime->setTimezone(new \DateTimeZone('UTC'));
                 $dateTime->setTimezone(new \DateTimeZone('UTC'));
 
 

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

@@ -50,8 +50,9 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['array_objects'] = '[{"foo":"foo","moo":"bar","camel_case":"boo"},{"foo":"baz","moo":"boo","camel_case":"boo"}]';
             $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_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['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['array_named_datetimes_object'] = '{"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,"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,"comments":[],"comments2":[],"metadata":{"foo":"bar"},"author":null}';
             $outputs['price'] = '{"price":3}';
             $outputs['price'] = '{"price":3}';
             $outputs['currency_aware_price'] = '{"currency":"EUR","amount":2.34}';
             $outputs['currency_aware_price'] = '{"currency":"EUR","amount":2.34}';
             $outputs['order'] = '{"cost":{"price":12.34}}';
             $outputs['order'] = '{"cost":{"price":12.34}}';

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

@@ -8,8 +8,4 @@
     <entry><![CDATA[01.01.2047 12:47:47]]></entry>
     <entry><![CDATA[01.01.2047 12:47:47]]></entry>
     <entry><![CDATA[05.12.2013 00:00:00]]></entry>
     <entry><![CDATA[05.12.2013 00:00:00]]></entry>
   </array_with_formatted_date_time>
   </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>
 </result>

+ 7 - 0
tests/JMS/Serializer/Tests/Serializer/xml/array_named_datetimes_object.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result>
+  <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>

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

@@ -4,6 +4,3 @@ array_with_default_date_time:
 array_with_formatted_date_time:
 array_with_formatted_date_time:
     - '01.01.2047 12:47:47'
     - '01.01.2047 12:47:47'
     - '05.12.2013 00:00:00'
     - '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'

+ 3 - 0
tests/JMS/Serializer/Tests/Serializer/yml/array_named_datetimes_object.yml

@@ -0,0 +1,3 @@
+named_array_with_formatted_date:
+    testdate1: '01.01.2047 12:47:47'
+    testdate2: '05.12.2013 00:00:00'