ErrorTest.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. require_once dirname(__FILE__).'/TestCase.php';
  3. /*
  4. * This file is part of Twig.
  5. *
  6. * (c) Fabien Potencier
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. class Twig_Tests_ErrorTest extends Twig_Tests_TestCase
  12. {
  13. public function testTwigExceptionAddsFileAndLineWhenMissing()
  14. {
  15. $loader = new Twig_Loader_Array(array('index' => "\n\n{{ foo.bar }}"));
  16. $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => $this->getTempDir()));
  17. $template = $twig->loadTemplate('index');
  18. try {
  19. $template->render(array());
  20. $this->fail();
  21. } catch (Twig_Error_Runtime $e) {
  22. $this->assertEquals('Variable "foo" does not exist in "index" at line 3', $e->getMessage());
  23. $this->assertEquals(3, $e->getTemplateLine());
  24. $this->assertEquals('index', $e->getTemplateFile());
  25. }
  26. }
  27. public function testRenderWrapsExceptions()
  28. {
  29. $loader = new Twig_Loader_Array(array('index' => "\n\n\n{{ foo.bar }}"));
  30. $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => $this->getTempDir()));
  31. $template = $twig->loadTemplate('index');
  32. try {
  33. $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
  34. $this->fail();
  35. } catch (Twig_Error_Runtime $e) {
  36. $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index" at line 4.', $e->getMessage());
  37. $this->assertEquals(4, $e->getTemplateLine());
  38. $this->assertEquals('index', $e->getTemplateFile());
  39. }
  40. }
  41. }
  42. class Twig_Tests_ErrorTest_Foo
  43. {
  44. public function bar()
  45. {
  46. throw new Exception('Runtime error...');
  47. }
  48. }