| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Debug\Tests; |
| 13 |
|
| 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; |
| 19 |
|
| 20 |
require_once __DIR__.'/HeaderMock.php'; |
| 21 |
|
| 22 |
class ExceptionHandlerTest extends TestCase |
| 23 |
{ |
| 24 |
protected function setUp() |
| 25 |
{ |
| 26 |
testHeader(); |
| 27 |
} |
| 28 |
|
| 29 |
protected function tearDown() |
| 30 |
{ |
| 31 |
testHeader(); |
| 32 |
} |
| 33 |
|
| 34 |
public function testDebug() |
| 35 |
{ |
| 36 |
$handler = new ExceptionHandler(false); |
| 37 |
|
| 38 |
ob_start(); |
| 39 |
$handler->sendPhpResponse(new \RuntimeException('Foo')); |
| 40 |
$response = ob_get_clean(); |
| 41 |
|
| 42 |
$this->assertStringContainsString('Whoops, looks like something went wrong.', $response); |
| 43 |
$this->assertStringNotContainsString('<div class="trace trace-as-html">', $response); |
| 44 |
|
| 45 |
$handler = new ExceptionHandler(true); |
| 46 |
|
| 47 |
ob_start(); |
| 48 |
$handler->sendPhpResponse(new \RuntimeException('Foo')); |
| 49 |
$response = ob_get_clean(); |
| 50 |
|
| 51 |
$this->assertStringContainsString('Whoops, looks like something went wrong.', $response); |
| 52 |
$this->assertStringContainsString('<div class="trace trace-as-html">', $response); |
| 53 |
} |
| 54 |
|
| 55 |
public function testStatusCode() |
| 56 |
{ |
| 57 |
$handler = new ExceptionHandler(false, 'iso8859-1'); |
| 58 |
|
| 59 |
ob_start(); |
| 60 |
$handler->sendPhpResponse(new NotFoundHttpException('Foo')); |
| 61 |
$response = ob_get_clean(); |
| 62 |
|
| 63 |
$this->assertStringContainsString('Sorry, the page you are looking for could not be found.', $response); |
| 64 |
|
| 65 |
$expectedHeaders = [ |
| 66 |
['HTTP/1.0 404', true, null], |
| 67 |
['Content-Type: text/html; charset=iso8859-1', true, null], |
| 68 |
]; |
| 69 |
|
| 70 |
$this->assertSame($expectedHeaders, testHeader()); |
| 71 |
} |
| 72 |
|
| 73 |
public function testHeaders() |
| 74 |
{ |
| 75 |
$handler = new ExceptionHandler(false, 'iso8859-1'); |
| 76 |
|
| 77 |
ob_start(); |
| 78 |
$handler->sendPhpResponse(new MethodNotAllowedHttpException(['POST'])); |
| 79 |
ob_get_clean(); |
| 80 |
|
| 81 |
$expectedHeaders = [ |
| 82 |
['HTTP/1.0 405', true, null], |
| 83 |
['Allow: POST', false, null], |
| 84 |
['Content-Type: text/html; charset=iso8859-1', true, null], |
| 85 |
]; |
| 86 |
|
| 87 |
$this->assertSame($expectedHeaders, testHeader()); |
| 88 |
} |
| 89 |
|
| 90 |
public function testNestedExceptions() |
| 91 |
{ |
| 92 |
$handler = new ExceptionHandler(true); |
| 93 |
ob_start(); |
| 94 |
$handler->sendPhpResponse(new \RuntimeException('Foo', 0, new \RuntimeException('Bar'))); |
| 95 |
$response = ob_get_clean(); |
| 96 |
|
| 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); |
| 98 |
} |
| 99 |
|
| 100 |
public function testHandle() |
| 101 |
{ |
| 102 |
$handler = new ExceptionHandler(true); |
| 103 |
ob_start(); |
| 104 |
|
| 105 |
$handler->handle(new \Exception('foo')); |
| 106 |
|
| 107 |
$this->assertThatTheExceptionWasOutput(ob_get_clean(), \Exception::class, 'Exception', 'foo'); |
| 108 |
} |
| 109 |
|
| 110 |
public function testHandleWithACustomHandlerThatOutputsSomething() |
| 111 |
{ |
| 112 |
$handler = new ExceptionHandler(true); |
| 113 |
ob_start(); |
| 114 |
$handler->setHandler(function () { |
| 115 |
echo 'ccc'; |
| 116 |
}); |
| 117 |
|
| 118 |
$handler->handle(new \Exception()); |
| 119 |
ob_end_flush(); // Necessary because of this PHP bug : https://bugs.php.net/76563 |
| 120 |
$this->assertSame('ccc', ob_get_clean()); |
| 121 |
} |
| 122 |
|
| 123 |
public function testHandleWithACustomHandlerThatOutputsNothing() |
| 124 |
{ |
| 125 |
$handler = new ExceptionHandler(true); |
| 126 |
$handler->setHandler(function () {}); |
| 127 |
|
| 128 |
$handler->handle(new \Exception('ccc')); |
| 129 |
|
| 130 |
$this->assertThatTheExceptionWasOutput(ob_get_clean(), \Exception::class, 'Exception', 'ccc'); |
| 131 |
} |
| 132 |
|
| 133 |
public function testHandleWithACustomHandlerThatFails() |
| 134 |
{ |
| 135 |
$handler = new ExceptionHandler(true); |
| 136 |
$handler->setHandler(function () { |
| 137 |
throw new \RuntimeException(); |
| 138 |
}); |
| 139 |
|
| 140 |
$handler->handle(new \Exception('ccc')); |
| 141 |
|
| 142 |
$this->assertThatTheExceptionWasOutput(ob_get_clean(), \Exception::class, 'Exception', 'ccc'); |
| 143 |
} |
| 144 |
|
| 145 |
public function testHandleOutOfMemoryException() |
| 146 |
{ |
| 147 |
$handler = new ExceptionHandler(true); |
| 148 |
ob_start(); |
| 149 |
$handler->setHandler(function () { |
| 150 |
$this->fail('OutOfMemoryException should bypass the handler'); |
| 151 |
}); |
| 152 |
|
| 153 |
$handler->handle(new OutOfMemoryException('foo', 0, \E_ERROR, __FILE__, __LINE__)); |
| 154 |
|
| 155 |
$this->assertThatTheExceptionWasOutput(ob_get_clean(), OutOfMemoryException::class, 'OutOfMemoryException', 'foo'); |
| 156 |
} |
| 157 |
|
| 158 |
private function assertThatTheExceptionWasOutput($content, $expectedClass, $expectedTitle, $expectedMessage) |
| 159 |
{ |
| 160 |
$this->assertStringContainsString(sprintf('<span class="exception_title"><abbr title="%s">%s</abbr></span>', $expectedClass, $expectedTitle), $content); |
| 161 |
$this->assertStringContainsString(sprintf('<p class="break-long-words trace-message">%s</p>', $expectedMessage), $content); |
| 162 |
} |
| 163 |
} |
| 164 |
|