ExceptionHandlerTest.php 5.2 KB

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