ExceptionHandlerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\ExceptionHandler;
  13. use Symfony\Component\Debug\Exception\OutOfMemoryException;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  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->assertContains('Whoops, looks like something went wrong.', $response);
  34. $this->assertNotContains('<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->assertContains('Whoops, looks like something went wrong.', $response);
  40. $this->assertContains('<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->assertContains('Sorry, the page you are looking for could not be found.', $response);
  49. $expectedHeaders = array(
  50. array('HTTP/1.0 404', true, null),
  51. array('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(array('POST')));
  60. $response = ob_get_clean();
  61. $expectedHeaders = array(
  62. array('HTTP/1.0 405', true, null),
  63. array('Allow: POST', false, null),
  64. array('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. $exception = new \Exception('foo');
  79. $handler = $this->getMockBuilder('Symfony\Component\Debug\ExceptionHandler')->setMethods(array('sendPhpResponse'))->getMock();
  80. $handler
  81. ->expects($this->exactly(2))
  82. ->method('sendPhpResponse');
  83. $handler->handle($exception);
  84. $handler->setHandler(function ($e) use ($exception) {
  85. $this->assertSame($exception, $e);
  86. });
  87. $handler->handle($exception);
  88. }
  89. public function testHandleOutOfMemoryException()
  90. {
  91. $exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
  92. $handler = $this->getMockBuilder('Symfony\Component\Debug\ExceptionHandler')->setMethods(array('sendPhpResponse'))->getMock();
  93. $handler
  94. ->expects($this->once())
  95. ->method('sendPhpResponse');
  96. $handler->setHandler(function ($e) {
  97. $this->fail('OutOfMemoryException should bypass the handler');
  98. });
  99. $handler->handle($exception);
  100. }
  101. }