4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Debug\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Debug\ExceptionHandler;
16 use Symfony\Component\Debug\Exception\OutOfMemoryException;
17 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
20 require_once __DIR__.'/HeaderMock.php';
22 class ExceptionHandlerTest extends TestCase
24 protected function setUp()
29 protected function tearDown()
34 public function testDebug()
36 $handler = new ExceptionHandler(false);
39 $handler->sendPhpResponse(new \RuntimeException('Foo'));
40 $response = ob_get_clean();
42 $this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response);
43 $this->assertNotContains('<h2 class="block_exception clear_fix">', $response);
45 $handler = new ExceptionHandler(true);
48 $handler->sendPhpResponse(new \RuntimeException('Foo'));
49 $response = ob_get_clean();
51 $this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response);
52 $this->assertContains('<h2 class="block_exception clear_fix">', $response);
55 public function testStatusCode()
57 $handler = new ExceptionHandler(false, 'iso8859-1');
60 $handler->sendPhpResponse(new NotFoundHttpException('Foo'));
61 $response = ob_get_clean();
63 $this->assertContains('Sorry, the page you are looking for could not be found.', $response);
65 $expectedHeaders = array(
66 array('HTTP/1.0 404', true, null),
67 array('Content-Type: text/html; charset=iso8859-1', true, null),
70 $this->assertSame($expectedHeaders, testHeader());
73 public function testHeaders()
75 $handler = new ExceptionHandler(false, 'iso8859-1');
78 $handler->sendPhpResponse(new MethodNotAllowedHttpException(array('POST')));
79 $response = ob_get_clean();
81 $expectedHeaders = array(
82 array('HTTP/1.0 405', true, null),
83 array('Allow: POST', false, null),
84 array('Content-Type: text/html; charset=iso8859-1', true, null),
87 $this->assertSame($expectedHeaders, testHeader());
90 public function testNestedExceptions()
92 $handler = new ExceptionHandler(true);
94 $handler->sendPhpResponse(new \RuntimeException('Foo', 0, new \RuntimeException('Bar')));
95 $response = ob_get_clean();
97 $this->assertStringMatchesFormat('%A<span class="exception_message">Foo</span>%A<span class="exception_message">Bar</span>%A', $response);
100 public function testHandle()
102 $exception = new \Exception('foo');
104 $handler = $this->getMockBuilder('Symfony\Component\Debug\ExceptionHandler')->setMethods(array('sendPhpResponse'))->getMock();
106 ->expects($this->exactly(2))
107 ->method('sendPhpResponse');
109 $handler->handle($exception);
112 $handler->setHandler(function ($e) use ($exception, $that) {
113 $that->assertSame($exception, $e);
116 $handler->handle($exception);
119 public function testHandleOutOfMemoryException()
121 $exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
123 $handler = $this->getMockBuilder('Symfony\Component\Debug\ExceptionHandler')->setMethods(array('sendPhpResponse'))->getMock();
125 ->expects($this->once())
126 ->method('sendPhpResponse');
129 $handler->setHandler(function ($e) use ($that) {
130 $that->fail('OutOfMemoryException should bypass the handler');
133 $handler->handle($exception);