瀏覽代碼

Testing collections of polymorphic objects

David Simon 11 年之前
父節點
當前提交
dc6066a3af

+ 33 - 0
tests/JMS/Serializer/Tests/Fixtures/Garage.php

@@ -0,0 +1,33 @@
+<?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;
+
+class Garage
+{
+    /**
+     * @Type("array<JMS\Serializer\Tests\Fixtures\Discriminator\Vehicle>")
+     */
+    public $vehicles;
+
+    public function __construct($vehicles) {
+        $this->vehicles = $vehicles;
+    }
+}

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

@@ -25,6 +25,8 @@ 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\Discriminator\Moped;
+use JMS\Serializer\Tests\Fixtures\Garage;
 use JMS\Serializer\Tests\Fixtures\InlineChildEmpty;
 use JMS\Serializer\Tests\Fixtures\NamedDateTimeArraysObject;
 use JMS\Serializer\Tests\Fixtures\Tree;
@@ -820,6 +822,28 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         }
     }
 
+    /**
+     * @group polymorphic
+     */
+    public function testNestedPolymorphicObjects()
+    {
+        $garage = new Garage(array(new Car(3), new Moped(1)));
+        $this->assertEquals(
+            $this->getContent('garage'),
+            $this->serialize($garage)
+        );
+
+        if ($this->hasDeserializer()) {
+            $this->assertEquals(
+                $garage,
+                $this->deserialize(
+                    $this->getContent('garage'),
+                    'JMS\Serializer\Tests\Fixtures\Garage'
+                )
+            );
+        }
+    }
+
     /**
      * @group polymorphic
      * @expectedException LogicException

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

@@ -91,6 +91,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['date_interval'] = '"PT45M"';
             $outputs['car'] = '{"km":5,"type":"car"}';
             $outputs['car_without_type'] = '{"km":5}';
+            $outputs['garage'] = '{"vehicles":[{"km":3,"type":"car"},{"km":1,"type":"moped"}]}';
             $outputs['tree'] = '{"tree":{"children":[{"children":[{"children":[],"foo":"bar"}],"foo":"bar"}],"foo":"bar"}}';
         }
 

+ 13 - 0
tests/JMS/Serializer/Tests/Serializer/xml/garage.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result>
+  <vehicles>
+    <entry>
+      <km>3</km>
+      <type><![CDATA[car]]></type>
+    </entry>
+    <entry>
+      <km>1</km>
+      <type><![CDATA[moped]]></type>
+    </entry>
+  </vehicles>
+</result>

+ 7 - 0
tests/JMS/Serializer/Tests/Serializer/yml/garage.yml

@@ -0,0 +1,7 @@
+vehicles:
+    -
+        km: 3
+        type: car
+    -
+        km: 1
+        type: moped