TYPO3  7.6
ForViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
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(array());
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'] = array(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 = array(
46  array('innerVariable' => 0),
47  array('innerVariable' => 1),
48  array('innerVariable' => 2),
49  array('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'] = array('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 = array(
72  array(
73  'innerVariable' => 'value1',
74  'someKey' => 'key1'
75  ),
76  array(
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'] = array();
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'] = array(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 = array(
132  array('innerVariable' => 3),
133  array('innerVariable' => 2),
134  array('innerVariable' => 1),
135  array('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'] = array('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 = array(
159  array(
160  'innerVariable' => 'value2',
161  'someKey' => 'key2'
162  ),
163  array(
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'] = array('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 = array(
189  array(
190  'innerVariable' => 'foo',
191  'someKey' => 0
192  ),
193  array(
194  'innerVariable' => 'bar',
195  'someKey' => 1
196  ),
197  array(
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'] = array('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 = array(
224  array(
225  'innerVariable' => 'baz',
226  'someKey' => 0
227  ),
228  array(
229  'innerVariable' => 'bar',
230  'someKey' => 1
231  ),
232  array(
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 
258 
263  {
264  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
265 
266  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
267 
268  $this->arguments['each'] = new \ArrayObject(array('key1' => 'value1', 'key2' => 'value2'));
269  $this->arguments['as'] = 'innerVariable';
270 
271  $this->injectDependenciesIntoViewHelper($viewHelper);
272  $viewHelper->setViewHelperNode($viewHelperNode);
273  $viewHelper->render($this->arguments['each'], $this->arguments['as']);
274 
275  $expectedCallProtocol = array(
276  array('innerVariable' => 'value1'),
277  array('innerVariable' => 'value2')
278  );
279  $this->assertEquals($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
280  }
281 
286  {
287  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
288 
289  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
290 
291  $this->arguments['each'] = new \ArrayIterator(array('key1' => 'value1', 'key2' => 'value2'));
292  $this->arguments['as'] = 'innerVariable';
293  $this->arguments['key'] = 'someKey';
294 
295  $this->injectDependenciesIntoViewHelper($viewHelper);
296  $viewHelper->setViewHelperNode($viewHelperNode);
297  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key']);
298 
299  $expectedCallProtocol = array(
300  array(
301  'innerVariable' => 'value1',
302  'someKey' => 'key1'
303  ),
304  array(
305  'innerVariable' => 'value2',
306  'someKey' => 'key2'
307  )
308  );
309  $this->assertEquals($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
310  }
311 
316  {
317  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
318 
319  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
320 
321  $splObjectStorageObject = new \SplObjectStorage();
322  $object1 = new \stdClass();
323  $splObjectStorageObject->attach($object1);
324  $object2 = new \stdClass();
325  $splObjectStorageObject->attach($object2, 'foo');
326  $object3 = new \stdClass();
327  $splObjectStorageObject->attach($object3, 'bar');
328 
329  $this->arguments['each'] = $splObjectStorageObject;
330  $this->arguments['as'] = 'innerVariable';
331  $this->arguments['key'] = 'someKey';
332 
333  $this->injectDependenciesIntoViewHelper($viewHelper);
334  $viewHelper->setViewHelperNode($viewHelperNode);
335  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key']);
336 
337  $expectedCallProtocol = array(
338  array(
339  'innerVariable' => $object1,
340  'someKey' => 0
341  ),
342  array(
343  'innerVariable' => $object2,
344  'someKey' => 1
345  ),
346  array(
347  'innerVariable' => $object3,
348  'someKey' => 2
349  )
350  );
351  $this->assertSame($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
352  }
353 
358  {
359  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
360 
361  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
362 
363  $this->arguments['each'] = array('foo' => 'bar', 'FLOW3' => 'Fluid', 'TYPO3' => 'rocks');
364  $this->arguments['as'] = 'innerVariable';
365  $this->arguments['iteration'] = 'iteration';
366 
367  $this->injectDependenciesIntoViewHelper($viewHelper);
368  $viewHelper->setViewHelperNode($viewHelperNode);
369  $viewHelper->render($this->arguments['each'], $this->arguments['as'], $this->arguments['key'], $this->arguments['reverse'], $this->arguments['iteration']);
370 
371  $expectedCallProtocol = array(
372  array(
373  'innerVariable' => 'bar',
374  'iteration' => array(
375  'index' => 0,
376  'cycle' => 1,
377  'total' => 3,
378  'isFirst' => true,
379  'isLast' => false,
380  'isEven' => false,
381  'isOdd' => true
382  )
383  ),
384  array(
385  'innerVariable' => 'Fluid',
386  'iteration' => array(
387  'index' => 1,
388  'cycle' => 2,
389  'total' => 3,
390  'isFirst' => false,
391  'isLast' => false,
392  'isEven' => true,
393  'isOdd' => false
394  )
395  ),
396  array(
397  'innerVariable' => 'rocks',
398  'iteration' => array(
399  'index' => 2,
400  'cycle' => 3,
401  'total' => 3,
402  'isFirst' => false,
403  'isLast' => true,
404  'isEven' => false,
405  'isOdd' => true
406  )
407  )
408  );
409  $this->assertSame($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');
410  }
411 
416  {
417  $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\ForViewHelper();
418 
419  $viewHelperNode = new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\ConstraintSyntaxTreeNode($this->templateVariableContainer);
420 
421  $mockItems = $this->getMockBuilder(\ArrayObject::class)->setMethods(['count'])->disableOriginalConstructor()->getMock();
422  $mockItems->expects($this->never())->method('count');
423  $this->arguments['each'] = $mockItems;
424  $this->arguments['as'] = 'innerVariable';
425 
426  $this->injectDependenciesIntoViewHelper($viewHelper);
427  $viewHelper->setViewHelperNode($viewHelperNode);
428  $viewHelper->render($this->arguments['each'], $this->arguments['as']);
429  }
430 }