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\Exception\OutOfMemoryException;
16 use Symfony\Component\Debug\ExceptionHandler;
17 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
18 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
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('Whoops, looks like something went wrong.', $response);
43 $this->assertNotContains('<div class="trace trace-as-html">', $response);
45 $handler = new ExceptionHandler(true);
48 $handler->sendPhpResponse(new \RuntimeException('Foo'));
49 $response = ob_get_clean();
51 $this->assertContains('Whoops, looks like something went wrong.', $response);
52 $this->assertContains('<div class="trace trace-as-html">', $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<p class="break-long-words trace-message">Foo</p>%A<p class="break-long-words trace-message">Bar</p>%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);
111 $handler->setHandler(function ($e) use ($exception) {
112 $this->assertSame($exception, $e);
115 $handler->handle($exception);
118 public function testHandleOutOfMemoryException()
120 $exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
122 $handler = $this->getMockBuilder('Symfony\Component\Debug\ExceptionHandler')->setMethods(array('sendPhpResponse'))->getMock();
124 ->expects($this->once())
125 ->method('sendPhpResponse');
127 $handler->setHandler(function ($e) {
128 $this->fail('OutOfMemoryException should bypass the handler');
131 $handler->handle($exception);