瀏覽代碼

[Form] Fixed BooleanToStringTransformer::reverseTransform() to accept NULL values. Fixes ChoiceTypeTest

Bernhard Schussek 14 年之前
父節點
當前提交
94f2baa895

+ 4 - 0
src/Symfony/Component/Form/DataTransformer/BooleanToStringTransformer.php

@@ -48,6 +48,10 @@ class BooleanToStringTransformer implements DataTransformerInterface
      */
     public function reverseTransform($value)
     {
+        if (null === $value) {
+            return false;
+        }
+
         if (!is_string($value)) {
             throw new UnexpectedTypeException($value, 'string');
         }

+ 5 - 4
tests/Symfony/Tests/Component/Form/DataTransformer/BooleanToStringTransformerTest.php

@@ -40,13 +40,14 @@ class BooleanToStringTransformerTest extends \PHPUnit_Framework_TestCase
     {
         $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
 
-        $this->transformer->reverseTransform(1, null);
+        $this->transformer->reverseTransform(1);
     }
 
     public function testReverseTransform()
     {
-        $this->assertEquals(true, $this->transformer->reverseTransform('1', null));
-        $this->assertEquals(true, $this->transformer->reverseTransform('0', null));
-        $this->assertEquals(false, $this->transformer->reverseTransform('', null));
+        $this->assertEquals(true, $this->transformer->reverseTransform('1'));
+        $this->assertEquals(true, $this->transformer->reverseTransform('0'));
+        $this->assertEquals(false, $this->transformer->reverseTransform(''));
+        $this->assertEquals(false, $this->transformer->reverseTransform(null));
     }
 }