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
12use Twig\Environment;
13use Twig\Node\Expression\AssignNameExpression;
14use Twig\Node\Expression\ConditionalExpression;
15use Twig\Node\Expression\ConstantExpression;
16use Twig\Node\ImportNode;
17use Twig\Node\ModuleNode;
18use Twig\Node\Node;
19use Twig\Node\SetNode;
20use Twig\Node\TextNode;
21use Twig\Source;
22use Twig\Test\NodeTestCase;
23
24class Twig_Tests_Node_ModuleTest extends NodeTestCase
25{
26    public function testConstructor()
27    {
28        $body = new TextNode('foo', 1);
29        $parent = new ConstantExpression('layout.twig', 1);
30        $blocks = new Node();
31        $macros = new Node();
32        $traits = new Node();
33        $source = new Source('{{ foo }}', 'foo.twig');
34        $node = new ModuleNode($body, $parent, $blocks, $macros, $traits, new Node([]), $source);
35
36        $this->assertEquals($body, $node->getNode('body'));
37        $this->assertEquals($blocks, $node->getNode('blocks'));
38        $this->assertEquals($macros, $node->getNode('macros'));
39        $this->assertEquals($parent, $node->getNode('parent'));
40        $this->assertEquals($source->getName(), $node->getTemplateName());
41    }
42
43    public function getTests()
44    {
45        $twig = new Environment($this->getMockBuilder('\Twig\Loader\LoaderInterface')->getMock());
46
47        $tests = [];
48
49        $body = new TextNode('foo', 1);
50        $extends = null;
51        $blocks = new Node();
52        $macros = new Node();
53        $traits = new Node();
54        $source = new Source('{{ foo }}', 'foo.twig');
55
56        $node = new ModuleNode($body, $extends, $blocks, $macros, $traits, new Node([]), $source);
57        $tests[] = [$node, <<<EOF
58<?php
59
60use Twig\Environment;
61use Twig\Error\LoaderError;
62use Twig\Error\RuntimeError;
63use Twig\Markup;
64use Twig\Sandbox\SecurityError;
65use Twig\Sandbox\SecurityNotAllowedTagError;
66use Twig\Sandbox\SecurityNotAllowedFilterError;
67use Twig\Sandbox\SecurityNotAllowedFunctionError;
68use Twig\Source;
69use Twig\Template;
70
71/* foo.twig */
72class __TwigTemplate_%x extends \Twig\Template
73{
74    public function __construct(Environment \$env)
75    {
76        parent::__construct(\$env);
77
78        \$this->parent = false;
79
80        \$this->blocks = [
81        ];
82    }
83
84    protected function doDisplay(array \$context, array \$blocks = [])
85    {
86        // line 1
87        echo "foo";
88    }
89
90    public function getTemplateName()
91    {
92        return "foo.twig";
93    }
94
95    public function getDebugInfo()
96    {
97        return array (  30 => 1,);
98    }
99
100    /** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */
101    public function getSource()
102    {
103        @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', E_USER_DEPRECATED);
104
105        return \$this->getSourceContext()->getCode();
106    }
107
108    public function getSourceContext()
109    {
110        return new Source("", "foo.twig", "");
111    }
112}
113EOF
114        , $twig, true];
115
116        $import = new ImportNode(new ConstantExpression('foo.twig', 1), new AssignNameExpression('macro', 1), 2);
117
118        $body = new Node([$import]);
119        $extends = new ConstantExpression('layout.twig', 1);
120
121        $node = new ModuleNode($body, $extends, $blocks, $macros, $traits, new Node([]), $source);
122        $tests[] = [$node, <<<EOF
123<?php
124
125use Twig\Environment;
126use Twig\Error\LoaderError;
127use Twig\Error\RuntimeError;
128use Twig\Markup;
129use Twig\Sandbox\SecurityError;
130use Twig\Sandbox\SecurityNotAllowedTagError;
131use Twig\Sandbox\SecurityNotAllowedFilterError;
132use Twig\Sandbox\SecurityNotAllowedFunctionError;
133use Twig\Source;
134use Twig\Template;
135
136/* foo.twig */
137class __TwigTemplate_%x extends \Twig\Template
138{
139    public function __construct(Environment \$env)
140    {
141        parent::__construct(\$env);
142
143        // line 1
144        \$this->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1);
145        \$this->blocks = [
146        ];
147    }
148
149    protected function doGetParent(array \$context)
150    {
151        return "layout.twig";
152    }
153
154    protected function doDisplay(array \$context, array \$blocks = [])
155    {
156        // line 2
157        \$context["macro"] = \$this->loadTemplate("foo.twig", "foo.twig", 2);
158        // line 1
159        \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
160    }
161
162    public function getTemplateName()
163    {
164        return "foo.twig";
165    }
166
167    public function isTraitable()
168    {
169        return false;
170    }
171
172    public function getDebugInfo()
173    {
174        return array (  37 => 1,  35 => 2,  22 => 1,);
175    }
176
177    /** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */
178    public function getSource()
179    {
180        @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', E_USER_DEPRECATED);
181
182        return \$this->getSourceContext()->getCode();
183    }
184
185    public function getSourceContext()
186    {
187        return new Source("", "foo.twig", "");
188    }
189}
190EOF
191        , $twig, true];
192
193        $set = new SetNode(false, new Node([new AssignNameExpression('foo', 4)]), new Node([new ConstantExpression('foo', 4)]), 4);
194        $body = new Node([$set]);
195        $extends = new ConditionalExpression(
196                        new ConstantExpression(true, 2),
197                        new ConstantExpression('foo', 2),
198                        new ConstantExpression('foo', 2),
199                        2
200                    );
201
202        $twig = new Environment($this->getMockBuilder('\Twig\Loader\LoaderInterface')->getMock(), ['debug' => true]);
203        $node = new ModuleNode($body, $extends, $blocks, $macros, $traits, new Node([]), $source);
204        $tests[] = [$node, <<<EOF
205<?php
206
207use Twig\Environment;
208use Twig\Error\LoaderError;
209use Twig\Error\RuntimeError;
210use Twig\Markup;
211use Twig\Sandbox\SecurityError;
212use Twig\Sandbox\SecurityNotAllowedTagError;
213use Twig\Sandbox\SecurityNotAllowedFilterError;
214use Twig\Sandbox\SecurityNotAllowedFunctionError;
215use Twig\Source;
216use Twig\Template;
217
218/* foo.twig */
219class __TwigTemplate_%x extends \Twig\Template
220{
221    protected function doGetParent(array \$context)
222    {
223        // line 2
224        return \$this->loadTemplate(((true) ? ("foo") : ("foo")), "foo.twig", 2);
225    }
226
227    protected function doDisplay(array \$context, array \$blocks = [])
228    {
229        // line 4
230        \$context["foo"] = "foo";
231        // line 2
232        \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
233    }
234
235    public function getTemplateName()
236    {
237        return "foo.twig";
238    }
239
240    public function isTraitable()
241    {
242        return false;
243    }
244
245    public function getDebugInfo()
246    {
247        return array (  28 => 2,  26 => 4,  20 => 2,);
248    }
249
250    /** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */
251    public function getSource()
252    {
253        @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', E_USER_DEPRECATED);
254
255        return \$this->getSourceContext()->getCode();
256    }
257
258    public function getSourceContext()
259    {
260        return new Source("{{ foo }}", "foo.twig", "");
261    }
262}
263EOF
264        , $twig, true];
265
266        return $tests;
267    }
268}
269