From 8d79ebc8ce559fb8395e2c7f2d2b1fcd01ee3148 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 22 Jan 2012 11:17:52 +0100 Subject: [PATCH] [HttpKernel] added some unit tests for ExceptionHandler and FlattenException --- .../HttpKernel/Debug/ExceptionHandler.php | 5 +- .../HttpKernel/Exception/FlattenException.php | 7 ++- .../HttpKernel/Debug/ExceptionHandlerTest.php | 46 +++++++++++++++++++ .../Exception/FlattenExceptionTest.php | 14 ++++++ 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 tests/Symfony/Tests/Component/HttpKernel/Debug/ExceptionHandlerTest.php diff --git a/src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php b/src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php index 5081adb63c..70992975a7 100644 --- a/src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php +++ b/src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php @@ -71,10 +71,9 @@ class ExceptionHandler $content = ''; $title = ''; try { - $code = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500; $exception = FlattenException::create($exception); - switch($code) { + switch ($exception->getStatusCode()) { case 404: $title = 'Sorry, the page you are looking for could not be found.'; break; @@ -94,7 +93,7 @@ class ExceptionHandler } } - return new Response($this->decorate($content, $title), $code); + return new Response($this->decorate($content, $title), $exception->getStatusCode()); } private function getContent($exception) diff --git a/src/Symfony/Component/HttpKernel/Exception/FlattenException.php b/src/Symfony/Component/HttpKernel/Exception/FlattenException.php index 0c1844cdd0..c48606e784 100644 --- a/src/Symfony/Component/HttpKernel/Exception/FlattenException.php +++ b/src/Symfony/Component/HttpKernel/Exception/FlattenException.php @@ -28,11 +28,16 @@ class FlattenException private $statusCode; private $headers; - static public function create(\Exception $exception, $statusCode = 500, array $headers = array()) + static public function create(\Exception $exception, $statusCode = null, array $headers = array()) { $e = new static(); $e->setMessage($exception->getMessage()); $e->setCode($exception->getCode()); + + if (null === $statusCode) { + $statusCode = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500; + } + $e->setStatusCode($statusCode); $e->setHeaders($headers); $e->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine()); diff --git a/tests/Symfony/Tests/Component/HttpKernel/Debug/ExceptionHandlerTest.php b/tests/Symfony/Tests/Component/HttpKernel/Debug/ExceptionHandlerTest.php new file mode 100644 index 0000000000..26947c5669 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpKernel/Debug/ExceptionHandlerTest.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\HttpKernel\Debug; + +use Symfony\Component\HttpKernel\Debug\ExceptionHandler; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; + +class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase +{ + public function testDebug() + { + $handler = new ExceptionHandler(false); + $response = $handler->createResponse(new \RuntimeException('Foo')); + + $this->assertContains('

Whoops, looks like something went wrong.

', $response->getContent()); + $this->assertNotContains('
', $response->getContent()); + + $handler = new ExceptionHandler(true); + $response = $handler->createResponse(new \RuntimeException('Foo')); + + $this->assertContains('

Whoops, looks like something went wrong.

', $response->getContent()); + $this->assertContains('
', $response->getContent()); + } + + public function testStatusCode() + { + $handler = new ExceptionHandler(false); + + $response = $handler->createResponse(new \RuntimeException('Foo')); + $this->assertEquals('500', $response->getStatusCode()); + $this->assertContains('Whoops, looks like something went wrong.', $response->getContent()); + + $response = $handler->createResponse(new NotFoundHttpException('Foo')); + $this->assertEquals('404', $response->getStatusCode()); + $this->assertContains('Sorry, the page you are looking for could not be found.', $response->getContent()); + } +} diff --git a/tests/Symfony/Tests/Component/HttpKernel/Exception/FlattenExceptionTest.php b/tests/Symfony/Tests/Component/HttpKernel/Exception/FlattenExceptionTest.php index ed79239b25..93e069484d 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/Exception/FlattenExceptionTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/Exception/FlattenExceptionTest.php @@ -10,10 +10,24 @@ */ namespace Symfony\Tests\Component\HttpKernel\Exception; + use Symfony\Component\HttpKernel\Exception\FlattenException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class FlattenExceptionTest extends \PHPUnit_Framework_TestCase { + public function testStatusCode() + { + $flattened = FlattenException::create(new \RuntimeException(), 403); + $this->assertEquals('403', $flattened->getStatusCode()); + + $flattened = FlattenException::create(new \RuntimeException()); + $this->assertEquals('500', $flattened->getStatusCode()); + + $flattened = FlattenException::create(new NotFoundHttpException()); + $this->assertEquals('404', $flattened->getStatusCode()); + } + /** * @dataProvider flattenDataProvider */