]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
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->setTemplateFile(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->setTemplateFile(array('foo' => 'bar')); | |
26 | ||
27 | $this->assertEquals('foo in {"foo":"bar"}', $error->getMessage()); | |
28 | } | |
29 | ||
30 | public function testTwigExceptionAddsFileAndLineWhenMissing() | |
31 | { | |
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)); | |
34 | ||
35 | $template = $twig->loadTemplate('index'); | |
36 | ||
37 | try { | |
38 | $template->render(array()); | |
39 | ||
40 | $this->fail(); | |
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()); | |
45 | } | |
46 | } | |
47 | ||
48 | public function testRenderWrapsExceptions() | |
49 | { | |
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)); | |
52 | ||
53 | $template = $twig->loadTemplate('index'); | |
54 | ||
55 | try { | |
56 | $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo())); | |
57 | ||
58 | $this->fail(); | |
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()); | |
63 | } | |
64 | } | |
65 | ||
66 | public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritance() | |
67 | { | |
68 | $loader = new Twig_Loader_Array(array( | |
69 | 'index' => "{% extends 'base' %} | |
70 | {% block content %} | |
71 | {{ foo.bar }} | |
72 | {% endblock %} | |
73 | {% block foo %} | |
74 | {{ foo.bar }} | |
75 | {% endblock %}", | |
76 | 'base' => '{% block content %}{% endblock %}' | |
77 | )); | |
78 | $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false)); | |
79 | ||
80 | $template = $twig->loadTemplate('index'); | |
81 | try { | |
82 | $template->render(array()); | |
83 | ||
84 | $this->fail(); | |
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()); | |
89 | } | |
90 | ||
91 | try { | |
92 | $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo())); | |
93 | ||
94 | $this->fail(); | |
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()); | |
99 | } | |
100 | } | |
101 | ||
102 | public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritanceAgain() | |
103 | { | |
104 | $loader = new Twig_Loader_Array(array( | |
105 | 'index' => "{% extends 'base' %} | |
106 | {% block content %} | |
107 | {{ parent() }} | |
108 | {% endblock %}", | |
109 | 'base' => '{% block content %}{{ foo }}{% endblock %}' | |
110 | )); | |
111 | $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false)); | |
112 | ||
113 | $template = $twig->loadTemplate('index'); | |
114 | try { | |
115 | $template->render(array()); | |
116 | ||
117 | $this->fail(); | |
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()); | |
122 | } | |
123 | } | |
124 | ||
125 | public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritanceOnDisk() | |
126 | { | |
127 | $loader = new Twig_Loader_Filesystem(dirname(__FILE__).'/Fixtures/errors'); | |
128 | $twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false)); | |
129 | ||
130 | $template = $twig->loadTemplate('index.html'); | |
131 | try { | |
132 | $template->render(array()); | |
133 | ||
134 | $this->fail(); | |
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()); | |
139 | } | |
140 | ||
141 | try { | |
142 | $template->render(array('foo' => new Twig_Tests_ErrorTest_Foo())); | |
143 | ||
144 | $this->fail(); | |
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()); | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
153 | class Twig_Tests_ErrorTest_Foo | |
154 | { | |
155 | public function bar() | |
156 | { | |
157 | throw new Exception('Runtime error...'); | |
158 | } | |
159 | } |