ExceptionHandlerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 Symfony\Component\Debug\ExceptionHandler;
  12. use Symfony\Component\Debug\Exception\OutOfMemoryException;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  15. require_once __DIR__.'/HeaderMock.php';
  16. class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
  17. {
  18. protected function setUp()
  19. {
  20. testHeader();
  21. }
  22. protected function tearDown()
  23. {
  24. testHeader();
  25. }
  26. public function testDebug()
  27. {
  28. $handler = new ExceptionHandler(false);
  29. ob_start();
  30. $handler->sendPhpResponse(new \RuntimeException('Foo'));
  31. $response = ob_get_clean();
  32. $this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response);
  33. $this->assertNotContains('<h2 class="block_exception clear_fix">', $response);
  34. $handler = new ExceptionHandler(true);
  35. ob_start();
  36. $handler->sendPhpResponse(new \RuntimeException('Foo'));
  37. $response = ob_get_clean();
  38. $this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response);
  39. $this->assertContains('<h2 class="block_exception clear_fix">', $response);
  40. }
  41. public function testStatusCode()
  42. {
  43. $handler = new ExceptionHandler(false, 'iso8859-1');
  44. ob_start();
  45. $handler->sendPhpResponse(new NotFoundHttpException('Foo'));
  46. $response = ob_get_clean();
  47. $this->assertContains('Sorry, the page you are looking for could not be found.', $response);
  48. $expectedHeaders = array(
  49. array('HTTP/1.0 404', true, null),
  50. array('Content-Type: text/html; charset=iso8859-1', true, null),
  51. );
  52. $this->assertSame($expectedHeaders, testHeader());
  53. }
  54. public function testHeaders()
  55. {
  56. $handler = new ExceptionHandler(false, 'iso8859-1');
  57. ob_start();
  58. $handler->sendPhpResponse(new MethodNotAllowedHttpException(array('POST')));
  59. $response = ob_get_clean();
  60. $expectedHeaders = array(
  61. array('HTTP/1.0 405', true, null),
  62. array('Allow: POST', false, null),
  63. array('Content-Type: text/html; charset=iso8859-1', true, null),
  64. );
  65. $this->assertSame($expectedHeaders, testHeader());
  66. }
  67. public function testNestedExceptions()
  68. {
  69. $handler = new ExceptionHandler(true);
  70. ob_start();
  71. $handler->sendPhpResponse(new \RuntimeException('Foo', 0, new \RuntimeException('Bar')));
  72. $response = ob_get_clean();
  73. $this->assertStringMatchesFormat('%A<span class="exception_message">Foo</span>%A<span class="exception_message">Bar</span>%A', $response);
  74. }
  75. public function testHandle()
  76. {
  77. $exception = new \Exception('foo');
  78. $handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('sendPhpResponse'));
  79. $handler
  80. ->expects($this->exactly(2))
  81. ->method('sendPhpResponse');
  82. $handler->handle($exception);
  83. $handler->setHandler(function ($e) use ($exception) {
  84. $this->assertSame($exception, $e);
  85. });
  86. $handler->handle($exception);
  87. }
  88. public function testHandleOutOfMemoryException()
  89. {
  90. $exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
  91. $handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('sendPhpResponse'));
  92. $handler
  93. ->expects($this->once())
  94. ->method('sendPhpResponse');
  95. $handler->setHandler(function ($e) {
  96. $this->fail('OutOfMemoryException should bypass the handler');
  97. });
  98. $handler->handle($exception);
  99. }
  100. }