Ver código fonte

merged branch Seldaek/response (PR #1387)

Commits
-------

7af003b [HttpFoundation] Allow stringable objects and numbers in response body + added tests
8126fb7 [HttpFoundation] Ensure response body is string, fixes #1378

Discussion
----------

[HttpFoundation] Ensure response body is string, fixes #1378

The issue in #1378 is clearly just a misuse of the class due but the error it triggers is confusing. This hopes to fix that.

---------------------------------------------------------------------------

by Seldaek at 2011/06/21 04:08:40 -0700

Added tests and support for __toString + numbers (kinda pointless but well..)
Fabien Potencier 14 anos atrás
pai
commit
b2b0c4a98f

+ 6 - 1
src/Symfony/Component/HttpFoundation/Response.php

@@ -161,10 +161,15 @@ class Response
     /**
     /**
      * Sets the response content
      * Sets the response content
      *
      *
-     * @param string $content
+     * Valid types are strings, numbers, and objects that implement a __toString() method.
+     *
+     * @param mixed $content
      */
      */
     public function setContent($content)
     public function setContent($content)
     {
     {
+        if (!is_string($content) && !is_numeric($content) && !is_callable(array($content, '__toString'))) {
+            throw new \UnexpectedValueException('The Response content must be a string or object implementing __toString(), "'.gettype($content).'" given.');
+        }
         $this->content = $content;
         $this->content = $content;
     }
     }
 
 

+ 46 - 0
tests/Symfony/Tests/Component/HttpFoundation/ResponseTest.php

@@ -424,6 +424,44 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
         $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
         $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
     }
     }
 
 
+    /**
+     * @dataProvider validContentProvider
+     */
+    public function testSetContent($content)
+    {
+        $response = new Response();
+        $response->setContent($content);
+        $this->assertEquals($content, $response->getContent());
+    }
+
+    /**
+     * @expectedException UnexpectedValueException
+     * @dataProvider invalidContentProvider
+     */
+    public function testSetContentInvalid($content)
+    {
+        $response = new Response();
+        $response->setContent($content);
+    }
+
+    public function validContentProvider()
+    {
+        return array(
+            'obj'    => array(new StringableObject),
+            'string' => array('Foo'),
+            'int'    => array(2),
+        );
+    }
+
+    public function invalidContentProvider()
+    {
+        return array(
+            'obj'   => array(new \stdClass),
+            'array' => array(array()),
+            'bool'   => array(true, '1'),
+        );
+    }
+
     protected function createDateTimeOneHourAgo()
     protected function createDateTimeOneHourAgo()
     {
     {
         $date = new \DateTime();
         $date = new \DateTime();
@@ -443,3 +481,11 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
         return new \DateTime();
         return new \DateTime();
     }
     }
 }
 }
+
+class StringableObject
+{
+    public function __toString()
+    {
+        return 'Foo';
+    }
+}