ExceptionControllerTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\Bundle\TwigBundle\Tests\Controller;
  11. use Symfony\Bundle\TwigBundle\Tests\TestCase;
  12. use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
  13. use Symfony\Component\Debug\Exception\FlattenException;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class ExceptionControllerTest extends TestCase
  16. {
  17. public function testShowActionCanBeForcedToShowErrorPage()
  18. {
  19. $twig = new \Twig_Environment(
  20. new \Twig_Loader_Array(array(
  21. '@Twig/Exception/error404.html.twig' => 'ok',
  22. ))
  23. );
  24. $request = Request::create('whatever', 'GET');
  25. $request->headers->set('X-Php-Ob-Level', 1);
  26. $request->attributes->set('showException', false);
  27. $exception = FlattenException::create(new \Exception(), 404);
  28. $controller = new ExceptionController($twig, /* "showException" defaults to --> */ true);
  29. $response = $controller->showAction($request, $exception, null);
  30. $this->assertEquals(200, $response->getStatusCode()); // successful request
  31. $this->assertEquals('ok', $response->getContent()); // content of the error404.html template
  32. }
  33. public function testFallbackToHtmlIfNoTemplateForRequestedFormat()
  34. {
  35. $twig = new \Twig_Environment(
  36. new \Twig_Loader_Array(array(
  37. '@Twig/Exception/error.html.twig' => 'html',
  38. ))
  39. );
  40. $request = Request::create('whatever');
  41. $request->headers->set('X-Php-Ob-Level', 1);
  42. $request->setRequestFormat('txt');
  43. $exception = FlattenException::create(new \Exception());
  44. $controller = new ExceptionController($twig, false);
  45. $response = $controller->showAction($request, $exception);
  46. $this->assertEquals('html', $request->getRequestFormat());
  47. }
  48. }