[HttpKernel] added some unit tests for ExceptionHandler and
FlattenException
This commit is contained in:
parent
c290d829a7
commit
8d79ebc8ce
@ -71,10 +71,9 @@ class ExceptionHandler
|
|||||||
$content = '';
|
$content = '';
|
||||||
$title = '';
|
$title = '';
|
||||||
try {
|
try {
|
||||||
$code = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
|
|
||||||
$exception = FlattenException::create($exception);
|
$exception = FlattenException::create($exception);
|
||||||
|
|
||||||
switch($code) {
|
switch ($exception->getStatusCode()) {
|
||||||
case 404:
|
case 404:
|
||||||
$title = 'Sorry, the page you are looking for could not be found.';
|
$title = 'Sorry, the page you are looking for could not be found.';
|
||||||
break;
|
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)
|
private function getContent($exception)
|
||||||
|
@ -28,11 +28,16 @@ class FlattenException
|
|||||||
private $statusCode;
|
private $statusCode;
|
||||||
private $headers;
|
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 = new static();
|
||||||
$e->setMessage($exception->getMessage());
|
$e->setMessage($exception->getMessage());
|
||||||
$e->setCode($exception->getCode());
|
$e->setCode($exception->getCode());
|
||||||
|
|
||||||
|
if (null === $statusCode) {
|
||||||
|
$statusCode = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
|
||||||
|
}
|
||||||
|
|
||||||
$e->setStatusCode($statusCode);
|
$e->setStatusCode($statusCode);
|
||||||
$e->setHeaders($headers);
|
$e->setHeaders($headers);
|
||||||
$e->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
|
$e->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
|
||||||
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
@ -10,10 +10,24 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Symfony\Tests\Component\HttpKernel\Exception;
|
namespace Symfony\Tests\Component\HttpKernel\Exception;
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\Exception\FlattenException;
|
use Symfony\Component\HttpKernel\Exception\FlattenException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
class FlattenExceptionTest extends \PHPUnit_Framework_TestCase
|
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
|
* @dataProvider flattenDataProvider
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user