[HttpKernel] added some unit tests for ExceptionHandler and

FlattenException
This commit is contained in:
Fabien Potencier 2012-01-22 11:17:52 +01:00
parent c290d829a7
commit 8d79ebc8ce
4 changed files with 68 additions and 4 deletions

View File

@ -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)

View File

@ -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());

View File

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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('<h1>Whoops, looks like something went wrong.</h1>', $response->getContent());
$this->assertNotContains('<div class="block_exception clear_fix">', $response->getContent());
$handler = new ExceptionHandler(true);
$response = $handler->createResponse(new \RuntimeException('Foo'));
$this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response->getContent());
$this->assertContains('<div class="block_exception clear_fix">', $response->getContent());
}
public function testStatusCode()
{
$handler = new ExceptionHandler(false);
$response = $handler->createResponse(new \RuntimeException('Foo'));
$this->assertEquals('500', $response->getStatusCode());
$this->assertContains('<title>Whoops, looks like something went wrong.</title>', $response->getContent());
$response = $handler->createResponse(new NotFoundHttpException('Foo'));
$this->assertEquals('404', $response->getStatusCode());
$this->assertContains('<title>Sorry, the page you are looking for could not be found.</title>', $response->getContent());
}
}

View File

@ -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
*/