[!!!][TASK] Use native trigger_error and ErrorHandler for deprecations
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / Core / ViewHelper / AbstractViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\Core\ViewHelper;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
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 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Extbase\Mvc\Request;
17 use TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture;
18 use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
20
21 /**
22 * Test case
23 */
24 class AbstractViewHelperTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
26 /**
27 * @var array
28 */
29 protected $fixtureMethodParameters = [
30 'param1' => [
31 'position' => 0,
32 'optional' => false,
33 'type' => 'integer',
34 'defaultValue' => null
35 ],
36 'param2' => [
37 'position' => 1,
38 'optional' => false,
39 'type' => 'array',
40 'array' => true,
41 'defaultValue' => null
42 ],
43 'param3' => [
44 'position' => 2,
45 'optional' => true,
46 'type' => 'string',
47 'array' => false,
48 'defaultValue' => 'default'
49 ],
50 ];
51
52 /**
53 * @var array
54 */
55 protected $fixtureMethodTags = [
56 'param' => [
57 'integer $param1 P1 Stuff',
58 'array $param2 P2 Stuff',
59 'string $param3 P3 Stuff'
60 ]
61 ];
62
63 protected function setUp()
64 {
65 $this->mockReflectionService = $this->createMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
66 }
67
68 /**
69 * @test
70 */
71 public function prepareArgumentsCallsInitializeArguments()
72 {
73 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'initializeArguments'], [], '', false);
74
75 $viewHelper->expects($this->once())->method('initializeArguments');
76
77 $viewHelper->prepareArguments();
78 }
79
80 /**
81 * @test
82 */
83 public function validateArgumentsCallsPrepareArguments()
84 {
85 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
86
87 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([]));
88
89 $viewHelper->validateArguments();
90 }
91
92 /**
93 * @test
94 */
95 public function validateArgumentsAcceptsAllObjectsImplemtingArrayAccessAsAnArray()
96 {
97 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
98
99 $viewHelper->setArguments(['test' => new \ArrayObject]);
100 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(['test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'array', false, 'documentation')]));
101 $viewHelper->validateArguments();
102 }
103
104 /**
105 * @test
106 */
107 public function validateArgumentsCallsTheRightValidators()
108 {
109 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
110
111 $viewHelper->setArguments(['test' => 'Value of argument']);
112
113 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([
114 'test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'string', false, 'documentation')
115 ]));
116
117 $viewHelper->validateArguments();
118 }
119
120 /**
121 * @test
122 */
123 public function validateArgumentsCallsTheRightValidatorsAndThrowsExceptionIfValidationIsWrong()
124 {
125 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
126
127 $this->expectException(\InvalidArgumentException::class);
128 $this->expectExceptionCode(1256475113);
129
130 $viewHelper->setArguments(['test' => 'test']);
131
132 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([
133 'test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'stdClass', false, 'documentation')
134 ]));
135
136 $viewHelper->validateArguments();
137 }
138
139 /**
140 * @test
141 */
142 public function initializeArgumentsAndRenderCallsTheCorrectSequenceOfMethods()
143 {
144 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['validateArguments', 'initialize', 'callRenderMethod']);
145 $viewHelper->expects($this->at(0))->method('validateArguments');
146 $viewHelper->expects($this->at(1))->method('initialize');
147 $viewHelper->expects($this->at(2))->method('callRenderMethod')->will($this->returnValue('Output'));
148
149 $expectedOutput = 'Output';
150 $actualOutput = $viewHelper->initializeArgumentsAndRender(['argument1' => 'value1']);
151 $this->assertEquals($expectedOutput, $actualOutput);
152 }
153
154 /**
155 * @test
156 */
157 public function setRenderingContextShouldSetInnerVariables()
158 {
159 $templateVariableContainer = $this->createMock(StandardVariableProvider::class);
160 $viewHelperVariableContainer = $this->createMock(ViewHelperVariableContainer::class);
161 $controllerContext = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class)
162 ->setMethods(['getRequest'])
163 ->disableOriginalConstructor()
164 ->getMock();
165 $controllerContext->expects($this->atLeastOnce())->method('getRequest')->willReturn($this->createMock(Request::class));
166
167 $renderingContext = $this->getAccessibleMock(RenderingContextFixture::class, ['getControllerContext']);
168 $renderingContext->expects($this->any())->method('getControllerContext')->willReturn($controllerContext);
169 $renderingContext->setVariableProvider($templateVariableContainer);
170 $renderingContext->_set('viewHelperVariableContainer', $viewHelperVariableContainer);
171 $renderingContext->setControllerContext($controllerContext);
172
173 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
174
175 $viewHelper->setRenderingContext($renderingContext);
176
177 $this->assertSame($viewHelper->_get('templateVariableContainer'), $templateVariableContainer);
178 $this->assertSame($viewHelper->_get('viewHelperVariableContainer'), $viewHelperVariableContainer);
179 $this->assertSame($viewHelper->_get('controllerContext'), $controllerContext);
180 }
181 }