ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ErrorTest.php
Go to the documentation of this file.
1 <?php
2 
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 
12 class Twig_Tests_ErrorTest extends \PHPUnit\Framework\TestCase
13 {
14  public function testErrorWithObjectFilename()
15  {
16  $error = new Twig_Error('foo');
17  $error->setSourceContext(new Twig_Source('', new SplFileInfo(__FILE__)));
18 
19  $this->assertContains('test'.DIRECTORY_SEPARATOR.'Twig'.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR.'ErrorTest.php', $error->getMessage());
20  }
21 
22  public function testErrorWithArrayFilename()
23  {
24  $error = new Twig_Error('foo');
25  $error->setSourceContext(new Twig_Source('', array('foo' => 'bar')));
26 
27  $this->assertEquals('foo in {"foo":"bar"}', $error->getMessage());
28  }
29 
31  {
32  $loader = new Twig_Loader_Array(array(
33  'base.html' => '{% block content %}{% endblock %}',
34  'index.html' => <<<EOHTML
35 {% extends 'base.html' %}
36 {% block content %}
37  {{ foo.bar }}
38 {% endblock %}
39 {% block foo %}
40  {{ foo.bar }}
41 {% endblock %}
42 EOHTML
43  ));
44  $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
45 
46  $template = $twig->loadTemplate('index.html');
47  try {
48  $template->render(array());
49 
50  $this->fail();
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());
55  }
56  }
57 
59  {
60  $loader = new Twig_Loader_Array(array(
61  'base.html' => '{% block content %}{% endblock %}',
62  'index.html' => <<<EOHTML
63 {% extends 'base.html' %}
64 {% block content %}
65  {{ foo.bar }}
66 {% endblock %}
67 {% block foo %}
68  {{ foo.bar }}
69 {% endblock %}
70 EOHTML
71  ));
72  $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
73 
74  $template = $twig->loadTemplate('index.html');
75  try {
76  $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
77 
78  $this->fail();
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());
83  }
84  }
85 
87  {
88  $loader = new Twig_Loader_Filesystem(dirname(__FILE__).'/Fixtures/errors');
89  $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
90 
91  $template = $twig->loadTemplate('index.html');
92  try {
93  $template->render(array());
94 
95  $this->fail();
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());
102  }
103  }
104 
106  {
107  $loader = new Twig_Loader_Filesystem(dirname(__FILE__).'/Fixtures/errors');
108  $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
109 
110  $template = $twig->loadTemplate('index.html');
111  try {
112  $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
113 
114  $this->fail();
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());
121  }
122  }
123 
127  public function testTwigExceptionAddsFileAndLine($templates, $name, $line)
128  {
129  $loader = new Twig_Loader_Array($templates);
130  $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
131 
132  $template = $twig->loadTemplate('index');
133 
134  try {
135  $template->render(array());
136 
137  $this->fail();
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());
142  }
143 
144  try {
145  $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
146 
147  $this->fail();
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());
152  }
153  }
154 
155  public function getErroredTemplates()
156  {
157  return array(
158  // error occurs in a template
159  array(
160  array(
161  'index' => "\n\n{{ foo.bar }}\n\n\n{{ 'foo' }}",
162  ),
163  'index', 3,
164  ),
165 
166  // error occurs in an included template
167  array(
168  array(
169  'index' => "{% include 'partial' %}",
170  'partial' => '{{ foo.bar }}',
171  ),
172  'partial', 1,
173  ),
174 
175  // error occurs in a parent block when called via parent()
176  array(
177  array(
178  'index' => "{% extends 'base' %}
179  {% block content %}
180  {{ parent() }}
181  {% endblock %}",
182  'base' => '{% block content %}{{ foo.bar }}{% endblock %}',
183  ),
184  'base', 1,
185  ),
186 
187  // error occurs in a block from the child
188  array(
189  array(
190  'index' => "{% extends 'base' %}
191  {% block content %}
192  {{ foo.bar }}
193  {% endblock %}
194  {% block foo %}
195  {{ foo.bar }}
196  {% endblock %}",
197  'base' => '{% block content %}{% endblock %}',
198  ),
199  'index', 3,
200  ),
201  );
202  }
203 }
204 
206 {
207  public function bar()
208  {
209  throw new Exception('Runtime error...');
210  }
211 }
testTwigExceptionAddsFileAndLine($templates, $name, $line)
getErroredTemplates
Definition: ErrorTest.php:127
$template
getSourceContext()
Gets the source context of the Twig template where the error occurred.
Definition: Error.php:191
getTemplateLine()
Gets the template line where the error occurred.
Definition: Error.php:169
Twig base exception.
Definition: Error.php:34
testTwigExceptionGuessWithMissingVarAndArrayLoader()
Definition: ErrorTest.php:30
Exception thrown when an error occurs at runtime.
Definition: Runtime.php:18
Loads template from the filesystem.
Definition: Filesystem.php:17
testTwigExceptionGuessWithMissingVarAndFilesystemLoader()
Definition: ErrorTest.php:86
testTwigExceptionGuessWithExceptionAndArrayLoader()
Definition: ErrorTest.php:58
testTwigExceptionGuessWithExceptionAndFilesystemLoader()
Definition: ErrorTest.php:105
Holds information about a non-compiled Twig template.
Definition: Source.php:19
Stores the Twig configuration.
Definition: Environment.php:17
Loads a template from an array.
Definition: Array.php:26