[CLEANUP] Rework/simplify copyright header and remove @package
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / CycleViewHelperTest.php
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
14 /**
15 * Testcase for CycleViewHelper
16 */
17 class CycleViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase {
18
19 /**
20 * @var \TYPO3\CMS\Fluid\ViewHelpers\CycleViewHelper
21 */
22 protected $viewHelper;
23
24 public function setUp() {
25 parent::setUp();
26 $this->viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\CycleViewHelper', array('renderChildren'));
27 $this->injectDependenciesIntoViewHelper($this->viewHelper);
28 $this->viewHelper->initializeArguments();
29 }
30
31 /**
32 * @test
33 */
34 public function renderAddsCurrentValueToTemplateVariableContainerAndRemovesItAfterRendering() {
35 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'bar');
36 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
37
38 $values = array('bar', 'Fluid');
39 $this->viewHelper->render($values, 'innerVariable');
40 }
41
42 /**
43 * @test
44 */
45 public function renderAddsFirstValueToTemplateVariableContainerAfterLastValue() {
46 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'bar');
47 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
48 $this->templateVariableContainer->expects($this->at(2))->method('add')->with('innerVariable', 'Fluid');
49 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerVariable');
50 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('innerVariable', 'bar');
51 $this->templateVariableContainer->expects($this->at(5))->method('remove')->with('innerVariable');
52
53 $values = array('bar', 'Fluid');
54 $this->viewHelper->render($values, 'innerVariable');
55 $this->viewHelper->render($values, 'innerVariable');
56 $this->viewHelper->render($values, 'innerVariable');
57 }
58
59 /**
60 * @test
61 */
62 public function viewHelperSupportsAssociativeArrays() {
63 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'FLOW3');
64 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
65 $this->templateVariableContainer->expects($this->at(2))->method('add')->with('innerVariable', 'Fluid');
66 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerVariable');
67 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('innerVariable', 'FLOW3');
68 $this->templateVariableContainer->expects($this->at(5))->method('remove')->with('innerVariable');
69
70 $values = array('foo' => 'FLOW3', 'bar' => 'Fluid');
71 $this->viewHelper->render($values, 'innerVariable');
72 $this->viewHelper->render($values, 'innerVariable');
73 $this->viewHelper->render($values, 'innerVariable');
74 }
75
76 /**
77 * @test
78 * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
79 */
80 public function renderThrowsExceptionWhenPassingObjectsToValuesThatAreNotTraversable() {
81 $object = new \stdClass();
82
83 $this->viewHelper->render($object, 'innerVariable');
84 }
85
86 /**
87 * @test
88 */
89 public function renderReturnsChildNodesIfValuesIsNull() {
90 $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('Child nodes'));
91
92 $this->assertEquals('Child nodes', $this->viewHelper->render(NULL, 'foo'));
93 }
94
95 /**
96 * @test
97 */
98 public function renderReturnsChildNodesIfValuesIsAnEmptyArray() {
99 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('foo', NULL);
100 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('foo');
101
102 $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('Child nodes'));
103
104 $this->assertEquals('Child nodes', $this->viewHelper->render(array(), 'foo'));
105 }
106
107 /**
108 * @test
109 */
110 public function renderIteratesThroughElementsOfTraversableObjects() {
111 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'value1');
112 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
113 $this->templateVariableContainer->expects($this->at(2))->method('add')->with('innerVariable', 'value2');
114 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerVariable');
115 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('innerVariable', 'value1');
116 $this->templateVariableContainer->expects($this->at(5))->method('remove')->with('innerVariable');
117
118 $traversableObject = new \ArrayObject(array('key1' => 'value1', 'key2' => 'value2'));
119 $this->viewHelper->render($traversableObject, 'innerVariable');
120 $this->viewHelper->render($traversableObject, 'innerVariable');
121 $this->viewHelper->render($traversableObject, 'innerVariable');
122 }
123 }