[Debug] cleanup ExceptionHandlerTest

This commit is contained in:
Nicolas Grekas 2015-07-31 09:20:17 +02:00
parent 8d025cbc91
commit 51bacc6d41
4 changed files with 110 additions and 56 deletions

View File

@ -38,7 +38,7 @@ class ExceptionHandler
public function __construct($debug = true, $charset = null, $fileLinkFormat = null) public function __construct($debug = true, $charset = null, $fileLinkFormat = null)
{ {
if (false !== strpos($charset, '%') xor false === strpos($fileLinkFormat, '%')) { if (false !== strpos($charset, '%')) {
// Swap $charset and $fileLinkFormat for BC reasons // Swap $charset and $fileLinkFormat for BC reasons
$pivot = $fileLinkFormat; $pivot = $fileLinkFormat;
$fileLinkFormat = $charset; $fileLinkFormat = $charset;
@ -153,19 +153,22 @@ class ExceptionHandler
* it will fallback to plain PHP functions. * it will fallback to plain PHP functions.
* *
* @param \Exception $exception An \Exception instance * @param \Exception $exception An \Exception instance
*
* @see sendPhpResponse()
* @see createResponse()
*/ */
private function failSafeHandle(\Exception $exception) private function failSafeHandle(\Exception $exception)
{ {
if (class_exists('Symfony\Component\HttpFoundation\Response', false)) { if (class_exists('Symfony\Component\HttpFoundation\Response', false)
&& __CLASS__ !== get_class($this)
&& ($reflector = new \ReflectionMethod($this, 'createResponse'))
&& __CLASS__ !== $reflector->class
) {
$response = $this->createResponse($exception); $response = $this->createResponse($exception);
$response->sendHeaders(); $response->sendHeaders();
$response->sendContent(); $response->sendContent();
} else {
$this->sendPhpResponse($exception); return;
} }
$this->sendPhpResponse($exception);
} }
/** /**

View File

@ -13,71 +13,97 @@ namespace Symfony\Component\Debug\Tests;
use Symfony\Component\Debug\ExceptionHandler; use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\Debug\Exception\OutOfMemoryException; use Symfony\Component\Debug\Exception\OutOfMemoryException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
require_once __DIR__.'/HeaderMock.php';
class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
{ {
protected function setUp()
{
testHeader();
}
protected function tearDown()
{
testHeader();
}
public function testDebug() public function testDebug()
{ {
$handler = new ExceptionHandler(false); $handler = new ExceptionHandler(false);
$response = $handler->createResponse(new \RuntimeException('Foo'));
$this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response->getContent()); ob_start();
$this->assertNotContains('<h2 class="block_exception clear_fix">', $response->getContent()); $handler->sendPhpResponse(new \RuntimeException('Foo'));
$response = ob_get_clean();
$this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response);
$this->assertNotContains('<h2 class="block_exception clear_fix">', $response);
$handler = new ExceptionHandler(true); $handler = new ExceptionHandler(true);
$response = $handler->createResponse(new \RuntimeException('Foo'));
$this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response->getContent()); ob_start();
$this->assertContains('<h2 class="block_exception clear_fix">', $response->getContent()); $handler->sendPhpResponse(new \RuntimeException('Foo'));
$response = ob_get_clean();
$this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response);
$this->assertContains('<h2 class="block_exception clear_fix">', $response);
} }
public function testStatusCode() public function testStatusCode()
{ {
$handler = new ExceptionHandler(false); $handler = new ExceptionHandler(false, 'iso8859-1');
$response = $handler->createResponse(new \RuntimeException('Foo')); ob_start();
$this->assertEquals('500', $response->getStatusCode()); $handler->sendPhpResponse(new NotFoundHttpException('Foo'));
$this->assertContains('Whoops, looks like something went wrong.', $response->getContent()); $response = ob_get_clean();
$response = $handler->createResponse(new NotFoundHttpException('Foo')); $this->assertContains('Sorry, the page you are looking for could not be found.', $response);
$this->assertEquals('404', $response->getStatusCode());
$this->assertContains('Sorry, the page you are looking for could not be found.', $response->getContent()); $expectedHeaders = array(
array('HTTP/1.0 404', true, null),
array('Content-Type: text/html; charset=iso8859-1', true, null),
);
$this->assertSame($expectedHeaders, testHeader());
} }
public function testHeaders() public function testHeaders()
{ {
$handler = new ExceptionHandler(false); $handler = new ExceptionHandler(false, 'iso8859-1');
$response = $handler->createResponse(new MethodNotAllowedHttpException(array('POST'))); ob_start();
$this->assertEquals('405', $response->getStatusCode()); $handler->sendPhpResponse(new MethodNotAllowedHttpException(array('POST')));
$this->assertEquals('POST', $response->headers->get('Allow')); $response = ob_get_clean();
$expectedHeaders = array(
array('HTTP/1.0 405', true, null),
array('Allow: POST', false, null),
array('Content-Type: text/html; charset=iso8859-1', true, null),
);
$this->assertSame($expectedHeaders, testHeader());
} }
public function testNestedExceptions() public function testNestedExceptions()
{ {
$handler = new ExceptionHandler(true); $handler = new ExceptionHandler(true);
$response = $handler->createResponse(new \RuntimeException('Foo', 0, new \RuntimeException('Bar'))); ob_start();
$handler->sendPhpResponse(new \RuntimeException('Foo', 0, new \RuntimeException('Bar')));
$response = ob_get_clean();
$this->assertStringMatchesFormat('%A<span class="exception_message">Foo</span>%A<span class="exception_message">Bar</span>%A', $response);
} }
public function testHandle() public function testHandle()
{ {
$exception = new \Exception('foo'); $exception = new \Exception('foo');
if (class_exists('Symfony\Component\HttpFoundation\Response')) { $handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('sendPhpResponse'));
$handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('createResponse')); $handler
$handler ->expects($this->exactly(2))
->expects($this->exactly(2)) ->method('sendPhpResponse');
->method('createResponse')
->will($this->returnValue(new Response()));
} else {
$handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('sendPhpResponse'));
$handler
->expects($this->exactly(2))
->method('sendPhpResponse');
}
$handler->handle($exception); $handler->handle($exception);
@ -93,18 +119,10 @@ class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
{ {
$exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__); $exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
if (class_exists('Symfony\Component\HttpFoundation\Response')) { $handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('sendPhpResponse'));
$handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('createResponse')); $handler
$handler ->expects($this->once())
->expects($this->once()) ->method('sendPhpResponse');
->method('createResponse')
->will($this->returnValue(new Response()));
} else {
$handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('sendPhpResponse'));
$handler
->expects($this->once())
->method('sendPhpResponse');
}
$that = $this; $that = $this;
$handler->setHandler(function ($e) use ($that) { $handler->setHandler(function ($e) use ($that) {

View File

@ -0,0 +1,38 @@
<?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\Component\Debug;
function headers_sent()
{
return false;
}
function header($str, $replace = true, $status = null)
{
Tests\testHeader($str, $replace, $status);
}
namespace Symfony\Component\Debug\Tests;
function testHeader()
{
static $headers = array();
if (!$h = func_get_args()) {
$h = $headers;
$headers = array();
return $h;
}
$headers[] = func_get_args();
}

View File

@ -25,12 +25,7 @@
"require-dev": { "require-dev": {
"symfony/phpunit-bridge": "~2.7", "symfony/phpunit-bridge": "~2.7",
"symfony/class-loader": "~2.2", "symfony/class-loader": "~2.2",
"symfony/http-kernel": "~2.3.24|~2.5.9|~2.6,>=2.6.2", "symfony/http-kernel": "~2.3.24|~2.5.9|~2.6,>=2.6.2"
"symfony/http-foundation": "~2.1"
},
"suggest": {
"symfony/http-foundation": "",
"symfony/http-kernel": ""
}, },
"autoload": { "autoload": {
"psr-4": { "Symfony\\Component\\Debug\\": "" } "psr-4": { "Symfony\\Component\\Debug\\": "" }