[TASK] Make Condition ViewHelpers fully static compileable
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / Core / ViewHelper / AbstractConditionViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\Core\ViewHelper;
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 Condition ViewHelper
16 */
17 class AbstractConditionViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase {
18
19 /**
20 * @var \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper
21 */
22 protected $viewHelper;
23
24 /**
25 * @var \TYPO3\CMS\Fluid\Core\ViewHelper\Arguments
26 */
27 protected $mockArguments;
28
29 protected function setUp() {
30 parent::setUp();
31 $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper::class, array('getRenderingContext', 'renderChildren', 'hasArgument'));
32 $this->viewHelper->expects($this->any())->method('getRenderingContext')->will($this->returnValue($this->renderingContext));
33 $this->injectDependenciesIntoViewHelper($this->viewHelper);
34 }
35
36 /**
37 * @test
38 */
39 public function renderThenChildReturnsAllChildrenIfNoThenViewHelperChildExists() {
40 $this->viewHelper->expects($this->any())->method('renderChildren')->will($this->returnValue('foo'));
41
42 $actualResult = $this->viewHelper->_call('renderThenChild');
43 $this->assertEquals('foo', $actualResult);
44 }
45
46 /**
47 * @test
48 */
49 public function renderThenChildReturnsThenViewHelperChildIfConditionIsTrueAndThenViewHelperChildExists() {
50 $mockThenViewHelperNode = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode::class, array('getViewHelperClassName', 'evaluate'), array(), '', FALSE);
51 $mockThenViewHelperNode->expects($this->at(0))->method('getViewHelperClassName')->will($this->returnValue(\TYPO3\CMS\Fluid\ViewHelpers\ThenViewHelper::class));
52 $mockThenViewHelperNode->expects($this->at(1))->method('evaluate')->with($this->renderingContext)->will($this->returnValue('ThenViewHelperResults'));
53
54 $this->viewHelper->setChildNodes(array($mockThenViewHelperNode));
55 $actualResult = $this->viewHelper->_call('renderThenChild');
56 $this->assertEquals('ThenViewHelperResults', $actualResult);
57 }
58
59 /**
60 * @test
61 */
62 public function renderElseChildReturnsEmptyStringIfConditionIsFalseAndNoElseViewHelperChildExists() {
63 $actualResult = $this->viewHelper->_call('renderElseChild');
64 $this->assertEquals('', $actualResult);
65 }
66
67 /**
68 * @test
69 */
70 public function renderElseChildRendersElseViewHelperChildIfConditionIsFalseAndNoThenViewHelperChildExists() {
71 $mockElseViewHelperNode = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode::class, array('getViewHelperClassName', 'evaluate', 'setRenderingContext'), array(), '', FALSE);
72 $mockElseViewHelperNode->expects($this->at(0))->method('getViewHelperClassName')->will($this->returnValue(\TYPO3\CMS\Fluid\ViewHelpers\ElseViewHelper::class));
73 $mockElseViewHelperNode->expects($this->at(1))->method('evaluate')->with($this->renderingContext)->will($this->returnValue('ElseViewHelperResults'));
74
75 $this->viewHelper->setChildNodes(array($mockElseViewHelperNode));
76 $actualResult = $this->viewHelper->_call('renderElseChild');
77 $this->assertEquals('ElseViewHelperResults', $actualResult);
78 }
79
80 /**
81 * @test
82 */
83 public function renderThenChildReturnsValueOfThenArgumentIfConditionIsTrue() {
84 $this->arguments['then'] = 'ThenArgument';
85 $this->injectDependenciesIntoViewHelper($this->viewHelper);
86
87 $actualResult = $this->viewHelper->_call('renderThenChild');
88 $this->assertEquals('ThenArgument', $actualResult);
89 }
90
91 /**
92 * @test
93 */
94 public function renderThenChildReturnsEmptyStringIfChildNodesOnlyContainElseViewHelper() {
95 $mockElseViewHelperNode = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode::class, array('getViewHelperClassName', 'evaluate'), array(), '', FALSE);
96 $mockElseViewHelperNode->expects($this->any())->method('getViewHelperClassName')->will($this->returnValue(\TYPO3\CMS\Fluid\ViewHelpers\ElseViewHelper::class));
97 $this->viewHelper->setChildNodes(array($mockElseViewHelperNode));
98 $this->viewHelper->expects($this->never())->method('renderChildren')->will($this->returnValue('Child nodes'));
99
100 $actualResult = $this->viewHelper->_call('renderThenChild');
101 $this->assertEquals('', $actualResult);
102 }
103
104 /**
105 * @test
106 */
107 public function thenArgumentHasPriorityOverChildNodesIfConditionIsTrue() {
108 $mockThenViewHelperNode = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode::class, array('getViewHelperClassName', 'evaluate', 'setRenderingContext'), array(), '', FALSE);
109 $mockThenViewHelperNode->expects($this->never())->method('evaluate');
110
111 $this->viewHelper->setChildNodes(array($mockThenViewHelperNode));
112
113 $this->arguments['then'] = 'ThenArgument';
114
115 $this->injectDependenciesIntoViewHelper($this->viewHelper);
116
117 $actualResult = $this->viewHelper->_call('renderThenChild');
118 $this->assertEquals('ThenArgument', $actualResult);
119 }
120
121 /**
122 * @test
123 */
124 public function renderReturnsValueOfElseArgumentIfConditionIsFalse() {
125 $this->arguments['else'] = 'ElseArgument';
126 $this->injectDependenciesIntoViewHelper($this->viewHelper);
127
128 $actualResult = $this->viewHelper->_call('renderElseChild');
129 $this->assertEquals('ElseArgument', $actualResult);
130 }
131
132 /**
133 * @test
134 */
135 public function elseArgumentHasPriorityOverChildNodesIfConditionIsFalse() {
136 $mockElseViewHelperNode = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode::class, array('getViewHelperClassName', 'evaluate', 'setRenderingContext'), array(), '', FALSE);
137 $mockElseViewHelperNode->expects($this->any())->method('getViewHelperClassName')->will($this->returnValue(\TYPO3\CMS\Fluid\ViewHelpers\ElseViewHelper::class));
138 $mockElseViewHelperNode->expects($this->never())->method('evaluate');
139
140 $this->viewHelper->setChildNodes(array($mockElseViewHelperNode));
141
142 $this->arguments['else'] = 'ElseArgument';
143 $this->injectDependenciesIntoViewHelper($this->viewHelper);
144
145 $actualResult = $this->viewHelper->_call('renderElseChild');
146 $this->assertEquals('ElseArgument', $actualResult);
147 }
148
149 }