TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ViewHelperBaseTestcase.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
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 Prophecy\Argument;
17 use Prophecy\Prophecy\ObjectProphecy;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
25 
30 {
35 
40 
44  protected $uriBuilder;
45 
49  protected $controllerContext;
50 
54  protected $tagBuilder;
55 
59  protected $arguments;
60 
64  protected $request;
65 
69  protected $renderingContext;
70 
75 
79  protected function setUp()
80  {
81  $this->viewHelperVariableContainer = $this->prophesize(ViewHelperVariableContainer::class);
82  $this->templateVariableContainer = $this->createMock(CmsVariableProvider::class);
83  $this->uriBuilder = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class);
84  $this->uriBuilder->expects($this->any())->method('reset')->will($this->returnValue($this->uriBuilder));
85  $this->uriBuilder->expects($this->any())->method('setArguments')->will($this->returnValue($this->uriBuilder));
86  $this->uriBuilder->expects($this->any())->method('setSection')->will($this->returnValue($this->uriBuilder));
87  $this->uriBuilder->expects($this->any())->method('setFormat')->will($this->returnValue($this->uriBuilder));
88  $this->uriBuilder->expects($this->any())->method('setCreateAbsoluteUri')->will($this->returnValue($this->uriBuilder));
89  $this->uriBuilder->expects($this->any())->method('setAddQueryString')->will($this->returnValue($this->uriBuilder));
90  $this->uriBuilder->expects($this->any())->method('setArgumentsToBeExcludedFromQueryString')->will($this->returnValue($this->uriBuilder));
91  $this->uriBuilder->expects($this->any())->method('setLinkAccessRestrictedPages')->will($this->returnValue($this->uriBuilder));
92  $this->uriBuilder->expects($this->any())->method('setTargetPageUid')->will($this->returnValue($this->uriBuilder));
93  $this->uriBuilder->expects($this->any())->method('setTargetPageType')->will($this->returnValue($this->uriBuilder));
94  $this->uriBuilder->expects($this->any())->method('setNoCache')->will($this->returnValue($this->uriBuilder));
95  $this->uriBuilder->expects($this->any())->method('setUseCacheHash')->will($this->returnValue($this->uriBuilder));
96  $this->uriBuilder->expects($this->any())->method('setAddQueryStringMethod')->will($this->returnValue($this->uriBuilder));
97  $this->request = $this->prophesize(\TYPO3\CMS\Extbase\Mvc\Web\Request::class);
98  $this->controllerContext = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class);
99  $this->controllerContext->expects($this->any())->method('getUriBuilder')->will($this->returnValue($this->uriBuilder));
100  $this->controllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->request->reveal()));
101  $this->arguments = [];
102  $this->renderingContext = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture::class, ['getControllerContext']);
103  $this->renderingContext->expects($this->any())->method('getControllerContext')->willReturn($this->controllerContext);
104  $this->renderingContext->setVariableProvider($this->templateVariableContainer);
105  $this->renderingContext->_set('viewHelperVariableContainer', $this->viewHelperVariableContainer->reveal());
106  $this->renderingContext->setControllerContext($this->controllerContext);
107  $this->mvcPropertyMapperConfigurationService = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class, ['dummy']);
108  }
109 
114  protected function injectDependenciesIntoViewHelper(ViewHelperInterface $viewHelper)
115  {
116  $viewHelper->setRenderingContext($this->renderingContext);
117  $viewHelper->setArguments($this->arguments);
118  // this condition is needed, because the (Be)/Security\*ViewHelper don't extend the
119  // AbstractViewHelper and contain no method injectReflectionService()
120  if ($viewHelper instanceof AbstractViewHelper) {
121  $reflectionServiceProphecy = $this->prophesize(ReflectionService::class);
122  $viewHelper->injectReflectionService($reflectionServiceProphecy->reveal());
123  }
124  }
125 
133  protected function setArgumentsUnderTest(ViewHelperInterface $viewHelper, array $arguments = [])
134  {
135  $argumentDefinitions = $viewHelper->prepareArguments();
136  foreach ($argumentDefinitions as $argumentName => $argumentDefinition) {
137  if (!isset($arguments[$argumentName])) {
138  $arguments[$argumentName] = $argumentDefinition->getDefaultValue();
139  }
140  }
141  $viewHelper->setArguments($arguments);
142  }
143 
147  protected function stubRequestWithoutMappingErrors()
148  {
149  $this->request->getOriginalRequest()->willReturn(null);
150  $this->request->getArguments()->willReturn([]);
151  $result = $this->prophesize(Result::class);
152  $result->forProperty('objectName')->willReturn($result->reveal());
153  $result->forProperty('someProperty')->willReturn($result->reveal());
154  $result->hasErrors()->willReturn(false);
155  $this->request->getOriginalRequestMappingResults()->willReturn($result->reveal());
156  }
157 
161  protected function stubRequestWithMappingErrors()
162  {
163  $this->request->getOriginalRequest()->willReturn(null);
164  $this->request->getArguments()->willReturn([]);
165  $result = $this->prophesize(Result::class);
166  $result->forProperty('objectName')->willReturn($result->reveal());
167  $result->forProperty('someProperty')->willReturn($result->reveal());
168  $result->hasErrors()->willReturn(true);
169  $this->request->getOriginalRequestMappingResults()->willReturn($result->reveal());
170  }
171 
177  protected function stubVariableContainer($formObject)
178  {
179  $this->viewHelperVariableContainer->exists(Argument::cetera())->willReturn(true);
180  $this->viewHelperVariableContainer->get(Argument::any(),
181  'formObjectName')->willReturn('objectName');
182  $this->viewHelperVariableContainer->get(Argument::any(),
183  'fieldNamePrefix')->willReturn('fieldPrefix');
184  $this->viewHelperVariableContainer->get(Argument::any(), 'formFieldNames')->willReturn([]);
185  $this->viewHelperVariableContainer->get(Argument::any(),
186  'formObject')->willReturn($formObject);
187  $this->viewHelperVariableContainer->get(Argument::any(),
188  'renderedHiddenFields')->willReturn([]);
189  $this->viewHelperVariableContainer->addOrUpdate(Argument::cetera())->willReturn(null);
190  }
191 }
setArgumentsUnderTest(ViewHelperInterface $viewHelper, array $arguments=[])
getAccessibleMock($originalClassName, $methods=[], array $arguments=[], $mockClassName= '', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)