Security update for Core, with self-updated composer
[yaffs-website] / vendor / twig / twig / test / Twig / Tests / Node / ForTest.php
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_Node_ForTest extends Twig_Test_NodeTestCase
13 {
14     public function testConstructor()
15     {
16         $keyTarget = new Twig_Node_Expression_AssignName('key', 1);
17         $valueTarget = new Twig_Node_Expression_AssignName('item', 1);
18         $seq = new Twig_Node_Expression_Name('items', 1);
19         $ifexpr = new Twig_Node_Expression_Constant(true, 1);
20         $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1);
21         $else = null;
22         $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
23         $node->setAttribute('with_loop', false);
24
25         $this->assertEquals($keyTarget, $node->getNode('key_target'));
26         $this->assertEquals($valueTarget, $node->getNode('value_target'));
27         $this->assertEquals($seq, $node->getNode('seq'));
28         $this->assertTrue($node->getAttribute('ifexpr'));
29         $this->assertEquals('Twig_Node_If', get_class($node->getNode('body')));
30         $this->assertEquals($body, $node->getNode('body')->getNode('tests')->getNode(1)->getNode(0));
31         $this->assertFalse($node->hasNode('else'));
32
33         $else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1);
34         $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
35         $node->setAttribute('with_loop', false);
36         $this->assertEquals($else, $node->getNode('else'));
37     }
38
39     public function getTests()
40     {
41         $tests = array();
42
43         $keyTarget = new Twig_Node_Expression_AssignName('key', 1);
44         $valueTarget = new Twig_Node_Expression_AssignName('item', 1);
45         $seq = new Twig_Node_Expression_Name('items', 1);
46         $ifexpr = null;
47         $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1);
48         $else = null;
49         $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
50         $node->setAttribute('with_loop', false);
51
52         $tests[] = array($node, <<<EOF
53 // line 1
54 \$context['_parent'] = \$context;
55 \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('items')});
56 foreach (\$context['_seq'] as \$context["key"] => \$context["item"]) {
57     echo {$this->getVariableGetter('foo')};
58 }
59 \$_parent = \$context['_parent'];
60 unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']);
61 \$context = array_intersect_key(\$context, \$_parent) + \$_parent;
62 EOF
63         );
64
65         $keyTarget = new Twig_Node_Expression_AssignName('k', 1);
66         $valueTarget = new Twig_Node_Expression_AssignName('v', 1);
67         $seq = new Twig_Node_Expression_Name('values', 1);
68         $ifexpr = null;
69         $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1);
70         $else = null;
71         $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
72         $node->setAttribute('with_loop', true);
73
74         $tests[] = array($node, <<<EOF
75 // line 1
76 \$context['_parent'] = \$context;
77 \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')});
78 \$context['loop'] = array(
79   'parent' => \$context['_parent'],
80   'index0' => 0,
81   'index'  => 1,
82   'first'  => true,
83 );
84 if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {
85     \$length = count(\$context['_seq']);
86     \$context['loop']['revindex0'] = \$length - 1;
87     \$context['loop']['revindex'] = \$length;
88     \$context['loop']['length'] = \$length;
89     \$context['loop']['last'] = 1 === \$length;
90 }
91 foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
92     echo {$this->getVariableGetter('foo')};
93     ++\$context['loop']['index0'];
94     ++\$context['loop']['index'];
95     \$context['loop']['first'] = false;
96     if (isset(\$context['loop']['length'])) {
97         --\$context['loop']['revindex0'];
98         --\$context['loop']['revindex'];
99         \$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
100     }
101 }
102 \$_parent = \$context['_parent'];
103 unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
104 \$context = array_intersect_key(\$context, \$_parent) + \$_parent;
105 EOF
106         );
107
108         $keyTarget = new Twig_Node_Expression_AssignName('k', 1);
109         $valueTarget = new Twig_Node_Expression_AssignName('v', 1);
110         $seq = new Twig_Node_Expression_Name('values', 1);
111         $ifexpr = new Twig_Node_Expression_Constant(true, 1);
112         $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1);
113         $else = null;
114         $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
115         $node->setAttribute('with_loop', true);
116
117         $tests[] = array($node, <<<EOF
118 // line 1
119 \$context['_parent'] = \$context;
120 \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')});
121 \$context['loop'] = array(
122   'parent' => \$context['_parent'],
123   'index0' => 0,
124   'index'  => 1,
125   'first'  => true,
126 );
127 foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
128     if (true) {
129         echo {$this->getVariableGetter('foo')};
130         ++\$context['loop']['index0'];
131         ++\$context['loop']['index'];
132         \$context['loop']['first'] = false;
133     }
134 }
135 \$_parent = \$context['_parent'];
136 unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
137 \$context = array_intersect_key(\$context, \$_parent) + \$_parent;
138 EOF
139         );
140
141         $keyTarget = new Twig_Node_Expression_AssignName('k', 1);
142         $valueTarget = new Twig_Node_Expression_AssignName('v', 1);
143         $seq = new Twig_Node_Expression_Name('values', 1);
144         $ifexpr = null;
145         $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1);
146         $else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1);
147         $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
148         $node->setAttribute('with_loop', true);
149
150         $tests[] = array($node, <<<EOF
151 // line 1
152 \$context['_parent'] = \$context;
153 \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')});
154 \$context['_iterated'] = false;
155 \$context['loop'] = array(
156   'parent' => \$context['_parent'],
157   'index0' => 0,
158   'index'  => 1,
159   'first'  => true,
160 );
161 if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {
162     \$length = count(\$context['_seq']);
163     \$context['loop']['revindex0'] = \$length - 1;
164     \$context['loop']['revindex'] = \$length;
165     \$context['loop']['length'] = \$length;
166     \$context['loop']['last'] = 1 === \$length;
167 }
168 foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
169     echo {$this->getVariableGetter('foo')};
170     \$context['_iterated'] = true;
171     ++\$context['loop']['index0'];
172     ++\$context['loop']['index'];
173     \$context['loop']['first'] = false;
174     if (isset(\$context['loop']['length'])) {
175         --\$context['loop']['revindex0'];
176         --\$context['loop']['revindex'];
177         \$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
178     }
179 }
180 if (!\$context['_iterated']) {
181     echo {$this->getVariableGetter('foo')};
182 }
183 \$_parent = \$context['_parent'];
184 unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
185 \$context = array_intersect_key(\$context, \$_parent) + \$_parent;
186 EOF
187         );
188
189         return $tests;
190     }
191 }