TYPO3 CMS  TYPO3_7-6
AbstractViewHelperTest.php
Go to the documentation of this file.
1 <?php
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 
18 {
23 
28  'param1' => [
29  'position' => 0,
30  'optional' => false,
31  'type' => 'integer',
32  'defaultValue' => null
33  ],
34  'param2' => [
35  'position' => 1,
36  'optional' => false,
37  'type' => 'array',
38  'array' => true,
39  'defaultValue' => null
40  ],
41  'param3' => [
42  'position' => 2,
43  'optional' => true,
44  'type' => 'string',
45  'array' => false,
46  'defaultValue' => 'default'
47  ],
48  ];
49 
53  protected $fixtureMethodTags = [
54  'param' => [
55  'integer $param1 P1 Stuff',
56  'array $param2 P2 Stuff',
57  'string $param3 P3 Stuff'
58  ]
59  ];
60 
61  protected function setUp()
62  {
63  $this->mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class, [], [], '', false);
64  }
65 
69  public function argumentsCanBeRegistered()
70  {
71  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render'], [], '', false);
72 
73  $this->mockReflectionService->method('getMethodParameters')->willReturn([]);
74  $viewHelper->injectReflectionService($this->mockReflectionService);
75 
76  $name = 'This is a name';
77  $description = 'Example desc';
78  $type = 'string';
79  $isRequired = true;
80  $expected = new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition($name, $type, $description, $isRequired);
81 
82  $viewHelper->_call('registerArgument', $name, $type, $description, $isRequired);
83  $this->assertEquals([$name => $expected], $viewHelper->prepareArguments(), 'Argument definitions not returned correctly.');
84  }
85 
91  {
92  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render'], [], '', false);
93 
94  $name = 'shortName';
95  $description = 'Example desc';
96  $type = 'string';
97  $isRequired = true;
98 
99  $viewHelper->_call('registerArgument', $name, $type, $description, $isRequired);
100  $viewHelper->_call('registerArgument', $name, 'integer', $description, $isRequired);
101  }
102 
107  {
108  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render'], [], '', false);
109  $this->mockReflectionService->method('getMethodParameters')->willReturn([]);
110  $viewHelper->injectReflectionService($this->mockReflectionService);
111 
112  $name = 'argumentName';
113  $description = 'argument description';
114  $overriddenDescription = 'overwritten argument description';
115  $type = 'string';
116  $overriddenType = 'integer';
117  $isRequired = true;
118  $expected = new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition($name, $overriddenType, $overriddenDescription, $isRequired);
119 
120  $viewHelper->_call('registerArgument', $name, $type, $description, $isRequired);
121  $viewHelper->_call('overrideArgument', $name, $overriddenType, $overriddenDescription, $isRequired);
122  $this->assertEquals($viewHelper->prepareArguments(), [$name => $expected], 'Argument definitions not returned correctly. The original ArgumentDefinition could not be overridden.');
123  }
124 
130  {
131  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render'], [], '', false);
132  $viewHelper->injectReflectionService($this->mockReflectionService);
133 
134  $viewHelper->_call('overrideArgument', 'argumentName', 'string', 'description', true);
135  }
136 
141  {
142  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'initializeArguments'], [], '', false);
143  $this->mockReflectionService->method('getMethodParameters')->willReturn([]);
144  $viewHelper->injectReflectionService($this->mockReflectionService);
145 
146  $viewHelper->expects($this->once())->method('initializeArguments');
147 
148  $viewHelper->prepareArguments();
149  }
150 
155  {
157 
158  $dataCacheMock = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class, [], [], '', false);
159  $dataCacheMock->expects($this->any())->method('has')->will($this->returnValue(true));
160  $dataCacheMock->expects($this->any())->method('get')->will($this->returnValue([]));
161 
162  $viewHelper = new \TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper();
163 
164  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper::class, 'render')->will($this->returnValue($this->fixtureMethodParameters));
165  $this->mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper::class, 'render')->will($this->returnValue($this->fixtureMethodTags));
166  $viewHelper->injectReflectionService($this->mockReflectionService);
167 
168  $expected = [
169  'param1' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param1', 'integer', 'P1 Stuff', true, null, true),
170  'param2' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param2', 'array', 'P2 Stuff', true, null, true),
171  'param3' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param3', 'string', 'P3 Stuff', false, 'default', true),
172  ];
173 
174  $this->assertEquals($expected, $viewHelper->prepareArguments(), 'Annotation based arguments were not registered.');
175 
177  }
178 
183  {
185 
186  $dataCacheMock = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class, [], [], '', false);
187  $dataCacheMock->expects($this->any())->method('has')->will($this->returnValue(true));
188  $dataCacheMock->expects($this->any())->method('get')->will($this->returnValue([]));
189 
190  $viewHelper = new \TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper2();
191 
192  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper2::class, 'render')->will($this->returnValue($this->fixtureMethodParameters));
193  $this->mockReflectionService->expects($this->never())->method('getMethodTagsValues');
194  $viewHelper->injectReflectionService($this->mockReflectionService);
195 
196  $expected = [
197  'param1' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param1', 'integer', '', true, null, true),
198  'param2' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param2', 'array', '', true, null, true),
199  'param3' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param3', 'string', '', false, 'default', true)
200  ];
201 
202  $this->assertEquals($expected, $viewHelper->prepareArguments(), 'Annotation based arguments were not registered.');
203  }
204 
209  {
210  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
211  $viewHelper->injectReflectionService($this->mockReflectionService);
212 
213  $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([]));
214 
215  $viewHelper->validateArguments();
216  }
217 
222  {
223  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
224 
225  $viewHelper->setArguments(['test' => new \ArrayObject]);
226  $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(['test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'array', false, 'documentation')]));
227  $viewHelper->validateArguments();
228  }
229 
234  {
235  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
236  $viewHelper->injectReflectionService($this->mockReflectionService);
237 
238  $viewHelper->setArguments(['test' => 'Value of argument']);
239 
240  $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([
241  'test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'string', false, 'documentation')
242  ]));
243 
244  $viewHelper->validateArguments();
245  }
246 
252  {
253  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
254  $viewHelper->injectReflectionService($this->mockReflectionService);
255 
256  $viewHelper->setArguments(['test' => 'test']);
257 
258  $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue([
259  'test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'stdClass', false, 'documentation')
260  ]));
261 
262  $viewHelper->validateArguments();
263  }
264 
269  {
270  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['validateArguments', 'initialize', 'callRenderMethod']);
271  $viewHelper->expects($this->at(0))->method('validateArguments');
272  $viewHelper->expects($this->at(1))->method('initialize');
273  $viewHelper->expects($this->at(2))->method('callRenderMethod')->will($this->returnValue('Output'));
274 
275  $expectedOutput = 'Output';
276  $actualOutput = $viewHelper->initializeArgumentsAndRender(['argument1' => 'value1']);
277  $this->assertEquals($expectedOutput, $actualOutput);
278  }
279 
284  {
285  $templateVariableContainer = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer::class);
286  $viewHelperVariableContainer = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperVariableContainer::class);
287  $controllerContext = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class, [], [], '', false);
288 
289  $renderingContext = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\Rendering\RenderingContext::class, ['dummy']);
290  $renderingContext->injectTemplateVariableContainer($templateVariableContainer);
291  $renderingContext->_set('viewHelperVariableContainer', $viewHelperVariableContainer);
292  $renderingContext->setControllerContext($controllerContext);
293 
294  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, ['render', 'prepareArguments'], [], '', false);
295 
296  $viewHelper->setRenderingContext($renderingContext);
297 
298  $this->assertSame($viewHelper->_get('templateVariableContainer'), $templateVariableContainer);
299  $this->assertSame($viewHelper->_get('viewHelperVariableContainer'), $viewHelperVariableContainer);
300  $this->assertSame($viewHelper->_get('controllerContext'), $controllerContext);
301  }
302 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)