4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 class Twig_Tests_ErrorTest
extends PHPUnit_Framework_TestCase
14 public function testErrorWithObjectFilename()
16 $error = new Twig_Error('foo');
17 $error->setTemplateFile(new SplFileInfo(__FILE__
));
19 $this->assertContains('test'.DIRECTORY_SEPARATOR
.'Twig'.DIRECTORY_SEPARATOR
.'Tests'.DIRECTORY_SEPARATOR
.'ErrorTest.php', $error->getMessage());
22 public function testErrorWithArrayFilename()
24 $error = new Twig_Error('foo');
25 $error->setTemplateFile(array('foo' => 'bar'));
27 $this->assertEquals('foo in {"foo":"bar"}', $error->getMessage());
30 public function testTwigExceptionAddsFileAndLineWhenMissing()
32 $loader = new Twig_Loader_Array(array('index' => "\n\n{{ foo.bar }}\n\n\n{{ 'foo' }}"));
33 $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
35 $template = $twig->loadTemplate('index');
38 $template->render(array());
41 } catch (Twig_Error_Runtime
$e) {
42 $this->assertEquals('Variable "foo" does not exist in "index" at line 3', $e->getMessage());
43 $this->assertEquals(3, $e->getTemplateLine());
44 $this->assertEquals('index', $e->getTemplateFile());
48 public function testRenderWrapsExceptions()
50 $loader = new Twig_Loader_Array(array('index' => "\n\n\n{{ foo.bar }}\n\n\n\n{{ 'foo' }}"));
51 $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
53 $template = $twig->loadTemplate('index');
56 $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
59 } catch (Twig_Error_Runtime
$e) {
60 $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index" at line 4.', $e->getMessage());
61 $this->assertEquals(4, $e->getTemplateLine());
62 $this->assertEquals('index', $e->getTemplateFile());
66 public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritance()
68 $loader = new Twig_Loader_Array(array(
69 'index' => "{% extends 'base' %}
76 'base' => '{% block content %}{% endblock %}'
78 $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
80 $template = $twig->loadTemplate('index');
82 $template->render(array());
85 } catch (Twig_Error_Runtime
$e) {
86 $this->assertEquals('Variable "foo" does not exist in "index" at line 3', $e->getMessage());
87 $this->assertEquals(3, $e->getTemplateLine());
88 $this->assertEquals('index', $e->getTemplateFile());
92 $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
95 } catch (Twig_Error_Runtime
$e) {
96 $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index" at line 3.', $e->getMessage());
97 $this->assertEquals(3, $e->getTemplateLine());
98 $this->assertEquals('index', $e->getTemplateFile());
102 public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritanceAgain()
104 $loader = new Twig_Loader_Array(array(
105 'index' => "{% extends 'base' %}
109 'base' => '{% block content %}{{ foo }}{% endblock %}'
111 $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
113 $template = $twig->loadTemplate('index');
115 $template->render(array());
118 } catch (Twig_Error_Runtime
$e) {
119 $this->assertEquals('Variable "foo" does not exist in "base" at line 1', $e->getMessage());
120 $this->assertEquals(1, $e->getTemplateLine());
121 $this->assertEquals('base', $e->getTemplateFile());
125 public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritanceOnDisk()
127 $loader = new Twig_Loader_Filesystem(dirname(__FILE__
).'/Fixtures/errors');
128 $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
130 $template = $twig->loadTemplate('index.html');
132 $template->render(array());
135 } catch (Twig_Error_Runtime
$e) {
136 $this->assertEquals('Variable "foo" does not exist in "index.html" at line 3', $e->getMessage());
137 $this->assertEquals(3, $e->getTemplateLine());
138 $this->assertEquals('index.html', $e->getTemplateFile());
142 $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
145 } catch (Twig_Error_Runtime
$e) {
146 $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index.html" at line 3.', $e->getMessage());
147 $this->assertEquals(3, $e->getTemplateLine());
148 $this->assertEquals('index.html', $e->getTemplateFile());
153 class Twig_Tests_ErrorTest_Foo
155 public function bar()
157 throw new Exception('Runtime error...');