No Description

ExceptionHandlerTest.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  16. class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testDebug()
  19. {
  20. $handler = new ExceptionHandler(false);
  21. $response = $handler->createResponse(new \RuntimeException('Foo'));
  22. $this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response->getContent());
  23. $this->assertNotContains('<h2 class="block_exception clear_fix">', $response->getContent());
  24. $handler = new ExceptionHandler(true);
  25. $response = $handler->createResponse(new \RuntimeException('Foo'));
  26. $this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response->getContent());
  27. $this->assertContains('<h2 class="block_exception clear_fix">', $response->getContent());
  28. }
  29. public function testStatusCode()
  30. {
  31. $handler = new ExceptionHandler(false);
  32. $response = $handler->createResponse(new \RuntimeException('Foo'));
  33. $this->assertEquals('500', $response->getStatusCode());
  34. $this->assertContains('Whoops, looks like something went wrong.', $response->getContent());
  35. $response = $handler->createResponse(new NotFoundHttpException('Foo'));
  36. $this->assertEquals('404', $response->getStatusCode());
  37. $this->assertContains('Sorry, the page you are looking for could not be found.', $response->getContent());
  38. }
  39. public function testHeaders()
  40. {
  41. $handler = new ExceptionHandler(false);
  42. $response = $handler->createResponse(new MethodNotAllowedHttpException(array('POST')));
  43. $this->assertEquals('405', $response->getStatusCode());
  44. $this->assertEquals('POST', $response->headers->get('Allow'));
  45. }
  46. public function testNestedExceptions()
  47. {
  48. $handler = new ExceptionHandler(true);
  49. $response = $handler->createResponse(new \RuntimeException('Foo', 0, new \RuntimeException('Bar')));
  50. }
  51. public function testHandle()
  52. {
  53. $exception = new \Exception('foo');
  54. if (class_exists('Symfony\Component\HttpFoundation\Response')) {
  55. $handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('createResponse'));
  56. $handler
  57. ->expects($this->exactly(2))
  58. ->method('createResponse')
  59. ->will($this->returnValue(new Response()));
  60. } else {
  61. $handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('sendPhpResponse'));
  62. $handler
  63. ->expects($this->exactly(2))
  64. ->method('sendPhpResponse');
  65. }
  66. $handler->handle($exception);
  67. $that = $this;
  68. $handler->setHandler(function ($e) use ($exception, $that) {
  69. $that->assertSame($exception, $e);
  70. });
  71. $handler->handle($exception);
  72. }
  73. public function testHandleOutOfMemoryException()
  74. {
  75. $exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
  76. if (class_exists('Symfony\Component\HttpFoundation\Response')) {
  77. $handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('createResponse'));
  78. $handler
  79. ->expects($this->once())
  80. ->method('createResponse')
  81. ->will($this->returnValue(new Response()));
  82. } else {
  83. $handler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('sendPhpResponse'));
  84. $handler
  85. ->expects($this->once())
  86. ->method('sendPhpResponse');
  87. }
  88. $that = $this;
  89. $handler->setHandler(function ($e) use ($that) {
  90. $that->fail('OutOfMemoryException should bypass the handler');
  91. });
  92. $handler->handle($exception);
  93. }
  94. }