diff options
Diffstat (limited to 'vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php')
-rw-r--r-- | vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php | 282 |
1 files changed, 282 insertions, 0 deletions
diff --git a/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php b/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php new file mode 100644 index 00000000..c1f247ca --- /dev/null +++ b/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php | |||
@@ -0,0 +1,282 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
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 | namespace Symfony\Bridge\Twig\Tests\Node; | ||
13 | |||
14 | use Symfony\Bridge\Twig\Tests\TestCase; | ||
15 | use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode; | ||
16 | |||
17 | class SearchAndRenderBlockNodeTest extends TestCase | ||
18 | { | ||
19 | protected function setUp() | ||
20 | { | ||
21 | parent::setUp(); | ||
22 | |||
23 | if (version_compare(\Twig_Environment::VERSION, '1.5.0', '<')) { | ||
24 | $this->markTestSkipped('Requires Twig version to be at least 1.5.0.'); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | public function testCompileWidget() | ||
29 | { | ||
30 | $arguments = new \Twig_Node(array( | ||
31 | new \Twig_Node_Expression_Name('form', 0), | ||
32 | )); | ||
33 | |||
34 | $node = new SearchAndRenderBlockNode('form_widget', $arguments, 0); | ||
35 | |||
36 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | ||
37 | |||
38 | $this->assertEquals( | ||
39 | sprintf( | ||
40 | '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\')', | ||
41 | $this->getVariableGetter('form') | ||
42 | ), | ||
43 | trim($compiler->compile($node)->getSource()) | ||
44 | ); | ||
45 | } | ||
46 | |||
47 | public function testCompileWidgetWithVariables() | ||
48 | { | ||
49 | $arguments = new \Twig_Node(array( | ||
50 | new \Twig_Node_Expression_Name('form', 0), | ||
51 | new \Twig_Node_Expression_Array(array( | ||
52 | new \Twig_Node_Expression_Constant('foo', 0), | ||
53 | new \Twig_Node_Expression_Constant('bar', 0), | ||
54 | ), 0), | ||
55 | )); | ||
56 | |||
57 | $node = new SearchAndRenderBlockNode('form_widget', $arguments, 0); | ||
58 | |||
59 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | ||
60 | |||
61 | $this->assertEquals( | ||
62 | sprintf( | ||
63 | '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))', | ||
64 | $this->getVariableGetter('form') | ||
65 | ), | ||
66 | trim($compiler->compile($node)->getSource()) | ||
67 | ); | ||
68 | } | ||
69 | |||
70 | public function testCompileLabelWithLabel() | ||
71 | { | ||
72 | $arguments = new \Twig_Node(array( | ||
73 | new \Twig_Node_Expression_Name('form', 0), | ||
74 | new \Twig_Node_Expression_Constant('my label', 0), | ||
75 | )); | ||
76 | |||
77 | $node = new SearchAndRenderBlockNode('form_label', $arguments, 0); | ||
78 | |||
79 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | ||
80 | |||
81 | $this->assertEquals( | ||
82 | sprintf( | ||
83 | '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))', | ||
84 | $this->getVariableGetter('form') | ||
85 | ), | ||
86 | trim($compiler->compile($node)->getSource()) | ||
87 | ); | ||
88 | } | ||
89 | |||
90 | public function testCompileLabelWithNullLabel() | ||
91 | { | ||
92 | $arguments = new \Twig_Node(array( | ||
93 | new \Twig_Node_Expression_Name('form', 0), | ||
94 | new \Twig_Node_Expression_Constant(null, 0), | ||
95 | )); | ||
96 | |||
97 | $node = new SearchAndRenderBlockNode('form_label', $arguments, 0); | ||
98 | |||
99 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | ||
100 | |||
101 | // "label" => null must not be included in the output! | ||
102 | // Otherwise the default label is overwritten with null. | ||
103 | $this->assertEquals( | ||
104 | sprintf( | ||
105 | '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')', | ||
106 | $this->getVariableGetter('form') | ||
107 | ), | ||
108 | trim($compiler->compile($node)->getSource()) | ||
109 | ); | ||
110 | } | ||
111 | |||
112 | public function testCompileLabelWithEmptyStringLabel() | ||
113 | { | ||
114 | $arguments = new \Twig_Node(array( | ||
115 | new \Twig_Node_Expression_Name('form', 0), | ||
116 | new \Twig_Node_Expression_Constant('', 0), | ||
117 | )); | ||
118 | |||
119 | $node = new SearchAndRenderBlockNode('form_label', $arguments, 0); | ||
120 | |||
121 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | ||
122 | |||
123 | // "label" => null must not be included in the output! | ||
124 | // Otherwise the default label is overwritten with null. | ||
125 | $this->assertEquals( | ||
126 | sprintf( | ||
127 | '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')', | ||
128 | $this->getVariableGetter('form') | ||
129 | ), | ||
130 | trim($compiler->compile($node)->getSource()) | ||
131 | ); | ||
132 | } | ||
133 | |||
134 | public function testCompileLabelWithDefaultLabel() | ||
135 | { | ||
136 | $arguments = new \Twig_Node(array( | ||
137 | new \Twig_Node_Expression_Name('form', 0), | ||
138 | )); | ||
139 | |||
140 | $node = new SearchAndRenderBlockNode('form_label', $arguments, 0); | ||
141 | |||
142 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | ||
143 | |||
144 | $this->assertEquals( | ||
145 | sprintf( | ||
146 | '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')', | ||
147 | $this->getVariableGetter('form') | ||
148 | ), | ||
149 | trim($compiler->compile($node)->getSource()) | ||
150 | ); | ||
151 | } | ||
152 | |||
153 | public function testCompileLabelWithAttributes() | ||
154 | { | ||
155 | $arguments = new \Twig_Node(array( | ||
156 | new \Twig_Node_Expression_Name('form', 0), | ||
157 | new \Twig_Node_Expression_Constant(null, 0), | ||
158 | new \Twig_Node_Expression_Array(array( | ||
159 | new \Twig_Node_Expression_Constant('foo', 0), | ||
160 | new \Twig_Node_Expression_Constant('bar', 0), | ||
161 | ), 0), | ||
162 | )); | ||
163 | |||
164 | $node = new SearchAndRenderBlockNode('form_label', $arguments, 0); | ||
165 | |||
166 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | ||
167 | |||
168 | // "label" => null must not be included in the output! | ||
169 | // Otherwise the default label is overwritten with null. | ||
170 | // https://github.com/symfony/symfony/issues/5029 | ||
171 | $this->assertEquals( | ||
172 | sprintf( | ||
173 | '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))', | ||
174 | $this->getVariableGetter('form') | ||
175 | ), | ||
176 | trim($compiler->compile($node)->getSource()) | ||
177 | ); | ||
178 | } | ||
179 | |||
180 | public function testCompileLabelWithLabelAndAttributes() | ||
181 | { | ||
182 | $arguments = new \Twig_Node(array( | ||
183 | new \Twig_Node_Expression_Name('form', 0), | ||
184 | new \Twig_Node_Expression_Constant('value in argument', 0), | ||
185 | new \Twig_Node_Expression_Array(array( | ||
186 | new \Twig_Node_Expression_Constant('foo', 0), | ||
187 | new \Twig_Node_Expression_Constant('bar', 0), | ||
188 | new \Twig_Node_Expression_Constant('label', 0), | ||
189 | new \Twig_Node_Expression_Constant('value in attributes', 0), | ||
190 | ), 0), | ||
191 | )); | ||
192 | |||
193 | $node = new SearchAndRenderBlockNode('form_label', $arguments, 0); | ||
194 | |||
195 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | ||
196 | |||
197 | $this->assertEquals( | ||
198 | sprintf( | ||
199 | '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))', | ||
200 | $this->getVariableGetter('form') | ||
201 | ), | ||
202 | trim($compiler->compile($node)->getSource()) | ||
203 | ); | ||
204 | } | ||
205 | |||
206 | public function testCompileLabelWithLabelThatEvaluatesToNull() | ||
207 | { | ||
208 | $arguments = new \Twig_Node(array( | ||
209 | new \Twig_Node_Expression_Name('form', 0), | ||
210 | new \Twig_Node_Expression_Conditional( | ||
211 | // if | ||
212 | new \Twig_Node_Expression_Constant(true, 0), | ||
213 | // then | ||
214 | new \Twig_Node_Expression_Constant(null, 0), | ||
215 | // else | ||
216 | new \Twig_Node_Expression_Constant(null, 0), | ||
217 | 0 | ||
218 | ), | ||
219 | )); | ||
220 | |||
221 | $node = new SearchAndRenderBlockNode('form_label', $arguments, 0); | ||
222 | |||
223 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | ||
224 | |||
225 | // "label" => null must not be included in the output! | ||
226 | // Otherwise the default label is overwritten with null. | ||
227 | // https://github.com/symfony/symfony/issues/5029 | ||
228 | $this->assertEquals( | ||
229 | sprintf( | ||
230 | '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))', | ||
231 | $this->getVariableGetter('form') | ||
232 | ), | ||
233 | trim($compiler->compile($node)->getSource()) | ||
234 | ); | ||
235 | } | ||
236 | |||
237 | public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes() | ||
238 | { | ||
239 | $arguments = new \Twig_Node(array( | ||
240 | new \Twig_Node_Expression_Name('form', 0), | ||
241 | new \Twig_Node_Expression_Conditional( | ||
242 | // if | ||
243 | new \Twig_Node_Expression_Constant(true, 0), | ||
244 | // then | ||
245 | new \Twig_Node_Expression_Constant(null, 0), | ||
246 | // else | ||
247 | new \Twig_Node_Expression_Constant(null, 0), | ||
248 | 0 | ||
249 | ), | ||
250 | new \Twig_Node_Expression_Array(array( | ||
251 | new \Twig_Node_Expression_Constant('foo', 0), | ||
252 | new \Twig_Node_Expression_Constant('bar', 0), | ||
253 | new \Twig_Node_Expression_Constant('label', 0), | ||
254 | new \Twig_Node_Expression_Constant('value in attributes', 0), | ||
255 | ), 0), | ||
256 | )); | ||
257 | |||
258 | $node = new SearchAndRenderBlockNode('form_label', $arguments, 0); | ||
259 | |||
260 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | ||
261 | |||
262 | // "label" => null must not be included in the output! | ||
263 | // Otherwise the default label is overwritten with null. | ||
264 | // https://github.com/symfony/symfony/issues/5029 | ||
265 | $this->assertEquals( | ||
266 | sprintf( | ||
267 | '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in attributes") + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))', | ||
268 | $this->getVariableGetter('form') | ||
269 | ), | ||
270 | trim($compiler->compile($node)->getSource()) | ||
271 | ); | ||
272 | } | ||
273 | |||
274 | protected function getVariableGetter($name) | ||
275 | { | ||
276 | if (version_compare(phpversion(), '5.4.0RC1', '>=')) { | ||
277 | return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name); | ||
278 | } | ||
279 | |||
280 | return sprintf('$this->getContext($context, "%s")', $name); | ||
281 | } | ||
282 | } | ||