Browse Source

added handling for resources (fixes #112)

Johannes M. Schmitt 13 years ago
parent
commit
fd4e2648dd

+ 12 - 0
Serializer/GraphNavigator.php

@@ -65,6 +65,18 @@ final class GraphNavigator
             return $visitor->visitDouble($data, $type);
         } else if ('array' === $type || ('a' === $type[0] && 0 === strpos($type, 'array<'))) {
             return $visitor->visitArray($data, $type);
+        } else if ('resource' === $type) {
+            $path = array();
+            foreach ($this->visiting as $obj) {
+                $path[] = get_class($obj);
+            }
+
+            $msg = 'Resources are not supported in serialized data.';
+            if ($path) {
+                $msg .= ' Path: '.implode(' -> ', $path);
+            }
+
+            throw new \RuntimeException($msg);
         } else {
             if (self::DIRECTION_SERIALIZATION === $this->direction && null !== $data) {
                 if ($this->visiting->contains($data)) {

+ 4 - 2
Tests/Serializer/BaseSerializationTest.php

@@ -456,11 +456,13 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($this->getContent('groups_all'), $serializer->serialize($groupsObject, $this->getFormat()));
     }
 
-    public function testVirtualProperty() {
+    public function testVirtualProperty()
+    {
         $this->assertEquals($this->getContent('virtual_properties'), $this->serialize(new ObjectWithVirtualProperties()));
     }
 
-    public function testVirtualVersions() {
+    public function testVirtualVersions()
+    {
         $serializer = $this->getSerializer();
 
         $serializer->setVersion(2);

+ 32 - 0
Tests/Serializer/GraphNavigatorTest.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace JMS\SerializerBundle\Tests\Serializer;
+
+use Doctrine\Common\Annotations\AnnotationReader;
+use JMS\SerializerBundle\Metadata\Driver\AnnotationDriver;
+use JMS\SerializerBundle\Serializer\GraphNavigator;
+use Metadata\MetadataFactory;
+
+class GraphNavigatorTest extends \PHPUnit_Framework_TestCase
+{
+    private $metadataFactory;
+    private $navigator;
+    private $visitor;
+
+    /**
+     * @expectedException \RuntimeException
+     * @expectedExceptionMessage Resources are not supported in serialized data.
+     */
+    public function testResourceThrowsException()
+    {
+        $this->navigator->accept(STDIN, null, $this->visitor);
+    }
+
+    protected function setUp()
+    {
+        $this->visitor = $this->getMock('JMS\SerializerBundle\Serializer\VisitorInterface');
+
+        $this->metadataFactory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
+        $this->navigator = new GraphNavigator(GraphNavigator::DIRECTION_SERIALIZATION, $this->metadataFactory);
+    }
+}