ExceptionHandlerTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  14. class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected function setUp()
  17. {
  18. if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
  19. $this->markTestSkipped('The "HttpFoundation" component is not available');
  20. }
  21. }
  22. public function testDebug()
  23. {
  24. $handler = new ExceptionHandler(false);
  25. $response = $handler->createResponse(new \RuntimeException('Foo'));
  26. $this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response->getContent());
  27. $this->assertNotContains('<div class="block_exception clear_fix">', $response->getContent());
  28. $handler = new ExceptionHandler(true);
  29. $response = $handler->createResponse(new \RuntimeException('Foo'));
  30. $this->assertContains('<h1>Whoops, looks like something went wrong.</h1>', $response->getContent());
  31. $this->assertContains('<div class="block_exception clear_fix">', $response->getContent());
  32. }
  33. public function testStatusCode()
  34. {
  35. $handler = new ExceptionHandler(false);
  36. $response = $handler->createResponse(new \RuntimeException('Foo'));
  37. $this->assertEquals('500', $response->getStatusCode());
  38. $this->assertContains('Whoops, looks like something went wrong.', $response->getContent());
  39. $response = $handler->createResponse(new NotFoundHttpException('Foo'));
  40. $this->assertEquals('404', $response->getStatusCode());
  41. $this->assertContains('Sorry, the page you are looking for could not be found.', $response->getContent());
  42. }
  43. public function testHeaders()
  44. {
  45. $handler = new ExceptionHandler(false);
  46. $response = $handler->createResponse(new MethodNotAllowedHttpException(array('POST')));
  47. $this->assertEquals('405', $response->getStatusCode());
  48. $this->assertEquals('POST', $response->headers->get('Allow'));
  49. }
  50. public function testNestedExceptions()
  51. {
  52. $handler = new ExceptionHandler(true);
  53. $response = $handler->createResponse(new \RuntimeException('Foo', null, new \RuntimeException('Bar')));
  54. }
  55. }