Browse Source

added setSerializeNull to Serializer

mattw 12 years ago
parent
commit
5323bdd69d
2 changed files with 15 additions and 26 deletions
  1. 7 0
      Serializer/Serializer.php
  2. 8 26
      Tests/Serializer/BaseSerializationTest.php

+ 7 - 0
Serializer/Serializer.php

@@ -52,6 +52,13 @@ class Serializer implements SerializerInterface
         $this->deserializationVisitors = $deserializationVisitors;
     }
 
+    public function setSerializeNull($serializeNull)
+    {
+        foreach ($this->serializationVisitors as $visitor) {
+            $visitor->setSerializeNull($serializeNull);
+        }
+    }
+
     public function setExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy = null)
     {
         $this->exclusionStrategy = $exclusionStrategy;

+ 8 - 26
Tests/Serializer/BaseSerializationTest.php

@@ -88,40 +88,22 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
     protected $serializationVisitors;
     protected $deserializationVisitors;
 
-    public function testNullableArray()
+    public function testSerializeNullArray()
     {
         $arr = array('foo' => 'bar', 'baz' => null, null);
 
-        $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
-        $visitors = array(
-            'json' => new JsonSerializationVisitor($namingStrategy),
-            'xml'  => new XmlSerializationVisitor($namingStrategy),
-            'yml'  => new YamlSerializationVisitor($namingStrategy),
-        );
-        foreach($visitors as $v) {
-            $v->setSerializeNull(true);
-        }
-        $serializer = new Serializer($this->factory, $this->handlerRegistry, new UnserializeObjectConstructor(), $this->dispatcher, null, $visitors, $this->deserializationVisitors);
-
-        $this->assertEquals($this->getContent('nullable'), $serializer->serialize($arr, $this->getFormat()));
+        $this->serializer->setSerializeNull(true);
+        $this->assertEquals($this->getContent('nullable'), $this->serializer->serialize($arr, $this->getFormat()));
+        $this->serializer->setSerializeNull(false);
     }
 
-    public function testNullableObject()
+    public function testSerializeNullObject()
     {
         $obj = new ObjectWithNullProperty('foo', 'bar');
 
-        $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
-        $visitors = array(
-            'json' => new JsonSerializationVisitor($namingStrategy),
-            'xml'  => new XmlSerializationVisitor($namingStrategy),
-            'yml'  => new YamlSerializationVisitor($namingStrategy),
-        );
-        foreach($visitors as $v) {
-            $v->setSerializeNull(true);
-        }
-        $serializer = new Serializer($this->factory, $this->handlerRegistry, new UnserializeObjectConstructor(), $this->dispatcher, null, $visitors, $this->deserializationVisitors);
-
-        $this->assertEquals($this->getContent('simple_object_nullable'), $serializer->serialize($obj, $this->getFormat()));
+        $this->serializer->setSerializeNull(true);
+        $this->assertEquals($this->getContent('simple_object_nullable'), $this->serializer->serialize($obj, $this->getFormat()));
+        $this->serializer->setSerializeNull(false);
     }
 
     public function testNull()