[CLEANUP] Rework/simplify copyright header and remove @package
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / CaseViewHelperTest.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 General Public License, either version 3 of the *
9 * License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * Testcase for CaseViewHelper
16 */
17 class CaseViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase {
18
19 /**
20 * @var \TYPO3\CMS\Fluid\ViewHelpers\CaseViewHelper
21 */
22 protected $viewHelper;
23
24 public function setUp() {
25 parent::setUp();
26 $this->viewHelper = $this->getMock('TYPO3\CMS\Fluid\ViewHelpers\CaseViewHelper', array('renderChildren'));
27 $this->injectDependenciesIntoViewHelper($this->viewHelper);
28 $this->viewHelper->initializeArguments();
29 }
30
31 /**
32 * @test
33 * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
34 */
35 public function renderThrowsExceptionIfSwitchExpressionIsNotSetInViewHelperVariableContainer() {
36 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue(FALSE));
37 $this->viewHelper->render('foo');
38 }
39
40 /**
41 * @test
42 */
43 public function renderReturnsChildNodesIfTheSpecifiedValueIsEqualToTheSwitchExpression() {
44 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue(TRUE));
45 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue('someValue'));
46
47 $renderedChildNodes = 'ChildNodes';
48 $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue($renderedChildNodes));
49
50 $this->assertSame($renderedChildNodes, $this->viewHelper->render('someValue'));
51 }
52
53 /**
54 * @test
55 */
56 public function renderSetsBreakStateInViewHelperVariableContainerIfTheSpecifiedValueIsEqualToTheSwitchExpression() {
57 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue(TRUE));
58 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue('someValue'));
59
60 $this->viewHelperVariableContainer->expects($this->once())->method('addOrUpdate')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break', TRUE);
61
62 $this->viewHelper->render('someValue');
63 }
64
65 /**
66 * @test
67 */
68 public function renderWeaklyComparesSpecifiedValueWithSwitchExpression() {
69 $numericValue = 123;
70 $stringValue = '123';
71
72 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue(TRUE));
73 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue($numericValue));
74
75 $this->viewHelperVariableContainer->expects($this->once())->method('addOrUpdate')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break', TRUE);
76
77 $this->viewHelper->render($stringValue);
78 }
79
80
81 /**
82 * @test
83 */
84 public function renderReturnsAnEmptyStringIfTheSpecifiedValueIsNotEqualToTheSwitchExpression() {
85 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue(TRUE));
86 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue('someValue'));
87 $this->assertSame('', $this->viewHelper->render('someOtherValue'));
88 }
89
90 /**
91 * @test
92 */
93 public function renderReturnsChildNodesIfDefaultIsTrue() {
94 $this->viewHelperVariableContainer->expects(
95 $this->atLeastOnce())
96 ->method('exists')
97 ->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')
98 ->will($this->returnValue(TRUE)
99 );
100 $this->viewHelperVariableContainer->expects(
101 $this->atLeastOnce())->method('get')
102 ->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')
103 ->will($this->returnValue(NULL)
104 );
105
106 $renderedChildNodes = 'ChildNodes';
107 $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue($renderedChildNodes));
108
109 $this->assertSame($renderedChildNodes, $this->viewHelper->render(NULL, TRUE));
110 }
111
112 /**
113 * @test
114 * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
115 */
116 public function renderThrowsExceptionIfNeighterValueNorDefaultAreGiven() {
117 $this->viewHelperVariableContainer->expects(
118 $this->atLeastOnce())
119 ->method('exists')
120 ->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')
121 ->will($this->returnValue(TRUE)
122 );
123 $this->viewHelperVariableContainer->expects($this->never())->method('get');
124
125 $this->viewHelper->render(NULL, FALSE);
126 }
127
128 /**
129 * @test
130 */
131 public function renderReturnsChildNodesEvenIfValueIsFalseButDefaultIsTrue() {
132 $this->viewHelperVariableContainer->expects(
133 $this->atLeastOnce())
134 ->method('exists')
135 ->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')
136 ->will($this->returnValue(TRUE)
137 );
138 $this->viewHelperVariableContainer->expects(
139 $this->atLeastOnce())->method('get')
140 ->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')
141 ->will($this->returnValue('someValue')
142 );
143
144 $renderedChildNodes = 'ChildNodes';
145 $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue($renderedChildNodes));
146
147 $this->assertSame($renderedChildNodes, $this->viewHelper->render('someOtherValue', TRUE));
148 }
149
150 }