[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / View / StandaloneViewTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\View;
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\Core\Cache\CacheManager;
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
19 use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
20 use TYPO3\CMS\Extbase\Mvc\Web\Request;
21 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
22 use TYPO3\CMS\Fluid\Core\Parser\PreProcessor\XmlnsNamespaceTemplatePreProcessor;
23 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
24 use TYPO3\CMS\Fluid\Core\Variables\CmsVariableProvider;
25 use TYPO3\CMS\Fluid\View\StandaloneView;
26 use TYPO3\CMS\Fluid\View\TemplatePaths;
27 use TYPO3Fluid\Core\Compiler\TemplateCompiler;
28 use TYPO3Fluid\Fluid\Core\Parser\TemplateParser;
29 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
30
31 /**
32 * Test case
33 */
34 class StandaloneViewTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
35 {
36 /**
37 * @var array A backup of registered singleton instances
38 */
39 protected $singletonInstances = [];
40
41 /**
42 * @var StandaloneView|\TYPO3\CMS\Components\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject
43 */
44 protected $view;
45
46 /**
47 * @var \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface|\PHPUnit_Framework_MockObject_MockObject
48 */
49 protected $mockRenderingContext;
50
51 /**
52 * @var ViewHelperVariableContainer|\PHPUnit_Framework_MockObject_MockObject
53 */
54 protected $mockViewHelperVariableContainer;
55
56 /**
57 * @var ControllerContext|\PHPUnit_Framework_MockObject_MockObject
58 */
59 protected $mockControllerContext;
60
61 /**
62 * @var \TYPO3\CMS\Extbase\Object\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
63 */
64 protected $mockObjectManager;
65
66 /**
67 * @var Request|\PHPUnit_Framework_MockObject_MockObject
68 */
69 protected $mockRequest;
70
71 /**
72 * @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject
73 */
74 protected $mockUriBuilder;
75
76 /**
77 * @var ConfigurationManagerInterface|\PHPUnit_Framework_MockObject_MockObject
78 */
79 protected $mockConfigurationManager;
80
81 /**
82 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer|\PHPUnit_Framework_MockObject_MockObject
83 */
84 protected $mockContentObject;
85
86 /**
87 * @var TemplatePaths|\PHPUnit_Framework_MockObject_MockObject
88 */
89 protected $mockTemplatePaths;
90
91 /**
92 * @var CmsVariableProvider|\PHPUnit_Framework_MockObject_MockObject
93 */
94 protected $mockVariableProvider;
95
96 /**
97 * @var CacheManager|\PHPUnit_Framework_MockObject_MockObject
98 */
99 protected $mockCacheManager;
100
101 /**
102 * Sets up this test case
103 *
104 * @return void
105 */
106 protected function setUp()
107 {
108 $this->singletonInstances = GeneralUtility::getSingletonInstances();
109 $this->view = $this->getAccessibleMock(
110 \TYPO3\CMS\Fluid\View\StandaloneView::class,
111 ['testFileExistence', 'buildParserConfiguration', 'getOrParseAndStoreTemplate'], [], '', false
112 );
113 $this->mockConfigurationManager = $this->createMock(ConfigurationManagerInterface::class);
114 $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
115 $this->mockObjectManager->expects($this->any())->method('get')->will($this->returnCallback([$this, 'objectManagerCallback']));
116 $this->mockRequest = $this->createMock(Request::class);
117 $this->mockUriBuilder = $this->createMock(UriBuilder::class);
118 $this->mockContentObject = $this->createMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
119 $this->mockControllerContext = $this->createMock(ControllerContext::class);
120 $this->mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->mockRequest));
121 $this->mockTemplatePaths = $this->createMock(TemplatePaths::class);
122 $this->mockViewHelperVariableContainer = $this->createMock(ViewHelperVariableContainer::class);
123 $this->mockRenderingContext = $this->createMock(\TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture::class);
124 $this->mockRenderingContext->expects($this->any())->method('getControllerContext')->will($this->returnValue($this->mockControllerContext));
125 $this->mockRenderingContext->expects($this->any())->method('getViewHelperVariableContainer')->will($this->returnValue($this->mockViewHelperVariableContainer));
126 $this->mockRenderingContext->expects($this->any())->method('getVariableProvider')->willReturn($this->mockVariableProvider);
127 $this->mockRenderingContext->expects($this->any())->method('getTemplatePaths')->willReturn($this->mockTemplatePaths);
128 $this->view->_set('objectManager', $this->mockObjectManager);
129 $this->view->_set('baseRenderingContext', $this->mockRenderingContext);
130 $this->view->_set('controllerContext', $this->mockControllerContext);
131 $this->view->expects($this->any())->method('getOrParseAndStoreTemplate')->willReturn($this->mockParsedTemplate);
132 GeneralUtility::setSingletonInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class, $this->mockObjectManager);
133 GeneralUtility::addInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, $this->mockContentObject);
134
135 $this->mockCacheManager = $this->createMock(\TYPO3\CMS\Core\Cache\CacheManager::class);
136 $mockCache = $this->createMock(\TYPO3Fluid\Fluid\Core\Cache\FluidCacheInterface::class);
137 $this->mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
138 GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Cache\CacheManager::class, $this->mockCacheManager);
139 }
140
141 /**
142 * @return void
143 */
144 protected function tearDown()
145 {
146 GeneralUtility::purgeInstances();
147 GeneralUtility::resetSingletonInstances($this->singletonInstances);
148 parent::tearDown();
149 }
150
151 /**
152 * @param string $className
153 * @return ConfigurationManagerInterface|TemplateParser|RenderingContext|Request|UriBuilder|ControllerContext|TemplateCompiler
154 */
155 public function objectManagerCallback($className)
156 {
157 switch ($className) {
158 case ConfigurationManagerInterface::class:
159 return $this->mockConfigurationManager;
160 case TemplateParser::class:
161 return $this->mockTemplateParser;
162 case RenderingContext::class:
163 return $this->mockRenderingContext;
164 case Request::class:
165 return $this->mockRequest;
166 case UriBuilder::class:
167 return $this->mockUriBuilder;
168 case ControllerContext::class:
169 return $this->mockControllerContext;
170 case TemplateCompiler::class:
171 return $this->mockTemplateCompiler;
172 case TemplatePaths::class:
173 return $this->mockTemplatePaths;
174 case CacheManager::class:
175 return $this->mockCacheManager;
176 case XmlnsNamespaceTemplatePreProcessor::class:
177 return $this->mockTemplateProcessor;
178 }
179 throw new \InvalidArgumentException('objectManagerCallback cannot handle class "' . $className . '". Looks like incomplete mocking in the tests.', 1417105493);
180 }
181
182 /**
183 * @test
184 */
185 public function constructorSetsSpecifiedContentObject()
186 {
187 $mockContentObject = $this->createMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
188 $this->mockConfigurationManager->expects($this->once())->method('setContentObject')->with($this->identicalTo($mockContentObject));
189 new StandaloneView($mockContentObject);
190 }
191
192 /**
193 * @test
194 */
195 public function constructorCreatesContentObjectIfItIsNotSpecified()
196 {
197 $this->mockConfigurationManager->expects($this->once())->method('setContentObject')->with($this->identicalTo($this->mockContentObject));
198 new StandaloneView();
199 }
200
201 /**
202 * @test
203 */
204 public function constructorSetsRequestUri()
205 {
206 $expectedRequestUri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
207 $this->mockRequest->expects($this->once())->method('setRequestURI')->with($expectedRequestUri);
208 new StandaloneView();
209 }
210
211 /**
212 * @test
213 */
214 public function constructorSetsBaseUri()
215 {
216 $expectedBaseUri = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
217 $this->mockRequest->expects($this->once())->method('setBaseURI')->with($expectedBaseUri);
218 new StandaloneView();
219 }
220
221 /**
222 * @test
223 */
224 public function constructorInjectsRequestToUriBuilder()
225 {
226 $this->mockUriBuilder->expects($this->once())->method('setRequest')->with($this->mockRequest);
227 new StandaloneView();
228 }
229
230 /**
231 * @test
232 */
233 public function constructorInjectsRequestToControllerContext()
234 {
235 $this->mockControllerContext->expects($this->once())->method('setRequest')->with($this->mockRequest);
236 new StandaloneView();
237 }
238
239 /**
240 * @test
241 */
242 public function constructorInjectsUriBuilderToControllerContext()
243 {
244 $this->mockControllerContext->expects($this->once())->method('setUriBuilder')->with($this->mockUriBuilder);
245 new StandaloneView();
246 }
247
248 /**
249 * @test
250 */
251 public function setFormatSetsRequestFormat()
252 {
253 $this->mockRequest->expects($this->once())->method('setFormat')->with('xml');
254 $this->view->setFormat('xml');
255 }
256 }