Просмотр исходного кода

[BrowserKit] added a __toString() method to the Client to ease debugging

Fabien Potencier 15 лет назад
Родитель
Сommit
20527a0a5b

+ 15 - 2
src/Symfony/Components/BrowserKit/Response.php

@@ -41,6 +41,21 @@ class Response
     $this->cookies = $cookies;
   }
 
+  public function __toString()
+  {
+    $headers = '';
+    foreach ($this->headers as $name => $value)
+    {
+      $headers .= sprintf("%s: %s\n", $name, $value);
+    }
+    foreach ($this->cookies as $name => $cookie)
+    {
+      $headers .= sprintf("Set-Cookie: %s=%s\n", $name, $cookie['value']);
+    }
+
+    return $headers."\n".$this->content;
+  }
+
   /**
    * Gets the response content.
    *
@@ -87,8 +102,6 @@ class Response
         return $value;
       }
     }
-
-    return null;
   }
 
   /**

+ 7 - 0
tests/Symfony/Tests/Components/BrowserKit/ResponseTest.php

@@ -49,4 +49,11 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
     $response = new Response('foo', 304, array(), array('foo' => 'bar'));
     $this->assertEquals(array('foo' => 'bar'), $response->getCookies(), '->getCookies() returns the cookies of the response');
   }
+
+  public function testMagicToString()
+  {
+    $response = new Response('foo', 304, array('foo' => 'bar'), array('foo' => array('value' => 'bar')));
+
+    $this->assertEquals("foo: bar\nSet-Cookie: foo=bar\n\nfoo", $response->__toString(), '->__toString() returns the headers and the content as a string');
+  }
 }