瀏覽代碼

Changed xml and yml drivers in such way that only excluded or exposed
properties has to be written into the config.

Miha Vrhovnik 13 年之前
父節點
當前提交
10a2516678

+ 4 - 4
Metadata/Driver/XmlDriver.php

@@ -141,12 +141,12 @@ class XmlDriver extends AbstractFileDriver
                         (string) ($pElem->attributes()->{'access-type'} ?: $classAccessType),
                         $accessor ? (string) $accessor : null
                     );
+                }
 
-                    if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)
-                        || (ExclusionPolicy::ALL === (string)$exclusionPolicy && $isExpose)) {
+                if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)
+                    || (ExclusionPolicy::ALL === (string)$exclusionPolicy && $isExpose)) {
 
-                        $metadata->addPropertyMetadata($pMetadata);
-                    }
+                    $metadata->addPropertyMetadata($pMetadata);
                 }
             }
         }

+ 4 - 5
Metadata/Driver/YamlDriver.php

@@ -129,11 +129,10 @@ class YamlDriver extends AbstractFileDriver
                         isset($pConfig['access_type']) ? $pConfig['access_type'] : $classAccessType,
                         isset($pConfig['accessor']) ? $pConfig['accessor'] : null
                     );
-
-                    if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
-                    || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
-                        $metadata->addPropertyMetadata($pMetadata);
-                    }
+                }
+                if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
+                || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
+                    $metadata->addPropertyMetadata($pMetadata);
                 }
             }
         }

+ 30 - 1
Tests/Metadata/Driver/XmlDriverTest.php

@@ -19,6 +19,7 @@
 namespace JMS\SerializerBundle\Tests\Metadata\Driver;
 
 use Metadata\Driver\FileLocator;
+use JMS\SerializerBundle\Metadata\PropertyMetadata;
 use JMS\SerializerBundle\Metadata\Driver\XmlDriver;
 
 class XmlDriverTest extends BaseDriverTest
@@ -36,10 +37,38 @@ class XmlDriverTest extends BaseDriverTest
         $ref->invoke($driver, new \ReflectionClass('stdClass'), __DIR__.'/xml/invalid.xml');
     }
 
+    public function testBlogPostExcludeAllStrategy()
+    {
+        $m = $this->getDriver('exclude_all')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
+
+        $this->assertArrayHasKey('title', $m->propertyMetadata);
+
+        $excluded = array('createdAt', 'published', 'comments', 'author');
+        foreach ($excluded as $key) {
+            $this->assertArrayNotHasKey($key, $m->propertyMetadata);
+        }
+    }
+
+    public function testBlogPostExcludeNoneStrategy()
+    {
+        $m = $this->getDriver('exclude_none')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
+
+        $this->assertArrayNotHasKey('title', $m->propertyMetadata);
+
+        $excluded = array('createdAt', 'published', 'comments', 'author');
+        foreach ($excluded as $key) {
+            $this->assertArrayHasKey($key, $m->propertyMetadata);
+        }
+    }
+
     protected function getDriver()
     {
+        $append = '';
+        if (func_num_args() == 1) {
+            $append = '/'.func_get_arg(0);
+        }
         return new XmlDriver(new FileLocator(array(
-            'JMS\SerializerBundle\Tests\Fixtures' => __DIR__.'/xml',
+            'JMS\SerializerBundle\Tests\Fixtures' => __DIR__.'/xml'.$append,
         )));
     }
 }

+ 30 - 1
Tests/Metadata/Driver/YamlDriverTest.php

@@ -23,10 +23,39 @@ use JMS\SerializerBundle\Metadata\Driver\YamlDriver;
 
 class YamlDriverTest extends BaseDriverTest
 {
+    public function testBlogPostExcludeAllStrategy()
+    {
+        $m = $this->getDriver('exclude_all')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
+
+        $this->assertArrayHasKey('title', $m->propertyMetadata);
+
+        $excluded = array('createdAt', 'published', 'comments', 'author');
+        foreach ($excluded as $key) {
+            $this->assertArrayNotHasKey($key, $m->propertyMetadata);
+        }
+    }
+
+    public function testBlogPostExcludeNoneStrategy()
+    {
+        $m = $this->getDriver('exclude_none')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
+
+        $this->assertArrayNotHasKey('title', $m->propertyMetadata);
+
+        $excluded = array('createdAt', 'published', 'comments', 'author');
+        foreach ($excluded as $key) {
+            $this->assertArrayHasKey($key, $m->propertyMetadata);
+        }
+    }
+
     protected function getDriver()
     {
+        $append = '';
+        if (func_num_args() == 1) {
+            $append = '/'.func_get_arg(0);
+        }
+
         return new YamlDriver(new FileLocator(array(
-            'JMS\SerializerBundle\Tests\Fixtures' => __DIR__.'/yml',
+            'JMS\SerializerBundle\Tests\Fixtures' => __DIR__.'/yml'.$append,
         )));
     }
 }

+ 6 - 0
Tests/Metadata/Driver/xml/exclude_all/BlogPost.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<serializer>
+    <class name="JMS\SerializerBundle\Tests\Fixtures\BlogPost" xml-root-name="blog-post" exclusion-policy="ALL">
+        <property name="title" type="string" expose="true" />
+    </class>
+</serializer>

+ 6 - 0
Tests/Metadata/Driver/xml/exclude_none/BlogPost.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<serializer>
+    <class name="JMS\SerializerBundle\Tests\Fixtures\BlogPost" xml-root-name="blog-post" exclusion-policy="NONE">
+        <property name="title" type="string" exclude="true" />
+    </class>
+</serializer>

+ 7 - 0
Tests/Metadata/Driver/yml/exclude_all/BlogPost.yml

@@ -0,0 +1,7 @@
+JMS\SerializerBundle\Tests\Fixtures\BlogPost:
+    xml_root_name: blog-post
+    exclusion_policy: ALL
+    properties:
+        title:
+            type: string
+            expose: true

+ 7 - 0
Tests/Metadata/Driver/yml/exclude_none/BlogPost.yml

@@ -0,0 +1,7 @@
+JMS\SerializerBundle\Tests\Fixtures\BlogPost:
+    xml_root_name: blog-post
+    exclusion_policy: NONE
+    properties:
+        title:
+            type: string
+            exclude: true