ErrorTest.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Tests_ErrorTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function testErrorWithObjectFilename()
  13. {
  14. $error = new Twig_Error('foo');
  15. $error->setTemplateFile(new SplFileInfo(__FILE__));
  16. $this->assertContains('test'.DIRECTORY_SEPARATOR.'Twig'.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR.'ErrorTest.php', $error->getMessage());
  17. }
  18. public function testErrorWithArrayFilename()
  19. {
  20. $error = new Twig_Error('foo');
  21. $error->setTemplateFile(array('foo' => 'bar'));
  22. $this->assertEquals('foo in {"foo":"bar"}', $error->getMessage());
  23. }
  24. public function testTwigExceptionAddsFileAndLineWhenMissing()
  25. {
  26. $loader = new Twig_Loader_Array(array('index' => "\n\n{{ foo.bar }}\n\n\n{{ 'foo' }}"));
  27. $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
  28. $template = $twig->loadTemplate('index');
  29. try {
  30. $template->render(array());
  31. $this->fail();
  32. } catch (Twig_Error_Runtime $e) {
  33. $this->assertEquals('Variable "foo" does not exist in "index" at line 3', $e->getMessage());
  34. $this->assertEquals(3, $e->getTemplateLine());
  35. $this->assertEquals('index', $e->getTemplateFile());
  36. }
  37. }
  38. public function testRenderWrapsExceptions()
  39. {
  40. $loader = new Twig_Loader_Array(array('index' => "\n\n\n{{ foo.bar }}\n\n\n\n{{ 'foo' }}"));
  41. $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
  42. $template = $twig->loadTemplate('index');
  43. try {
  44. $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
  45. $this->fail();
  46. } catch (Twig_Error_Runtime $e) {
  47. $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index" at line 4.', $e->getMessage());
  48. $this->assertEquals(4, $e->getTemplateLine());
  49. $this->assertEquals('index', $e->getTemplateFile());
  50. }
  51. }
  52. public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritance()
  53. {
  54. $loader = new Twig_Loader_Array(array(
  55. 'index' => "{% extends 'base' %}
  56. {% block content %}
  57. {{ foo.bar }}
  58. {% endblock %}
  59. {% block foo %}
  60. {{ foo.bar }}
  61. {% endblock %}",
  62. 'base' => '{% block content %}{% endblock %}'
  63. ));
  64. $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
  65. $template = $twig->loadTemplate('index');
  66. try {
  67. $template->render(array());
  68. $this->fail();
  69. } catch (Twig_Error_Runtime $e) {
  70. $this->assertEquals('Variable "foo" does not exist in "index" at line 3', $e->getMessage());
  71. $this->assertEquals(3, $e->getTemplateLine());
  72. $this->assertEquals('index', $e->getTemplateFile());
  73. }
  74. try {
  75. $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
  76. $this->fail();
  77. } catch (Twig_Error_Runtime $e) {
  78. $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index" at line 3.', $e->getMessage());
  79. $this->assertEquals(3, $e->getTemplateLine());
  80. $this->assertEquals('index', $e->getTemplateFile());
  81. }
  82. }
  83. public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritanceOnDisk()
  84. {
  85. $loader = new Twig_Loader_Filesystem(dirname(__FILE__).'/Fixtures/errors');
  86. $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
  87. $template = $twig->loadTemplate('index.html');
  88. try {
  89. $template->render(array());
  90. $this->fail();
  91. } catch (Twig_Error_Runtime $e) {
  92. $this->assertEquals('Variable "foo" does not exist in "index.html" at line 3', $e->getMessage());
  93. $this->assertEquals(3, $e->getTemplateLine());
  94. $this->assertEquals('index.html', $e->getTemplateFile());
  95. }
  96. try {
  97. $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
  98. $this->fail();
  99. } catch (Twig_Error_Runtime $e) {
  100. $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index.html" at line 3.', $e->getMessage());
  101. $this->assertEquals(3, $e->getTemplateLine());
  102. $this->assertEquals('index.html', $e->getTemplateFile());
  103. }
  104. }
  105. }
  106. class Twig_Tests_ErrorTest_Foo
  107. {
  108. public function bar()
  109. {
  110. throw new Exception('Runtime error...');
  111. }
  112. }