[TASK] Improve duplicate exception code check
[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\Extbase\Reflection\ReflectionService;
18 use TYPO3\CMS\Fluid\Core\Exception;
19 use TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper;
20 use TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture;
21 use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
23
24 /**
25 * Test case
26 */
27 class AbstractViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
28 {
29 /**
30 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
31 */
32 protected $mockReflectionService;
33
34 /**
35 * @var array
36 */
37 protected $fixtureMethodParameters = [
38 'param1' => [
39 'position' => 0,
40 'optional' => false,
41 'type' => 'integer',
42 'defaultValue' => null
43 ],
44 'param2' => [
45 'position' => 1,
46 'optional' => false,
47 'type' => 'array',
48 'array' => true,
49 'defaultValue' => null
50 ],
51 'param3' => [
52 'position' => 2,
53 'optional' => true,
54 'type' => 'string',
55 'array' => false,
56 'defaultValue' => 'default'
57 ],
58 ];
59
60 /**
61 * @var array
62 */
63 protected $fixtureMethodTags = [
64 'param' => [
65 'integer $param1 P1 Stuff',
66 'array $param2 P2 Stuff',
67 'string $param3 P3 Stuff'
68 ]
69 ];
70
71 protected function setUp()
72 {
73 $this->mockReflectionService = $this->createMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
74 }
75
76 /**
77 * @test
78 * @dataProvider getCallRenderMethodTestValues
79 * @param array $arguments
80 * @param bool $expectsException
81 */
82 public function registerRenderMethodArgumentsThrowsExceptionOnMissingType(array $arguments, $expectsException = false)
83 {
84 $reflectionService = $this->getMockBuilder(ReflectionService::class)
85 ->setMethods(['getMethodParameters', 'getMethodTagsValues'])
86 ->getMock();
87 $reflectionService->expects($this->once())->method('getMethodParameters')->willReturn(
88 [
89 'param1' => [
90 'position' => 0,
91 'byReference' => false,
92 'optional' => false,
93 'allowsNull' => true
94 ]
95 ]
96 );
97 $reflectionService->expects($this->once())->method('getMethodTagsValues')->willReturn([]);
98 $fixture = $this->getAccessibleMock(TestViewHelper::class, ['render']);
99 $fixture->injectReflectionService($reflectionService);
100 $this->expectException(Exception::class);
101 $this->callInaccessibleMethod($fixture, 'registerRenderMethodArguments');
102 }
103
104 /**
105 * @test
106 * @dataProvider getCallRenderMethodTestValues
107 * @param array $arguments
108 * @param bool $expectsException
109 */
110 public function callRenderMethodBehavesAsExpected(array $arguments, $expectsException = false)
111 {
112 $reflectionService = $this->getMockBuilder(ReflectionService::class)
113 ->setMethods(['getMethodParameters', 'getMethodTagsValues'])
114 ->getMock();
115 $reflectionService->expects($this->once())->method('getMethodParameters')->willReturn(
116 [
117 'param1' => [
118 'position' => 0,
119 'type' => 'integer',
120 'byReference' => false,
121 'array' => false,
122 'optional' => false,
123 'allowsNull' => true
124 ],
125 'param2' => [
126 'position' => 1,
127 'type' => 'array',
128 'byReference' => false,
129 'array' => true,
130 'optional' => false,
131 'allowsNull' => true
132 ],
133 'param3' => [
134 'position' => 2,
135 'type' => 'string',
136 'byReference' => false,
137 'array' => false,
138 'optional' => false,
139 'allowsNull' => true
140 ],
141 ]
142 );
143 $reflectionService->expects($this->once())->method('getMethodTagsValues')->willReturn(
144 []
145 );
146 $fixture = $this->getAccessibleMock(TestViewHelper::class, ['render']);
147 $namedArguments = array_combine(['param1', 'param2', 'param3'], $arguments);
148 $fixture->injectReflectionService($reflectionService);
149 $this->callInaccessibleMethod($fixture, 'registerRenderMethodArguments');
150 $fixture->setArguments($namedArguments);
151 if ($expectsException) {
152 $exception = new \TYPO3Fluid\Fluid\Core\ViewHelper\Exception('test', 1476108352);
153 $this->expectException(get_class($exception));
154 $fixture->expects($this->once())->method('render')->willThrowException($exception);
155 $this->assertEquals('test', $this->callInaccessibleMethod($fixture, 'callRenderMethod'));
156 } else {
157 $fixture->expects($this->once())
158 ->method('render')
159 ->with($arguments[0], $arguments[1], $arguments[2])
160 ->willReturn('okay');
161 $this->assertEquals('okay', $this->callInaccessibleMethod($fixture, 'callRenderMethod'));
162 }
163 }
164
165 /**
166 * @return array
167 */
168 public function getCallRenderMethodTestValues()
169 {
170 return [
171 [[3, ['bar'], 'baz'], false],
172 [[2, ['baz'], 'bar'], false],
173 [[3, ['bar'], 'baz'], true],
174 ];
175 }
176
177 /**
178 * @test
179 */
180 public function prepareArgumentsCallsInitializeArguments()
181 {
182 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'initializeArguments'], [], '', false);
183 $viewHelper->injectReflectionService($this->mockReflectionService);
184
185 $viewHelper->expects($this->once())->method('initializeArguments');
186
187 $viewHelper->prepareArguments();
188 }
189
190 /**
191 * @test
192 */
193 public function prepareArgumentsRegistersAnnotationBasedArgumentsWithDescriptionIfDebugModeIsEnabled()
194 {
195 $dataCacheMock = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class);
196 $dataCacheMock->expects($this->any())->method('has')->will($this->returnValue(true));
197 $dataCacheMock->expects($this->any())->method('get')->will($this->returnValue([]));
198
199 $viewHelper = new \TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper();
200
201 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper::class, 'render')->will($this->returnValue($this->fixtureMethodParameters));
202 $this->mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper::class, 'render')->will($this->returnValue($this->fixtureMethodTags));
203 $viewHelper->injectReflectionService($this->mockReflectionService);
204
205 $expected = [
206 'param1' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param1', 'integer', 'P1 Stuff', true, null, true),
207 'param2' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param2', 'array', 'P2 Stuff', true, null, true),
208 'param3' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param3', 'string', 'P3 Stuff', false, 'default', true),
209 ];
210
211 $this->assertEquals($expected, $viewHelper->prepareArguments(), 'Annotation based arguments were not registered.');
212 }
213
214 /**
215 * @test
216 */
217 public function validateArgumentsCallsPrepareArguments()
218 {
219 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
220 $viewHelper->injectReflectionService($this->mockReflectionService);
221
222 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([]));
223
224 $viewHelper->validateArguments();
225 }
226
227 /**
228 * @test
229 */
230 public function validateArgumentsAcceptsAllObjectsImplemtingArrayAccessAsAnArray()
231 {
232 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
233
234 $viewHelper->setArguments(['test' => new \ArrayObject]);
235 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(['test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'array', false, 'documentation')]));
236 $viewHelper->validateArguments();
237 }
238
239 /**
240 * @test
241 */
242 public function validateArgumentsCallsTheRightValidators()
243 {
244 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
245 $viewHelper->injectReflectionService($this->mockReflectionService);
246
247 $viewHelper->setArguments(['test' => 'Value of argument']);
248
249 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([
250 'test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'string', false, 'documentation')
251 ]));
252
253 $viewHelper->validateArguments();
254 }
255
256 /**
257 * @test
258 */
259 public function validateArgumentsCallsTheRightValidatorsAndThrowsExceptionIfValidationIsWrong()
260 {
261 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
262 $viewHelper->injectReflectionService($this->mockReflectionService);
263
264 $this->expectException(\InvalidArgumentException::class);
265 $this->expectExceptionCode(1256475113);
266
267 $viewHelper->setArguments(['test' => 'test']);
268
269 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([
270 'test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'stdClass', false, 'documentation')
271 ]));
272
273 $viewHelper->validateArguments();
274 }
275
276 /**
277 * @test
278 */
279 public function initializeArgumentsAndRenderCallsTheCorrectSequenceOfMethods()
280 {
281 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['validateArguments', 'initialize', 'callRenderMethod']);
282 $viewHelper->expects($this->at(0))->method('validateArguments');
283 $viewHelper->expects($this->at(1))->method('initialize');
284 $viewHelper->expects($this->at(2))->method('callRenderMethod')->will($this->returnValue('Output'));
285
286 $expectedOutput = 'Output';
287 $actualOutput = $viewHelper->initializeArgumentsAndRender(['argument1' => 'value1']);
288 $this->assertEquals($expectedOutput, $actualOutput);
289 }
290
291 /**
292 * @test
293 */
294 public function setRenderingContextShouldSetInnerVariables()
295 {
296 $templateVariableContainer = $this->createMock(StandardVariableProvider::class);
297 $viewHelperVariableContainer = $this->createMock(ViewHelperVariableContainer::class);
298 $controllerContext = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class)
299 ->setMethods(['getRequest'])
300 ->disableOriginalConstructor()
301 ->getMock();
302 $controllerContext->expects($this->atLeastOnce())->method('getRequest')->willReturn($this->createMock(Request::class));
303
304 $renderingContext = $this->getAccessibleMock(RenderingContextFixture::class, ['getControllerContext']);
305 $renderingContext->expects($this->any())->method('getControllerContext')->willReturn($controllerContext);
306 $renderingContext->setVariableProvider($templateVariableContainer);
307 $renderingContext->_set('viewHelperVariableContainer', $viewHelperVariableContainer);
308 $renderingContext->setControllerContext($controllerContext);
309
310 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
311
312 $viewHelper->setRenderingContext($renderingContext);
313
314 $this->assertSame($viewHelper->_get('templateVariableContainer'), $templateVariableContainer);
315 $this->assertSame($viewHelper->_get('viewHelperVariableContainer'), $viewHelperVariableContainer);
316 $this->assertSame($viewHelper->_get('controllerContext'), $controllerContext);
317 }
318 }