TYPO3 CMS  TYPO3_7-6
ForViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
18 {
19  protected function setUp()
20  {
21  parent::setUp();
22  $this->templateVariableContainer = new \TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer([]);
23  $this->renderingContext->injectTemplateVariableContainer($this->templateVariableContainer);
24 
25  $this->arguments['reverse'] = null;
26  $this->arguments['key'] = '';
27  $this->arguments['iteration'] = null;
28  }
29 
34  {
35  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
36 
37  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
38  $this->arguments['each'] = [0, 1, 2, 3];
39  $this->arguments['as'] = 'innerVariable';
40 
41  $this->injectDependenciesIntoViewHelper($viewHelper);
42  $viewHelper->setViewHelperNode($viewHelperNode);
43  $viewHelper->render($this->arguments['each'], $this->arguments['as']);
44 
45  $expectedCallProtocol = [
46  ['innerVariable' => 0],
47  ['innerVariable' => 1],
48  ['innerVariable' => 2],
49  ['innerVariable' => 3]
50  ];
51  $this->assertEquals($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
52  }
53 
57  public function renderPreservesKeys()
58  {
59  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
60 
61  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
62 
63  $this->arguments['each'] = ['key1' => 'value1', 'key2' => 'value2'];
64  $this->arguments['as'] = 'innerVariable';
65  $this->arguments['key'] = 'someKey';
66 
67  $this->injectDependenciesIntoViewHelper($viewHelper);
68  $viewHelper->setViewHelperNode($viewHelperNode);
69  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key']);
70 
71  $expectedCallProtocol = [
72  [
73  'innerVariable' => 'value1',
74  'someKey' => 'key1'
75  ],
76  [
77  'innerVariable' => 'value2',
78  'someKey' => 'key2'
79  ]
80  ];
81  $this->assertEquals($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
82  }
83 
88  {
89  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
90 
91  $this->arguments['each'] = null;
92  $this->arguments['as'] = 'foo';
93 
94  $this->injectDependenciesIntoViewHelper($viewHelper);
95 
96  $this->assertEquals('', $viewHelper->render($this->arguments['each'], $this->arguments['as']));
97  }
98 
103  {
104  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
105 
106  $this->arguments['each'] = [];
107  $this->arguments['as'] = 'foo';
108 
109  $this->injectDependenciesIntoViewHelper($viewHelper);
110 
111  $this->assertEquals('', $viewHelper->render($this->arguments['each'], $this->arguments['as']));
112  }
113 
118  {
119  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
120 
121  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
122 
123  $this->arguments['each'] = [0, 1, 2, 3];
124  $this->arguments['as'] = 'innerVariable';
125  $this->arguments['reverse'] = true;
126 
127  $this->injectDependenciesIntoViewHelper($viewHelper);
128  $viewHelper->setViewHelperNode($viewHelperNode);
129  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key'], $this->arguments['reverse']);
130 
131  $expectedCallProtocol = [
132  ['innerVariable' => 3],
133  ['innerVariable' => 2],
134  ['innerVariable' => 1],
135  ['innerVariable' => 0]
136  ];
137  $this->assertEquals($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
138  }
139 
144  {
145  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
146 
147  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
148 
149  $this->arguments['each'] = ['key1' => 'value1', 'key2' => 'value2'];
150  $this->arguments['as'] = 'innerVariable';
151  $this->arguments['key'] = 'someKey';
152  $this->arguments['reverse'] = true;
153 
154  $this->injectDependenciesIntoViewHelper($viewHelper);
155  $viewHelper->setViewHelperNode($viewHelperNode);
156  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key'], $this->arguments['reverse']);
157 
158  $expectedCallProtocol = [
159  [
160  'innerVariable' => 'value2',
161  'someKey' => 'key2'
162  ],
163  [
164  'innerVariable' => 'value1',
165  'someKey' => 'key1'
166  ]
167  ];
168  $this->assertEquals($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
169  }
170 
175  {
176  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
177 
178  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
179 
180  $this->arguments['each'] = ['foo', 'bar', 'baz'];
181  $this->arguments['as'] = 'innerVariable';
182  $this->arguments['key'] = 'someKey';
183 
184  $this->injectDependenciesIntoViewHelper($viewHelper);
185  $viewHelper->setViewHelperNode($viewHelperNode);
186  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key']);
187 
188  $expectedCallProtocol = [
189  [
190  'innerVariable' => 'foo',
191  'someKey' => 0
192  ],
193  [
194  'innerVariable' => 'bar',
195  'someKey' => 1
196  ],
197  [
198  'innerVariable' => 'baz',
199  'someKey' => 2
200  ]
201  ];
202  $this->assertSame($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
203  }
204 
209  {
210  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
211 
212  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
213 
214  $this->arguments['each'] = ['foo', 'bar', 'baz'];
215  $this->arguments['as'] = 'innerVariable';
216  $this->arguments['key'] = 'someKey';
217  $this->arguments['reverse'] = true;
218 
219  $this->injectDependenciesIntoViewHelper($viewHelper);
220  $viewHelper->setViewHelperNode($viewHelperNode);
221  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key'], $this->arguments['reverse']);
222 
223  $expectedCallProtocol = [
224  [
225  'innerVariable' => 'baz',
226  'someKey' => 0
227  ],
228  [
229  'innerVariable' => 'bar',
230  'someKey' => 1
231  ],
232  [
233  'innerVariable' => 'foo',
234  'someKey' => 2
235  ]
236  ];
237  $this->assertSame($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
238  }
239 
245  {
246  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
247  $object = new \stdClass();
248 
249  $this->arguments['each'] = $object;
250  $this->arguments['as'] = 'innerVariable';
251  $this->arguments['key'] = 'someKey';
252  $this->arguments['reverse'] = true;
253 
254  $this->injectDependenciesIntoViewHelper($viewHelper);
255  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key']);
256  }
257 
262  {
263  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
264 
265  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
266 
267  $this->arguments['each'] = new \ArrayObject(['key1' => 'value1', 'key2' => 'value2']);
268  $this->arguments['as'] = 'innerVariable';
269 
270  $this->injectDependenciesIntoViewHelper($viewHelper);
271  $viewHelper->setViewHelperNode($viewHelperNode);
272  $viewHelper->render($this->arguments['each'], $this->arguments['as']);
273 
274  $expectedCallProtocol = [
275  ['innerVariable' => 'value1'],
276  ['innerVariable' => 'value2']
277  ];
278  $this->assertEquals($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
279  }
280 
285  {
286  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
287 
288  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
289 
290  $this->arguments['each'] = new \ArrayIterator(['key1' => 'value1', 'key2' => 'value2']);
291  $this->arguments['as'] = 'innerVariable';
292  $this->arguments['key'] = 'someKey';
293 
294  $this->injectDependenciesIntoViewHelper($viewHelper);
295  $viewHelper->setViewHelperNode($viewHelperNode);
296  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key']);
297 
298  $expectedCallProtocol = [
299  [
300  'innerVariable' => 'value1',
301  'someKey' => 'key1'
302  ],
303  [
304  'innerVariable' => 'value2',
305  'someKey' => 'key2'
306  ]
307  ];
308  $this->assertEquals($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
309  }
310 
315  {
316  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
317 
318  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
319 
320  $splObjectStorageObject = new \SplObjectStorage();
321  $object1 = new \stdClass();
322  $splObjectStorageObject->attach($object1);
323  $object2 = new \stdClass();
324  $splObjectStorageObject->attach($object2, 'foo');
325  $object3 = new \stdClass();
326  $splObjectStorageObject->attach($object3, 'bar');
327 
328  $this->arguments['each'] = $splObjectStorageObject;
329  $this->arguments['as'] = 'innerVariable';
330  $this->arguments['key'] = 'someKey';
331 
332  $this->injectDependenciesIntoViewHelper($viewHelper);
333  $viewHelper->setViewHelperNode($viewHelperNode);
334  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key']);
335 
336  $expectedCallProtocol = [
337  [
338  'innerVariable' => $object1,
339  'someKey' => 0
340  ],
341  [
342  'innerVariable' => $object2,
343  'someKey' => 1
344  ],
345  [
346  'innerVariable' => $object3,
347  'someKey' => 2
348  ]
349  ];
350  $this->assertSame($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
351  }
352 
357  {
358  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
359 
360  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
361 
362  $this->arguments['each'] = ['foo' => 'bar', 'FLOW3' => 'Fluid', 'TYPO3' => 'rocks'];
363  $this->arguments['as'] = 'innerVariable';
364  $this->arguments['iteration'] = 'iteration';
365 
366  $this->injectDependenciesIntoViewHelper($viewHelper);
367  $viewHelper->setViewHelperNode($viewHelperNode);
368  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key'], $this->arguments['reverse'], $this->arguments['iteration']);
369 
370  $expectedCallProtocol = [
371  [
372  'innerVariable' => 'bar',
373  'iteration' => [
374  'index' => 0,
375  'cycle' => 1,
376  'total' => 3,
377  'isFirst' => true,
378  'isLast' => false,
379  'isEven' => false,
380  'isOdd' => true
381  ]
382  ],
383  [
384  'innerVariable' => 'Fluid',
385  'iteration' => [
386  'index' => 1,
387  'cycle' => 2,
388  'total' => 3,
389  'isFirst' => false,
390  'isLast' => false,
391  'isEven' => true,
392  'isOdd' => false
393  ]
394  ],
395  [
396  'innerVariable' => 'rocks',
397  'iteration' => [
398  'index' => 2,
399  'cycle' => 3,
400  'total' => 3,
401  'isFirst' => false,
402  'isLast' => true,
403  'isEven' => false,
404  'isOdd' => true
405  ]
406  ]
407  ];
408  $this->assertSame($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
409  }
410 
415  {
416  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
417 
418  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
419 
420  $mockItems = $this->getMockBuilder(\ArrayObject::class)->setMethods(['count'])->disableOriginalConstructor()->getMock();
421  $mockItems->expects($this->never())->method('count');
422  $this->arguments['each'] = $mockItems;
423  $this->arguments['as'] = 'innerVariable';
424 
425  $this->injectDependenciesIntoViewHelper($viewHelper);
426  $viewHelper->setViewHelperNode($viewHelperNode);
427  $viewHelper->render($this->arguments['each'], $this->arguments['as']);
428  }
429 }
injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)