TYPO3  7.6
AbstractViewHelperTest.php
Go to the documentation of this file.
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 
17 class AbstractViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
18 {
23 
27  protected $fixtureMethodParameters = array(
28  'param1' => array(
29  'position' => 0,
30  'optional' => false,
31  'type' => 'integer',
32  'defaultValue' => null
33  ),
34  'param2' => array(
35  'position' => 1,
36  'optional' => false,
37  'type' => 'array',
38  'array' => true,
39  'defaultValue' => null
40  ),
41  'param3' => array(
42  'position' => 2,
43  'optional' => true,
44  'type' => 'string',
45  'array' => false,
46  'defaultValue' => 'default'
47  ),
48  );
49 
53  protected $fixtureMethodTags = array(
54  'param' => array(
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, array(), array(), '', false);
64  }
65 
69  public function argumentsCanBeRegistered()
70  {
71  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('render'), array(), '', false);
72  $viewHelper->injectReflectionService($this->mockReflectionService);
73 
74  $name = 'This is a name';
75  $description = 'Example desc';
76  $type = 'string';
77  $isRequired = true;
78  $expected = new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition($name, $type, $description, $isRequired);
79 
80  $viewHelper->_call('registerArgument', $name, $type, $description, $isRequired);
81  $this->assertEquals(array($name => $expected), $viewHelper->prepareArguments(), 'Argument definitions not returned correctly.');
82  }
83 
89  {
90  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('render'), array(), '', false);
91 
92  $name = 'shortName';
93  $description = 'Example desc';
94  $type = 'string';
95  $isRequired = true;
96 
97  $viewHelper->_call('registerArgument', $name, $type, $description, $isRequired);
98  $viewHelper->_call('registerArgument', $name, 'integer', $description, $isRequired);
99  }
100 
105  {
106  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('render'), array(), '', false);
107  $viewHelper->injectReflectionService($this->mockReflectionService);
108 
109  $name = 'argumentName';
110  $description = 'argument description';
111  $overriddenDescription = 'overwritten argument description';
112  $type = 'string';
113  $overriddenType = 'integer';
114  $isRequired = true;
115  $expected = new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition($name, $overriddenType, $overriddenDescription, $isRequired);
116 
117  $viewHelper->_call('registerArgument', $name, $type, $description, $isRequired);
118  $viewHelper->_call('overrideArgument', $name, $overriddenType, $overriddenDescription, $isRequired);
119  $this->assertEquals($viewHelper->prepareArguments(), array($name => $expected), 'Argument definitions not returned correctly. The original ArgumentDefinition could not be overridden.');
120  }
121 
127  {
128  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('render'), array(), '', false);
129  $viewHelper->injectReflectionService($this->mockReflectionService);
130 
131  $viewHelper->_call('overrideArgument', 'argumentName', 'string', 'description', true);
132  }
133 
138  {
139  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('render', 'initializeArguments'), array(), '', false);
140  $viewHelper->injectReflectionService($this->mockReflectionService);
141 
142  $viewHelper->expects($this->once())->method('initializeArguments');
143 
144  $viewHelper->prepareArguments();
145  }
146 
151  {
152  \TYPO3\CMS\Fluid\Fluid::$debugMode = true;
153 
154  $dataCacheMock = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class, array(), array(), '', false);
155  $dataCacheMock->expects($this->any())->method('has')->will($this->returnValue(true));
156  $dataCacheMock->expects($this->any())->method('get')->will($this->returnValue(array()));
157 
158  $viewHelper = new \TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper();
159 
160  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper::class, 'render')->will($this->returnValue($this->fixtureMethodParameters));
161  $this->mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper::class, 'render')->will($this->returnValue($this->fixtureMethodTags));
162  $viewHelper->injectReflectionService($this->mockReflectionService);
163 
164  $expected = array(
165  'param1' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param1', 'integer', 'P1 Stuff', true, null, true),
166  'param2' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param2', 'array', 'P2 Stuff', true, null, true),
167  'param3' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param3', 'string', 'P3 Stuff', false, 'default', true),
168  );
169 
170  $this->assertEquals($expected, $viewHelper->prepareArguments(), 'Annotation based arguments were not registered.');
171 
172  \TYPO3\CMS\Fluid\Fluid::$debugMode = false;
173  }
174 
179  {
180  \TYPO3\CMS\Fluid\Fluid::$debugMode = false;
181 
182  $dataCacheMock = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class, array(), array(), '', false);
183  $dataCacheMock->expects($this->any())->method('has')->will($this->returnValue(true));
184  $dataCacheMock->expects($this->any())->method('get')->will($this->returnValue(array()));
185 
186  $viewHelper = new \TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper2();
187 
188  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with(\TYPO3\CMS\Fluid\Tests\Unit\Core\Fixtures\TestViewHelper2::class, 'render')->will($this->returnValue($this->fixtureMethodParameters));
189  $this->mockReflectionService->expects($this->never())->method('getMethodTagsValues');
190  $viewHelper->injectReflectionService($this->mockReflectionService);
191 
192  $expected = array(
193  'param1' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param1', 'integer', '', true, null, true),
194  'param2' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param2', 'array', '', true, null, true),
195  'param3' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('param3', 'string', '', false, 'default', true)
196  );
197 
198  $this->assertEquals($expected, $viewHelper->prepareArguments(), 'Annotation based arguments were not registered.');
199  }
200 
205  {
206  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('render', 'prepareArguments'), array(), '', false);
207  $viewHelper->injectReflectionService($this->mockReflectionService);
208 
209  $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(array()));
210 
211  $viewHelper->validateArguments();
212  }
213 
218  {
219  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('render', 'prepareArguments'), array(), '', false);
220 
221  $viewHelper->setArguments(array('test' => new \ArrayObject));
222  $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(array('test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'array', false, 'documentation'))));
223  $viewHelper->validateArguments();
224  }
225 
230  {
231  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('render', 'prepareArguments'), array(), '', false);
232  $viewHelper->injectReflectionService($this->mockReflectionService);
233 
234  $viewHelper->setArguments(array('test' => 'Value of argument'));
235 
236  $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(array(
237  'test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'string', false, 'documentation')
238  )));
239 
240  $viewHelper->validateArguments();
241  }
242 
248  {
249  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('render', 'prepareArguments'), array(), '', false);
250  $viewHelper->injectReflectionService($this->mockReflectionService);
251 
252  $viewHelper->setArguments(array('test' => 'test'));
253 
254  $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(array(
255  'test' => new \TYPO3\CMS\Fluid\Core\ViewHelper\ArgumentDefinition('test', 'stdClass', false, 'documentation')
256  )));
257 
258  $viewHelper->validateArguments();
259  }
260 
265  {
266  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('validateArguments', 'initialize', 'callRenderMethod'));
267  $viewHelper->expects($this->at(0))->method('validateArguments');
268  $viewHelper->expects($this->at(1))->method('initialize');
269  $viewHelper->expects($this->at(2))->method('callRenderMethod')->will($this->returnValue('Output'));
270 
271  $expectedOutput = 'Output';
272  $actualOutput = $viewHelper->initializeArgumentsAndRender(array('argument1' => 'value1'));
273  $this->assertEquals($expectedOutput, $actualOutput);
274  }
275 
280  {
281  $templateVariableContainer = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer::class);
282  $viewHelperVariableContainer = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperVariableContainer::class);
283  $controllerContext = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class, array(), array(), '', false);
284 
285  $renderingContext = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\Rendering\RenderingContext::class, array('dummy'));
286  $renderingContext->injectTemplateVariableContainer($templateVariableContainer);
287  $renderingContext->_set('viewHelperVariableContainer', $viewHelperVariableContainer);
288  $renderingContext->setControllerContext($controllerContext);
289 
290  $viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper::class, array('render', 'prepareArguments'), array(), '', false);
291 
292  $viewHelper->setRenderingContext($renderingContext);
293 
294  $this->assertSame($viewHelper->_get('templateVariableContainer'), $templateVariableContainer);
295  $this->assertSame($viewHelper->_get('viewHelperVariableContainer'), $viewHelperVariableContainer);
296  $this->assertSame($viewHelper->_get('controllerContext'), $controllerContext);
297  }
298 }