瀏覽代碼

increased test coverage

Johannes Schmitt 14 年之前
父節點
當前提交
345b1adca7

+ 8 - 8
Serializer/GenericDeserializationVisitor.php

@@ -119,12 +119,12 @@ abstract class GenericDeserializationVisitor extends AbstractVisitor
             $listType = substr($type, 6, -1);
 
             $result = array();
-            foreach ($data as $v) {
-                $result[] = $this->navigator->accept($v, $listType, $this);
+            if (null === $this->result) {
+                $this->result = &$result;
             }
 
-            if (null === $this->result) {
-                $this->result = $result;
+            foreach ($data as $v) {
+                $result[] = $this->navigator->accept($v, $listType, $this);
             }
 
             return $result;
@@ -135,12 +135,12 @@ abstract class GenericDeserializationVisitor extends AbstractVisitor
         $entryType = trim(substr($type, $pos+1, -1));
 
         $result = array();
-        foreach ($data as $k => $v) {
-            $result[$this->navigator->accept($k, $keyType, $this)] = $this->navigator->accept($v, $entryType, $this);
+        if (null === $this->result) {
+            $this->result = &$result;
         }
 
-        if (null === $this->result) {
-            $this->result = $result;
+        foreach ($data as $k => $v) {
+            $result[$this->navigator->accept($k, $keyType, $this)] = $this->navigator->accept($v, $entryType, $this);
         }
 
         return $result;

+ 6 - 2
Serializer/Handler/DateTimeHandler.php

@@ -18,6 +18,10 @@
 
 namespace JMS\SerializerBundle\Serializer\Handler;
 
+use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;
+
+use JMS\SerializerBundle\Serializer\GenericDeserializationVisitor;
+
 use JMS\SerializerBundle\Exception\RuntimeException;
 use JMS\SerializerBundle\Serializer\GenericSerializationVisitor;
 use JMS\SerializerBundle\Serializer\JsonSerializationVisitor;
@@ -57,11 +61,11 @@ class DateTimeHandler implements SerializationHandlerInterface, DeserializationH
 
     public function deserialize(VisitorInterface $visitor, $data, $type, &$visited)
     {
-        if ('datetime' !== $type) {
+        if ('DateTime' !== $type) {
             return;
         }
 
-        if ($visitor instanceof GenericSerializationVisitor) {
+        if ($visitor instanceof GenericDeserializationVisitor || $visitor instanceof XmlDeserializationVisitor) {
             $datetime = \DateTime::createFromFormat($this->format, (string) $data, $this->defaultTimezone);
             if (false === $datetime) {
                 throw new RuntimeException(sprintf('Invalid datetime "%s", expected format %s.', $data, $this->format));

+ 17 - 9
Serializer/XmlDeserializationVisitor.php

@@ -80,7 +80,15 @@ class XmlDeserializationVisitor extends AbstractVisitor
 
     public function visitBoolean($data, $type)
     {
-        $data = (Boolean) $data;
+        $data = (string) $data;
+
+        if ('true' === $data) {
+            $data = true;
+        } else if ('false' === $data) {
+            $data = false;
+        } else {
+            throw new \RuntimeException(sprintf('Could not convert data to boolean. Expected "true", or "false", but got %s.', json_encode($data)));
+        }
 
         if (null === $this->result) {
             $this->result = $data;
@@ -131,12 +139,12 @@ class XmlDeserializationVisitor extends AbstractVisitor
             $listType = substr($type, 6, -1);
 
             $result = array();
-            foreach ($data->$entryName as $v) {
-                $result[] = $this->navigator->accept($v, $listType, $this);
+            if (null === $this->result) {
+                $this->result = &$result;
             }
 
-            if (null === $this->result) {
-                $this->result = $result;
+            foreach ($data->$entryName as $v) {
+                $result[] = $this->navigator->accept($v, $listType, $this);
             }
 
             return $result;
@@ -149,6 +157,10 @@ class XmlDeserializationVisitor extends AbstractVisitor
         $keyType = trim(substr($type, 6, $pos - 6));
         $entryType = trim(substr($type, $pos+1, -1));
         $result = array();
+        if (null === $this->result) {
+            $this->result = &$result;
+        }
+
         foreach ($data->$entryName as $v) {
             if (!isset($v[$this->currentMetadata->xmlKeyAttribute])) {
                 throw new RuntimeException(sprintf('The key attribute "%s" must be set for each entry of the map.', $metadata->xmlKeyAttribute));
@@ -158,10 +170,6 @@ class XmlDeserializationVisitor extends AbstractVisitor
             $result[$k] = $this->navigator->accept($v, $entryType, $this);
         }
 
-        if (null === $this->result) {
-            $this->result = $result;
-        }
-
         return $result;
     }
 

+ 4 - 0
Tests/Fixtures/SimpleObject.php

@@ -19,16 +19,20 @@
 namespace JMS\SerializerBundle\Tests\Fixtures;
 
 use JMS\SerializerBundle\Annotation\SerializedName;
+use JMS\SerializerBundle\Annotation\Type;
 
 class SimpleObject
 {
+    /** @Type("string") */
     private $foo;
 
     /**
      * @SerializedName("moo")
+     * @Type("string")
      */
     private $bar;
 
+    /** @Type("string") */
     protected $camelCase = 'boo';
 
     public function __construct($foo, $bar)

+ 15 - 5
Tests/PerformanceTest.php

@@ -39,23 +39,31 @@ use JMS\SerializerBundle\Serializer\Serializer;
 use Symfony\Component\Serializer\Encoder\JsonEncoder;
 use Symfony\Component\Serializer\Encoder\XmlEncoder;
 
+/**
+ * @group performance
+ */
 class PerformanceTest extends \PHPUnit_Framework_TestCase
 {
     /**
-     * @group performance
+     * @dataProvider getFormats
      */
-    public function testNormalizationPerformance()
+    public function testSerializationPerformance($format)
     {
         $serializer = $this->getSerializer();
         $testData   = $this->getTestCollection();
 
         $time = microtime(true);
         for ($i=0,$c=10; $i<$c; $i++) {
-            $serializer->normalize($testData);
+            $serializer->serialize($testData, $format);
         }
         $time = microtime(true) - $time;
 
-        $this->printResults('normalization', $time, $c);
+        $this->printResults("serialization ($format)", $time, $c);
+    }
+
+    public function getFormats()
+    {
+        return array(array('json'), array('xml'));
     }
 
     private function getTestCollection()
@@ -70,7 +78,7 @@ class PerformanceTest extends \PHPUnit_Framework_TestCase
 
     private function getTestObject()
     {
-        $post = new BlogPost('FooooooooooooooooooooooBAR', new Author('Foo'));
+        $post = new BlogPost('FooooooooooooooooooooooBAR', new Author('Foo'), new \DateTime);
         for ($i=0; $i<10; $i++) {
             $post->addComment(new Comment(new Author('foo'), 'foobar'));
         }
@@ -82,6 +90,8 @@ class PerformanceTest extends \PHPUnit_Framework_TestCase
     {
         $container = new ContainerBuilder();
         $container->set('annotation_reader', new AnnotationReader());
+        $container->setParameter('kernel.debug', true);
+        $container->setParameter('kernel.cache_dir', sys_get_temp_dir());
         $extension = new JMSSerializerExtension();
         $extension->load(array(array()), $container);
 

+ 37 - 15
Tests/Serializer/BaseSerializationTest.php

@@ -18,22 +18,16 @@
 
 namespace JMS\SerializerBundle\Tests\Serializer;
 
-use JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface;
+use Doctrine\Common\Collections\ArrayCollection;
 
+use JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface;
 use JMS\SerializerBundle\Tests\Fixtures\AuthorList;
-
 use JMS\SerializerBundle\Serializer\VisitorInterface;
-
 use JMS\SerializerBundle\Serializer\Handler\ArrayCollectionHandler;
-
 use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;
-
 use JMS\SerializerBundle\Serializer\Construction\UnserializeObjectConstructor;
-
 use JMS\SerializerBundle\Serializer\JsonDeserializationVisitor;
-
 use JMS\SerializerBundle\Tests\Fixtures\Log;
-
 use JMS\SerializerBundle\Serializer\Handler\DateTimeHandler;
 use JMS\SerializerBundle\Tests\Fixtures\Comment;
 use JMS\SerializerBundle\Tests\Fixtures\Author;
@@ -55,6 +49,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
     public function testString()
     {
         $this->assertEquals($this->getContent('string'), $this->serialize('foo'));
+        $this->assertEquals('foo', $this->deserialize($this->getContent('string'), 'string'));
     }
 
     /**
@@ -63,6 +58,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
     public function testBooleans($strBoolean, $boolean)
     {
         $this->assertEquals($this->getContent('boolean_'.$strBoolean), $this->serialize($boolean));
+        $this->assertSame($boolean, $this->deserialize($this->getContent('boolean_'.$strBoolean), 'boolean'));
     }
 
     public function getBooleans()
@@ -76,6 +72,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
     public function testNumerics($key, $value)
     {
         $this->assertEquals($this->getContent($key), $this->serialize($value));
+        $this->assertEquals($value, $this->deserialize($this->getContent($key), is_double($value) ? 'double' : 'integer'));
     }
 
     public function getNumerics()
@@ -89,32 +86,43 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
 
     public function testSimpleObject()
     {
-        $this->assertEquals($this->getContent('simple_object'), $this->serialize(new SimpleObject('foo', 'bar')));
+        $this->assertEquals($this->getContent('simple_object'), $this->serialize($obj = new SimpleObject('foo', 'bar')));
+        $this->assertEquals($obj, $this->deserialize($this->getContent('simple_object'), get_class($obj)));
     }
 
     public function testArrayStrings()
     {
-        $this->assertEquals($this->getContent('array_strings'), $this->serialize(array('foo', 'bar')));
+        $data = array('foo', 'bar');
+        $this->assertEquals($this->getContent('array_strings'), $this->serialize($data));
+        $this->assertEquals($data, $this->deserialize($this->getContent('array_strings'), 'array<string>'));
     }
 
     public function testArrayBooleans()
     {
-        $this->assertEquals($this->getContent('array_booleans'), $this->serialize(array(true, false)));
+        $data = array(true, false);
+        $this->assertEquals($this->getContent('array_booleans'), $this->serialize($data));
+        $this->assertEquals($data, $this->deserialize($this->getContent('array_booleans'), 'array<boolean>'));
     }
 
     public function testArrayIntegers()
     {
-        $this->assertEquals($this->getContent('array_integers'), $this->serialize(array(1, 3, 4)));
+        $data = array(1, 3, 4);
+        $this->assertEquals($this->getContent('array_integers'), $this->serialize($data));
+        $this->assertEquals($data, $this->deserialize($this->getContent('array_integers'), 'array<integer>'));
     }
 
     public function testArrayFloats()
     {
-        $this->assertEquals($this->getContent('array_floats'), $this->serialize(array(1.34, 3.0, 6.42)));
+        $data = array(1.34, 3.0, 6.42);
+        $this->assertEquals($this->getContent('array_floats'), $this->serialize($data));
+        $this->assertEquals($data, $this->deserialize($this->getContent('array_floats'), 'array<double>'));
     }
 
     public function testArrayObjects()
     {
-        $this->assertEquals($this->getContent('array_objects'), $this->serialize(array(new SimpleObject('foo', 'bar'), new SimpleObject('baz', 'boo'))));
+        $data = array(new SimpleObject('foo', 'bar'), new SimpleObject('baz', 'boo'));
+        $this->assertEquals($this->getContent('array_objects'), $this->serialize($data));
+        $this->assertEquals($data, $this->deserialize($this->getContent('array_objects'), 'array<JMS\SerializerBundle\Tests\Fixtures\SimpleObject>'));
     }
 
     public function testArrayMixed()
@@ -125,9 +133,16 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
     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')));
-        $post->addComment(new Comment($author, 'foo'));
+        $post->addComment($comment = new Comment($author, 'foo'));
 
         $this->assertEquals($this->getContent('blog_post'), $this->serialize($post));
+
+        $deserialized = $this->deserialize($this->getContent('blog_post'), 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(array($comment)), 'comments', $deserialized);
+        $this->assertAttributeEquals($author, 'author', $deserialized);
     }
 
     /**
@@ -218,6 +233,13 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
 
         return $ref->getValue($obj);
     }
+
+    private function setField($obj, $name, $value)
+    {
+        $ref = new \ReflectionProperty($obj, $name);
+        $ref->setAccessible(true);
+        $ref->setValue($obj, $value);
+    }
 }
 
 class AuthorListDeserializationHandler implements DeserializationHandlerInterface

+ 1 - 1
phpunit.xml.dist

@@ -20,7 +20,7 @@
     
     <groups>
         <exclude>
-	    <group>performance</group>
+            <group>performance</group>
         </exclude>
     </groups>
 </phpunit>