[FEATURE] Switch View Helper
[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 require_once(__DIR__ . '/ViewHelperBaseTestcase.php');
15
16 /**
17 * Testcase for CaseViewHelper
18 */
19 class CaseViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase {
20
21 /**
22 * @var \TYPO3\CMS\Fluid\ViewHelpers\CaseViewHelper
23 */
24 protected $viewHelper;
25
26 public function setUp() {
27 parent::setUp();
28 $this->viewHelper = $this->getMock('TYPO3\CMS\Fluid\ViewHelpers\CaseViewHelper', array('renderChildren'));
29 $this->injectDependenciesIntoViewHelper($this->viewHelper);
30 $this->viewHelper->initializeArguments();
31 }
32
33 /**
34 * @test
35 * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
36 */
37 public function renderThrowsExceptionIfSwitchExpressionIsNotSetInViewHelperVariableContainer() {
38 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue(FALSE));
39 $this->viewHelper->render('foo');
40 }
41
42 /**
43 * @test
44 */
45 public function renderReturnsChildNodesIfTheSpecifiedValueIsEqualToTheSwitchExpression() {
46 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue(TRUE));
47 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue('someValue'));
48
49 $renderedChildNodes = 'ChildNodes';
50 $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue($renderedChildNodes));
51
52 $this->assertSame($renderedChildNodes, $this->viewHelper->render('someValue'));
53 }
54
55 /**
56 * @test
57 */
58 public function renderSetsBreakStateInViewHelperVariableContainerIfTheSpecifiedValueIsEqualToTheSwitchExpression() {
59 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue(TRUE));
60 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue('someValue'));
61
62 $this->viewHelperVariableContainer->expects($this->once())->method('addOrUpdate')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break', TRUE);
63
64 $this->viewHelper->render('someValue');
65 }
66
67 /**
68 * @test
69 */
70 public function renderWeaklyComparesSpecifiedValueWithSwitchExpression() {
71 $numericValue = 123;
72 $stringValue = '123';
73
74 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue(TRUE));
75 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue($numericValue));
76
77 $this->viewHelperVariableContainer->expects($this->once())->method('addOrUpdate')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break', TRUE);
78
79 $this->viewHelper->render($stringValue);
80 }
81
82
83 /**
84 * @test
85 */
86 public function renderReturnsAnEmptyStringIfTheSpecifiedValueIsNotEqualToTheSwitchExpression() {
87 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('exists')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue(TRUE));
88 $this->viewHelperVariableContainer->expects($this->atLeastOnce())->method('get')->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')->will($this->returnValue('someValue'));
89 $this->assertSame('', $this->viewHelper->render('someOtherValue'));
90 }
91
92 }
93
94 ?>