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->setSourceContext(new Twig_Source('', 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->setSourceContext(new Twig_Source('', array('foo' => 'bar')));
27 $this->assertEquals('foo in {"foo":"bar"}', $error->getMessage());
30 public function testTwigExceptionGuessWithMissingVarAndArrayLoader()
32 $loader = new Twig_Loader_Array(array(
33 'base.html' => '{% block content %}{% endblock %}',
34 'index.html' => <<<EOHTML
35 {% extends 'base.html' %}
44 $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
46 $template = $twig->loadTemplate('index.html');
48 $template->render(array());
51 } catch (Twig_Error_Runtime $e) {
52 $this->assertEquals('Variable "foo" does not exist in "index.html" at line 3.', $e->getMessage());
53 $this->assertEquals(3, $e->getTemplateLine());
54 $this->assertEquals('index.html', $e->getSourceContext()->getName());
58 public function testTwigExceptionGuessWithExceptionAndArrayLoader()
60 $loader = new Twig_Loader_Array(array(
61 'base.html' => '{% block content %}{% endblock %}',
62 'index.html' => <<<EOHTML
63 {% extends 'base.html' %}
72 $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
74 $template = $twig->loadTemplate('index.html');
76 $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
79 } catch (Twig_Error_Runtime $e) {
80 $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index.html" at line 3.', $e->getMessage());
81 $this->assertEquals(3, $e->getTemplateLine());
82 $this->assertEquals('index.html', $e->getSourceContext()->getName());
86 public function testTwigExceptionGuessWithMissingVarAndFilesystemLoader()
88 $loader = new Twig_Loader_Filesystem(dirname(__FILE__).'/Fixtures/errors');
89 $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
91 $template = $twig->loadTemplate('index.html');
93 $template->render(array());
96 } catch (Twig_Error_Runtime $e) {
97 $this->assertEquals('Variable "foo" does not exist.', $e->getMessage());
98 $this->assertEquals(3, $e->getTemplateLine());
99 $this->assertEquals('index.html', $e->getSourceContext()->getName());
100 $this->assertEquals(3, $e->getLine());
101 $this->assertEquals(strtr(dirname(__FILE__).'/Fixtures/errors/index.html', '/', DIRECTORY_SEPARATOR), $e->getFile());
105 public function testTwigExceptionGuessWithExceptionAndFilesystemLoader()
107 $loader = new Twig_Loader_Filesystem(dirname(__FILE__).'/Fixtures/errors');
108 $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
110 $template = $twig->loadTemplate('index.html');
112 $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
115 } catch (Twig_Error_Runtime $e) {
116 $this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...").', $e->getMessage());
117 $this->assertEquals(3, $e->getTemplateLine());
118 $this->assertEquals('index.html', $e->getSourceContext()->getName());
119 $this->assertEquals(3, $e->getLine());
120 $this->assertEquals(strtr(dirname(__FILE__).'/Fixtures/errors/index.html', '/', DIRECTORY_SEPARATOR), $e->getFile());
125 * @dataProvider getErroredTemplates
127 public function testTwigExceptionAddsFileAndLine($templates, $name, $line)
129 $loader = new Twig_Loader_Array($templates);
130 $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
132 $template = $twig->loadTemplate('index');
135 $template->render(array());
138 } catch (Twig_Error_Runtime $e) {
139 $this->assertEquals(sprintf('Variable "foo" does not exist in "%s" at line %d.', $name, $line), $e->getMessage());
140 $this->assertEquals($line, $e->getTemplateLine());
141 $this->assertEquals($name, $e->getSourceContext()->getName());
145 $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
148 } catch (Twig_Error_Runtime $e) {
149 $this->assertEquals(sprintf('An exception has been thrown during the rendering of a template ("Runtime error...") in "%s" at line %d.', $name, $line), $e->getMessage());
150 $this->assertEquals($line, $e->getTemplateLine());
151 $this->assertEquals($name, $e->getSourceContext()->getName());
155 public function getErroredTemplates()
158 // error occurs in a template
161 'index' => "\n\n{{ foo.bar }}\n\n\n{{ 'foo' }}",
166 // error occurs in an included template
169 'index' => "{% include 'partial' %}",
170 'partial' => '{{ foo.bar }}',
175 // error occurs in a parent block when called via parent()
178 'index' => "{% extends 'base' %}
182 'base' => '{% block content %}{{ foo.bar }}{% endblock %}',
187 // error occurs in a block from the child
190 'index' => "{% extends 'base' %}
197 'base' => '{% block content %}{% endblock %}',
205 class Twig_Tests_ErrorTest_Foo
207 public function bar()
209 throw new Exception('Runtime error...');