Procházet zdrojové kódy

Updated SerializerTest with "normalizeTraversable" & "testNormalizeGivesPriorityToInterfaceOverTraversable"

Eric Clemmons před 13 roky
rodič
revize
e851efc8d6

+ 25 - 0
tests/Symfony/Tests/Component/Serializer/Fixtures/NormalizableTraversableDummy.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace Symfony\Tests\Component\Serializer\Fixtures;
+
+use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
+use Symfony\Component\Serializer\SerializerInterface;
+
+class NormalizableTraversableDummy extends TraversableDummy implements NormalizableInterface
+{
+    public function normalize(SerializerInterface $serializer, $format = null)
+    {
+        return array(
+            'foo' => 'normalizedFoo',
+            'bar' => 'normalizedBar',
+        );
+    }
+
+    public function denormalize(SerializerInterface $serializer, $data, $format = null)
+    {
+        return array(
+            'foo' => 'denormalizedFoo',
+            'bar' => 'denormalizedBar',
+        );
+    }
+}

+ 14 - 0
tests/Symfony/Tests/Component/Serializer/Fixtures/TraversableDummy.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace Symfony\Tests\Component\Serializer\Fixtures;
+
+class TraversableDummy implements \IteratorAggregate
+{
+    public $foo = 'foo';
+    public $bar = 'bar';
+
+    public function getIterator()
+    {
+        return new \ArrayIterator(get_object_vars($this));
+    }
+}

+ 17 - 0
tests/Symfony/Tests/Component/Serializer/SerializerTest.php

@@ -4,6 +4,9 @@ namespace Symfony\Tests\Component\Serializer;
 
 use Symfony\Component\Serializer\Serializer;
 use Symfony\Component\Serializer\Encoder\JsonEncoder;
+use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
+use Symfony\Tests\Component\Serializer\Fixtures\TraversableDummy;
+use Symfony\Tests\Component\Serializer\Fixtures\NormalizableTraversableDummy;
 
 /*
  * This file is part of the Symfony framework.
@@ -25,6 +28,20 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
         $this->serializer->normalize(new \stdClass, 'xml');
     }
 
+    public function testNormalizeTraversable()
+    {
+        $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
+        $result = $this->serializer->serialize(new TraversableDummy, 'json');
+        $this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
+    }
+
+    public function testNormalizeGivesPriorityToInterfaceOverTraversable()
+    {
+        $this->serializer = new Serializer(array(new CustomNormalizer), array('json' => new JsonEncoder()));
+        $result = $this->serializer->serialize(new NormalizableTraversableDummy, 'json');
+        $this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
+    }
+
     /**
      * @expectedException \UnexpectedValueException
      */