[BUGFIX] Incomplete mock in AbstractViewHelperTest
[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\TestingFramework\Core\Unit\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 $this->mockReflectionService->method('getMethodParameters')->willReturn([]);
184 $viewHelper->injectReflectionService($this->mockReflectionService);
185
186 $viewHelper->expects($this->once())->method('initializeArguments');
187
188 $viewHelper->prepareArguments();
189 }
190
191 /**
192 * @test
193 */
194 public function prepareArgumentsRegistersAnnotationBasedArgumentsWithDescriptionIfDebugModeIsEnabled()
195 {
196 $dataCacheMock = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class);
197 $dataCacheMock->expects($this->any())->method('has')->will($this->returnValue(true));
198 $dataCacheMock->expects($this->any())->method('get')->will($this->returnValue([]));
199
200 $viewHelper = new \TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper();
201
202 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper::class, 'render')->will($this->returnValue($this->fixtureMethodParameters));
203 $this->mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper::class, 'render')->will($this->returnValue($this->fixtureMethodTags));
204 $viewHelper->injectReflectionService($this->mockReflectionService);
205
206 $expected = [
207 'param1' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param1', 'integer', 'P1 Stuff', true, null, true),
208 'param2' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param2', 'array', 'P2 Stuff', true, null, true),
209 'param3' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param3', 'string', 'P3 Stuff', false, 'default', true),
210 ];
211
212 $this->assertEquals($expected, $viewHelper->prepareArguments(), 'Annotation based arguments were not registered.');
213 }
214
215 /**
216 * @test
217 */
218 public function validateArgumentsCallsPrepareArguments()
219 {
220 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
221 $viewHelper->injectReflectionService($this->mockReflectionService);
222
223 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([]));
224
225 $viewHelper->validateArguments();
226 }
227
228 /**
229 * @test
230 */
231 public function validateArgumentsAcceptsAllObjectsImplemtingArrayAccessAsAnArray()
232 {
233 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
234
235 $viewHelper->setArguments(['test' => new \ArrayObject]);
236 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(['test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'array', false, 'documentation')]));
237 $viewHelper->validateArguments();
238 }
239
240 /**
241 * @test
242 */
243 public function validateArgumentsCallsTheRightValidators()
244 {
245 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
246 $viewHelper->injectReflectionService($this->mockReflectionService);
247
248 $viewHelper->setArguments(['test' => 'Value of argument']);
249
250 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([
251 'test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'string', false, 'documentation')
252 ]));
253
254 $viewHelper->validateArguments();
255 }
256
257 /**
258 * @test
259 */
260 public function validateArgumentsCallsTheRightValidatorsAndThrowsExceptionIfValidationIsWrong()
261 {
262 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
263 $viewHelper->injectReflectionService($this->mockReflectionService);
264
265 $this->expectException(\InvalidArgumentException::class);
266 $this->expectExceptionCode(1256475113);
267
268 $viewHelper->setArguments(['test' => 'test']);
269
270 $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([
271 'test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'stdClass', false, 'documentation')
272 ]));
273
274 $viewHelper->validateArguments();
275 }
276
277 /**
278 * @test
279 */
280 public function initializeArgumentsAndRenderCallsTheCorrectSequenceOfMethods()
281 {
282 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['validateArguments', 'initialize', 'callRenderMethod']);
283 $viewHelper->expects($this->at(0))->method('validateArguments');
284 $viewHelper->expects($this->at(1))->method('initialize');
285 $viewHelper->expects($this->at(2))->method('callRenderMethod')->will($this->returnValue('Output'));
286
287 $expectedOutput = 'Output';
288 $actualOutput = $viewHelper->initializeArgumentsAndRender(['argument1' => 'value1']);
289 $this->assertEquals($expectedOutput, $actualOutput);
290 }
291
292 /**
293 * @test
294 */
295 public function setRenderingContextShouldSetInnerVariables()
296 {
297 $templateVariableContainer = $this->createMock(StandardVariableProvider::class);
298 $viewHelperVariableContainer = $this->createMock(ViewHelperVariableContainer::class);
299 $controllerContext = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class)
300 ->setMethods(['getRequest'])
301 ->disableOriginalConstructor()
302 ->getMock();
303 $controllerContext->expects($this->atLeastOnce())->method('getRequest')->willReturn($this->createMock(Request::class));
304
305 $renderingContext = $this->getAccessibleMock(RenderingContextFixture::class, ['getControllerContext']);
306 $renderingContext->expects($this->any())->method('getControllerContext')->willReturn($controllerContext);
307 $renderingContext->setVariableProvider($templateVariableContainer);
308 $renderingContext->_set('viewHelperVariableContainer', $viewHelperVariableContainer);
309 $renderingContext->setControllerContext($controllerContext);
310
311 $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
312
313 $viewHelper->setRenderingContext($renderingContext);
314
315 $this->assertSame($viewHelper->_get('templateVariableContainer'), $templateVariableContainer);
316 $this->assertSame($viewHelper->_get('viewHelperVariableContainer'), $viewHelperVariableContainer);
317 $this->assertSame($viewHelper->_get('controllerContext'), $controllerContext);
318 }
319 }