TYPO3 CMS  TYPO3_8-7
StandaloneViewTest.php
Go to the documentation of this file.
1 <?php
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  */
30 
34 class StandaloneViewTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
35 {
39  protected $singletonInstances = [];
40 
44  protected $view;
45 
50 
55 
60 
64  protected $mockObjectManager;
65 
69  protected $mockRequest;
70 
74  protected $mockUriBuilder;
75 
80 
84  protected $mockContentObject;
85 
89  protected $mockTemplatePaths;
90 
95 
99  protected $mockCacheManager;
100 
104  protected function setUp()
105  {
106  $this->singletonInstances = GeneralUtility::getSingletonInstances();
107  $this->view = $this->getAccessibleMock(
108  \TYPO3\CMS\Fluid\View\StandaloneView::class,
109  ['testFileExistence', 'buildParserConfiguration', 'getOrParseAndStoreTemplate'],
110  [],
111  '',
112  false
113  );
114  $this->mockConfigurationManager = $this->createMock(ConfigurationManagerInterface::class);
115  $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
116  $this->mockObjectManager->expects($this->any())->method('get')->will($this->returnCallback([$this, 'objectManagerCallback']));
117  $this->mockRequest = $this->createMock(Request::class);
118  $this->mockUriBuilder = $this->createMock(UriBuilder::class);
119  $this->mockContentObject = $this->createMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
120  $this->mockControllerContext = $this->createMock(ControllerContext::class);
121  $this->mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->mockRequest));
122  $this->mockTemplatePaths = $this->createMock(TemplatePaths::class);
123  $this->mockViewHelperVariableContainer = $this->createMock(ViewHelperVariableContainer::class);
124  $this->mockRenderingContext = $this->createMock(\TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture::class);
125  $this->mockRenderingContext->expects($this->any())->method('getControllerContext')->will($this->returnValue($this->mockControllerContext));
126  $this->mockRenderingContext->expects($this->any())->method('getViewHelperVariableContainer')->will($this->returnValue($this->mockViewHelperVariableContainer));
127  $this->mockRenderingContext->expects($this->any())->method('getVariableProvider')->willReturn($this->mockVariableProvider);
128  $this->mockRenderingContext->expects($this->any())->method('getTemplatePaths')->willReturn($this->mockTemplatePaths);
129  $this->view->_set('objectManager', $this->mockObjectManager);
130  $this->view->_set('baseRenderingContext', $this->mockRenderingContext);
131  $this->view->_set('controllerContext', $this->mockControllerContext);
132  $this->view->expects($this->any())->method('getOrParseAndStoreTemplate')->willReturn($this->mockParsedTemplate);
133  GeneralUtility::setSingletonInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class, $this->mockObjectManager);
134  GeneralUtility::addInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, $this->mockContentObject);
135 
136  $this->mockCacheManager = $this->createMock(\TYPO3\CMS\Core\Cache\CacheManager::class);
137  $mockCache = $this->createMock(\TYPO3Fluid\Fluid\Core\Cache\FluidCacheInterface::class);
138  $this->mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
139  GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Cache\CacheManager::class, $this->mockCacheManager);
140  }
141 
144  protected function tearDown()
145  {
147  GeneralUtility::resetSingletonInstances($this->singletonInstances);
148  parent::tearDown();
149  }
150 
155  public function objectManagerCallback($className)
156  {
157  switch ($className) {
158  case ConfigurationManagerInterface::class:
160  case TemplateParser::class:
161  return $this->mockTemplateParser;
162  case RenderingContext::class:
164  case Request::class:
165  return $this->mockRequest;
166  case UriBuilder::class:
167  return $this->mockUriBuilder;
168  case ControllerContext::class:
170  case TemplateCompiler::class:
171  return $this->mockTemplateCompiler;
172  case TemplatePaths::class:
174  case CacheManager::class:
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 
186  {
187  $mockContentObject = $this->createMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
188  $this->mockConfigurationManager->expects($this->once())->method('setContentObject')->with($this->identicalTo($mockContentObject));
190  }
191 
196  {
197  $this->mockConfigurationManager->expects($this->once())->method('setContentObject')->with($this->identicalTo($this->mockContentObject));
198  new StandaloneView();
199  }
200 
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 
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 
225  {
226  $this->mockUriBuilder->expects($this->once())->method('setRequest')->with($this->mockRequest);
227  new StandaloneView();
228  }
229 
234  {
235  $this->mockControllerContext->expects($this->once())->method('setRequest')->with($this->mockRequest);
236  new StandaloneView();
237  }
238 
243  {
244  $this->mockControllerContext->expects($this->once())->method('setUriBuilder')->with($this->mockUriBuilder);
245  new StandaloneView();
246  }
247 
251  public function setFormatSetsRequestFormat()
252  {
253  $this->mockRequest->expects($this->once())->method('setFormat')->with('xml');
254  $this->view->setFormat('xml');
255  }
256 }
static addInstance($className, $instance)
static setSingletonInstance($className, SingletonInterface $instance)
static resetSingletonInstances(array $newSingletonInstances)