[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Tests / Unit / ViewHelpers / ViewHelperBaseTestcase.php
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;
18 use TYPO3\CMS\Extbase\Error\Result;
19 use TYPO3\CMS\Extbase\Reflection\ReflectionService;
20 use TYPO3\CMS\Fluid\Core\Variables\CmsVariableProvider;
21 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
25
26 /**
27 * Base test class for testing view helpers
28 */
29 abstract class ViewHelperBaseTestcase extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
30 {
31 /**
32 * @var ViewHelperVariableContainer|ObjectProphecy
33 */
34 protected $viewHelperVariableContainer;
35
36 /**
37 * @var CmsVariableProvider
38 */
39 protected $templateVariableContainer;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
43 */
44 protected $uriBuilder;
45
46 /**
47 * @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
48 */
49 protected $controllerContext;
50
51 /**
52 * @var TagBuilder
53 */
54 protected $tagBuilder;
55
56 /**
57 * @var \TYPO3\CMS\Fluid\Core\ViewHelper\Arguments
58 */
59 protected $arguments;
60
61 /**
62 * @var \TYPO3\CMS\Extbase\Mvc\Web\Request
63 */
64 protected $request;
65
66 /**
67 * @var \TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContext
68 */
69 protected $renderingContext;
70
71 /**
72 * @var \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService
73 */
74 protected $mvcPropertyMapperConfigurationService;
75
76 /**
77 * @return void
78 */
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
110 /**
111 * @param ViewHelperInterface $viewHelper
112 * @return void
113 */
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
126 /**
127 * Helper function to merge arguments with default arguments according to their registration
128 * This usually happens in ViewHelperInvoker before the view helper methods are called
129 *
130 * @param ViewHelperInterface $viewHelper
131 * @param array $arguments
132 */
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
144 /**
145 * Helper function for a valid mapping result
146 */
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
158 /**
159 * Helper function for a mapping result with errors
160 */
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
172 /**
173 * Helper function for the bound property
174 *
175 * @param $formObject
176 */
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 }