From 1f8396a2da824ea255402f704e856c1ca699889e Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 30 Jul 2015 20:07:38 +0200 Subject: [PATCH] [Debug] PSR-7 support --- composer.json | 1 + .../Component/Debug/ExceptionHandler.php | 17 +++++++- .../Debug/Tests/ExceptionHandlerTest.php | 41 +++++++++++++++++++ src/Symfony/Component/Debug/composer.json | 1 + 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 1b7d12551747f..89b9e3ced9cc0 100644 --- a/composer.json +++ b/composer.json @@ -76,6 +76,7 @@ "monolog/monolog": "~1.11", "ircmaxell/password-compat": "~1.0", "ocramius/proxy-manager": "~0.4|~1.0", + "psr/http-message": "~1.0", "egulias/email-validator": "~1.2" }, "autoload": { diff --git a/src/Symfony/Component/Debug/ExceptionHandler.php b/src/Symfony/Component/Debug/ExceptionHandler.php index c883f15469e0b..c1b6e0109d673 100644 --- a/src/Symfony/Component/Debug/ExceptionHandler.php +++ b/src/Symfony/Component/Debug/ExceptionHandler.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Debug; +use Psr\Http\Message\ResponseInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Debug\Exception\FlattenException; use Symfony\Component\Debug\Exception\OutOfMemoryException; @@ -197,15 +198,27 @@ public function sendPhpResponse($exception) * Creates the error Response associated with the given Exception. * * @param \Exception|FlattenException $exception An \Exception instance + * @param ResponseInterface $response An optional prototype PSR-7 response * - * @return Response A Response instance + * @return Response|ResponseInterface A response instance */ - public function createResponse($exception) + public function createResponse($exception, ResponseInterface $response = null) { if (!$exception instanceof FlattenException) { $exception = FlattenException::create($exception); } + if (null !== $response) { + $response = $response->withStatus($exception->getStatusCode()); + foreach ($exception->getHeaders() as $k => $v) { + $response = $response->withAddedHeader($k, $v); + } + $response = $response->withHeader('Content-Type', 'text/html; charset='.$this->charset); + $response->getBody()->write($this->decorate($this->getContent($exception), $this->getStylesheet($exception))); + + return $response; + } + return Response::create($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders())->setCharset($this->charset); } diff --git a/src/Symfony/Component/Debug/Tests/ExceptionHandlerTest.php b/src/Symfony/Component/Debug/Tests/ExceptionHandlerTest.php index 26f889288ff93..dafd3d70c0d4f 100644 --- a/src/Symfony/Component/Debug/Tests/ExceptionHandlerTest.php +++ b/src/Symfony/Component/Debug/Tests/ExceptionHandlerTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Debug\Tests; use Symfony\Component\Debug\ExceptionHandler; +use Symfony\Component\Debug\Exception\FlattenException; use Symfony\Component\Debug\Exception\OutOfMemoryException; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -89,6 +90,46 @@ public function testHandle() $handler->handle($exception); } + public function testPsr7() + { + $exception = FlattenException::create(new \Exception('foo'), 123, array('Header' => 'Value')); + + $body = $this->getMock('Psr\Http\Message\StreamInterface'); + $body + ->expects($this->exactly(1)) + ->method('write') + ->with($this->stringContains('Whoops, looks like something went wrong.')); + + $response0 = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response1 = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response2 = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response3 = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response0 + ->expects($this->exactly(1)) + ->method('withStatus') + ->with(123) + ->will($this->returnValue($response1)); + $response1 + ->expects($this->exactly(1)) + ->method('withAddedHeader') + ->with('Header', 'Value') + ->will($this->returnValue($response2)); + $response2 + ->expects($this->exactly(1)) + ->method('withHeader') + ->with('Content-Type', 'text/html; charset=UTF-8') + ->will($this->returnValue($response3)); + $response3 + ->expects($this->exactly(1)) + ->method('getBody') + ->will($this->returnValue($body)); + + $handler = new ExceptionHandler(false); + $response = $handler->createResponse($exception, $response0); + + $this->assertSame($response3, $response); + } + public function testHandleOutOfMemoryException() { $exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__); diff --git a/src/Symfony/Component/Debug/composer.json b/src/Symfony/Component/Debug/composer.json index 17c56460cb703..2e9bbc4bc23eb 100644 --- a/src/Symfony/Component/Debug/composer.json +++ b/src/Symfony/Component/Debug/composer.json @@ -23,6 +23,7 @@ "symfony/http-kernel": ">=2.3,<2.3.24|~2.4.0|>=2.5,<2.5.9|>=2.6,<2.6.2" }, "require-dev": { + "psr/http-message": "~1.0", "symfony/phpunit-bridge": "~2.7|~3.0.0", "symfony/class-loader": "~2.2|~3.0.0", "symfony/http-kernel": "~2.3.24|~2.5.9|~2.6,>=2.6.2|~3.0.0",