Raised Extbase and Fluid to version 1.3.0
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / CycleViewHelperTest.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 require_once(dirname(__FILE__) . '/ViewHelperBaseTestcase.php');
24
25 /**
26 * Testcase for CycleViewHelper
27 *
28 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
29 */
30 class Tx_Fluid_Tests_Unit_ViewHelpers_CycleViewHelperTest extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
31
32 /**
33 * var Tx_Fluid_ViewHelpers_CycleViewHelper
34 */
35 protected $viewHelper;
36
37 public function setUp() {
38 parent::setUp();
39 $this->viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_CycleViewHelper', array('renderChildren'));
40 $this->injectDependenciesIntoViewHelper($this->viewHelper);
41 $this->viewHelper->initializeArguments();
42 }
43
44 /**
45 * @test
46 * @author Bastian Waidelich <bastian@typo3.org>
47 */
48 public function renderAddsCurrentValueToTemplateVariableContainerAndRemovesItAfterRendering() {
49 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'bar');
50 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
51
52 $values = array('bar', 'Fluid');
53 $this->viewHelper->render($values, 'innerVariable');
54 }
55
56 /**
57 * @test
58 * @author Bastian Waidelich <bastian@typo3.org>
59 */
60 public function renderAddsFirstValueToTemplateVariableContainerAfterLastValue() {
61 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'bar');
62 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
63 $this->templateVariableContainer->expects($this->at(2))->method('add')->with('innerVariable', 'Fluid');
64 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerVariable');
65 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('innerVariable', 'bar');
66 $this->templateVariableContainer->expects($this->at(5))->method('remove')->with('innerVariable');
67
68 $values = array('bar', 'Fluid');
69 $this->viewHelper->render($values, 'innerVariable');
70 $this->viewHelper->render($values, 'innerVariable');
71 $this->viewHelper->render($values, 'innerVariable');
72 }
73
74 /**
75 * @test
76 * @author Bastian Waidelich <bastian@typo3.org>
77 */
78 public function viewHelperSupportsAssociativeArrays() {
79 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'FLOW3');
80 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
81 $this->templateVariableContainer->expects($this->at(2))->method('add')->with('innerVariable', 'Fluid');
82 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerVariable');
83 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('innerVariable', 'FLOW3');
84 $this->templateVariableContainer->expects($this->at(5))->method('remove')->with('innerVariable');
85
86 $values = array('foo' => 'FLOW3', 'bar' => 'Fluid');
87 $this->viewHelper->render($values, 'innerVariable');
88 $this->viewHelper->render($values, 'innerVariable');
89 $this->viewHelper->render($values, 'innerVariable');
90 }
91
92 /**
93 * @test
94 * @expectedException Tx_Fluid_Core_ViewHelper_Exception
95 * @author Bastian Waidelich <bastian@typo3.org>
96 */
97 public function renderThrowsExceptionWhenPassingObjectsToValuesThatAreNotTraversable() {
98 $object = new stdClass();
99
100 $this->viewHelper->render($object, 'innerVariable');
101 }
102
103 /**
104 * @test
105 * @author Bastian Waidelich <bastian@typo3.org>
106 */
107 public function renderReturnsChildNodesIfValuesIsNull() {
108 $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('Child nodes'));
109
110 $this->assertEquals('Child nodes', $this->viewHelper->render(NULL, 'foo'));
111 }
112
113 /**
114 * @test
115 * @author Bastian Waidelich <bastian@typo3.org>
116 */
117 public function renderReturnsChildNodesIfValuesIsAnEmptyArray() {
118 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('foo', NULL);
119 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('foo');
120
121 $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('Child nodes'));
122
123 $this->assertEquals('Child nodes', $this->viewHelper->render(array(), 'foo'));
124 }
125
126 /**
127 * @test
128 * @author Bastian Waidelich <bastian@typo3.org>
129 */
130 public function renderIteratesThroughElementsOfTraversableObjects() {
131 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'value1');
132 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
133 $this->templateVariableContainer->expects($this->at(2))->method('add')->with('innerVariable', 'value2');
134 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerVariable');
135 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('innerVariable', 'value1');
136 $this->templateVariableContainer->expects($this->at(5))->method('remove')->with('innerVariable');
137
138 $traversableObject = new ArrayObject(array('key1' => 'value1', 'key2' => 'value2'));
139 $this->viewHelper->render($traversableObject, 'innerVariable');
140 $this->viewHelper->render($traversableObject, 'innerVariable');
141 $this->viewHelper->render($traversableObject, 'innerVariable');
142 }
143 }
144
145 ?>