浏览代码

[Builder] Add ability to include if metadata

Victor 12 年之前
父节点
当前提交
9c2f8dda33
共有 2 个文件被更改,包括 50 次插入6 次删除
  1. 16 1
      src/JMS/Serializer/SerializerBuilder.php
  2. 34 5
      tests/JMS/Serializer/Tests/SerializerBuilderTest.php

+ 16 - 1
src/JMS/Serializer/SerializerBuilder.php

@@ -47,6 +47,7 @@ class SerializerBuilder
     private $debug = false;
     private $cacheDir;
     private $annotationReader;
+    private $includeInterfaceMetadata = false;
 
     public static function create()
     {
@@ -180,6 +181,18 @@ class SerializerBuilder
         return $this;
     }
 
+    /**
+     * @param Boolean $include Whether to include the metadata from the interfaces
+     *
+     * @return SerializerBuilder
+     */
+    public function includeInterfaceMetadata($include)
+    {
+        $this->includeInterfaceMetadata = (Boolean) $include;
+
+        return $this;
+    }
+
     /**
      * Sets a map of namespace prefixes to directories.
      *
@@ -304,6 +317,8 @@ class SerializerBuilder
 
         $metadataFactory = new MetadataFactory($metadataDriver, null, $this->debug);
 
+        $metadataFactory->setIncludeInterfaces($this->includeInterfaceMetadata);
+
         if (null !== $this->cacheDir) {
             $this->createDir($this->cacheDir.'/metadata');
             $metadataFactory->setCache(new FileCache($this->cacheDir.'/metadata'));
@@ -351,4 +366,4 @@ class SerializerBuilder
             throw new \RuntimeException(sprintf('Could not create directory "%s".', $dir));
         }
     }
-}
+}

+ 34 - 5
tests/JMS/Serializer/Tests/SerializerBuilderTest.php

@@ -75,12 +75,27 @@ class SerializerBuilderTest extends \PHPUnit_Framework_TestCase
         $this->builder->build()->serialize('foo', 'xml');
     }
 
-    private function getField($obj, $name)
+    public function testIncludeInterfaceMetadata()
     {
-        $ref = new \ReflectionProperty($obj, $name);
-        $ref->setAccessible(true);
+        $this->assertFalse(
+            $this->getIncludeInterfaces($this->builder),
+            'Interface metadata are not included by default'
+        );
 
-        return $ref->getValue($obj);
+        $this->assertTrue(
+            $this->getIncludeInterfaces($this->builder->includeInterfaceMetadata(true)),
+            'Force including interface metadata'
+        );
+
+        $this->assertFalse(
+            $this->getIncludeInterfaces($this->builder->includeInterfaceMetadata(false)),
+            'Force not including interface metadata'
+        );
+
+        $this->assertSame(
+            $this->builder,
+            $this->builder->includeInterfaceMetadata(true)
+        );
     }
 
     protected function setUp()
@@ -97,4 +112,18 @@ class SerializerBuilderTest extends \PHPUnit_Framework_TestCase
     {
         $this->fs->remove($this->tmpDir);
     }
-}
+
+    private function getField($obj, $name)
+    {
+        $ref = new \ReflectionProperty($obj, $name);
+        $ref->setAccessible(true);
+
+        return $ref->getValue($obj);
+    }
+
+    private function getIncludeInterfaces(SerializerBuilder $builder)
+    {
+        $factory = $this->getField($builder->build(), 'factory');
+        return $this->getField($factory, 'includeInterfaces');
+    }
+}