[CLEANUP] Adjust code to CGL and fix small namespace bugs
[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 FLOW3 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 require_once dirname(__FILE__) . '/ViewHelperBaseTestcase.php';
14
15 /**
16 * Testcase for CycleViewHelper
17 */
18 class CycleViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase {
19
20 /**
21 * @var \TYPO3\CMS\Fluid\ViewHelpers\CycleViewHelper
22 */
23 protected $viewHelper;
24
25 public function setUp() {
26 parent::setUp();
27 $this->viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\CycleViewHelper', array('renderChildren'));
28 $this->injectDependenciesIntoViewHelper($this->viewHelper);
29 $this->viewHelper->initializeArguments();
30 }
31
32 /**
33 * @test
34 */
35 public function renderAddsCurrentValueToTemplateVariableContainerAndRemovesItAfterRendering() {
36 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'bar');
37 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
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 $values = array('bar', 'Fluid');
53 $this->viewHelper->render($values, 'innerVariable');
54 $this->viewHelper->render($values, 'innerVariable');
55 $this->viewHelper->render($values, 'innerVariable');
56 }
57
58 /**
59 * @test
60 */
61 public function viewHelperSupportsAssociativeArrays() {
62 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'FLOW3');
63 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
64 $this->templateVariableContainer->expects($this->at(2))->method('add')->with('innerVariable', 'Fluid');
65 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerVariable');
66 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('innerVariable', 'FLOW3');
67 $this->templateVariableContainer->expects($this->at(5))->method('remove')->with('innerVariable');
68 $values = array('foo' => 'FLOW3', '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 * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
77 */
78 public function renderThrowsExceptionWhenPassingObjectsToValuesThatAreNotTraversable() {
79 $object = new \stdClass();
80 $this->viewHelper->render($object, 'innerVariable');
81 }
82
83 /**
84 * @test
85 */
86 public function renderReturnsChildNodesIfValuesIsNull() {
87 $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('Child nodes'));
88 $this->assertEquals('Child nodes', $this->viewHelper->render(NULL, 'foo'));
89 }
90
91 /**
92 * @test
93 */
94 public function renderReturnsChildNodesIfValuesIsAnEmptyArray() {
95 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('foo', NULL);
96 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('foo');
97 $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('Child nodes'));
98 $this->assertEquals('Child nodes', $this->viewHelper->render(array(), 'foo'));
99 }
100
101 /**
102 * @test
103 */
104 public function renderIteratesThroughElementsOfTraversableObjects() {
105 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'value1');
106 $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
107 $this->templateVariableContainer->expects($this->at(2))->method('add')->with('innerVariable', 'value2');
108 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerVariable');
109 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('innerVariable', 'value1');
110 $this->templateVariableContainer->expects($this->at(5))->method('remove')->with('innerVariable');
111 $traversableObject = new \ArrayObject(array('key1' => 'value1', 'key2' => 'value2'));
112 $this->viewHelper->render($traversableObject, 'innerVariable');
113 $this->viewHelper->render($traversableObject, 'innerVariable');
114 $this->viewHelper->render($traversableObject, 'innerVariable');
115 }
116 }
117
118 ?>