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