b7f33cb4a9c1c963da5f0a2e29ca141df372350b
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Controller / ActionControllerTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Page\PageRenderer;
19 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
20 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
21 use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
22 use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
23 use TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService;
24 use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentTypeException;
25 use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchActionException;
26 use TYPO3\CMS\Extbase\Mvc\Request;
27 use TYPO3\CMS\Extbase\Mvc\RequestInterface;
28 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
29 use TYPO3\CMS\Extbase\Mvc\Web\Request as WebRequest;
30 use TYPO3\CMS\Extbase\Mvc\Web\Response;
31 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
32 use TYPO3\CMS\Extbase\Object\ObjectManager;
33 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
34 use TYPO3\CMS\Extbase\Reflection\ReflectionService;
35 use TYPO3\CMS\Fluid\View\TemplateView;
36 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
37 use TYPO3Fluid\Fluid\View\AbstractTemplateView;
38 use TYPO3Fluid\Fluid\View\TemplateView as FluidTemplateView;
39
40 /**
41 * Test case
42 */
43 class ActionControllerTest extends UnitTestCase
44 {
45 /**
46 * @var \TYPO3\CMS\Extbase\Mvc\Controller\ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
47 */
48 protected $actionController;
49
50 /**
51 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
52 */
53 protected $mockObjectManager;
54
55 /**
56 * @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
57 */
58 protected $mockUriBuilder;
59
60 /**
61 * @var \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService
62 */
63 protected $mockMvcPropertyMappingConfigurationService;
64
65 protected function setUp()
66 {
67 $this->actionController = $this->getAccessibleMock(ActionController::class);
68 }
69
70 /**
71 * @test
72 */
73 public function processRequestSticksToSpecifiedSequence()
74 {
75 $mockRequest = $this->createMock(WebRequest::class);
76 $mockRequest->expects($this->once())->method('setDispatched')->with(true);
77 $mockUriBuilder = $this->createMock(UriBuilder::class);
78 $mockUriBuilder->expects($this->once())->method('setRequest')->with($mockRequest);
79 $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
80 $mockObjectManager->expects($this->once())->method('get')->with(UriBuilder::class)->will($this->returnValue($mockUriBuilder));
81 $mockResponse = $this->createMock(Response::class);
82 $configurationService = $this->createMock(MvcPropertyMappingConfigurationService::class);
83 /** @var \TYPO3\CMS\Extbase\Mvc\Controller\ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface */
84 $mockController = $this->getAccessibleMock(ActionController::class, [
85 'initializeFooAction',
86 'initializeAction',
87 'resolveActionMethodName',
88 'initializeActionMethodArguments',
89 'initializeActionMethodValidators',
90 'mapRequestArgumentsToControllerArguments',
91 'buildControllerContext',
92 'resolveView',
93 'initializeView',
94 'callActionMethod',
95 'checkRequestHash'
96 ], [], '', false);
97 $mockController->_set('objectManager', $mockObjectManager);
98
99 $mockController->expects($this->at(0))->method('resolveActionMethodName')->will($this->returnValue('fooAction'));
100 $mockController->expects($this->at(1))->method('initializeActionMethodArguments');
101 $mockController->expects($this->at(2))->method('initializeActionMethodValidators');
102 $mockController->expects($this->at(3))->method('initializeAction');
103 $mockController->expects($this->at(4))->method('initializeFooAction');
104 $mockController->expects($this->at(5))->method('mapRequestArgumentsToControllerArguments');
105 $mockController->expects($this->at(6))->method('checkRequestHash');
106 $mockController->expects($this->at(7))->method('buildControllerContext');
107 $mockController->expects($this->at(8))->method('resolveView');
108
109 $mockController->_set('mvcPropertyMappingConfigurationService', $configurationService);
110 $mockController->_set('arguments', new Arguments());
111
112 $mockController->processRequest($mockRequest, $mockResponse);
113 $this->assertSame($mockRequest, $mockController->_get('request'));
114 $this->assertSame($mockResponse, $mockController->_get('response'));
115 }
116
117 /**
118 * @test
119 */
120 public function resolveViewUsesFluidTemplateViewIfTemplateIsAvailable()
121 {
122 $mockControllerContext = $this->createMock(ControllerContext::class);
123 $mockFluidTemplateView = $this->createMock(ViewInterface::class);
124 $mockFluidTemplateView->expects($this->once())->method('setControllerContext')->with($mockControllerContext);
125 $mockFluidTemplateView->expects($this->once())->method('canRender')->with($mockControllerContext)->will($this->returnValue(true));
126 $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
127 $mockObjectManager->expects($this->at(0))->method('get')->with(TemplateView::class)->will($this->returnValue($mockFluidTemplateView));
128 $mockController = $this->getAccessibleMock(ActionController::class, ['buildControllerContext', 'resolveViewObjectName', 'setViewConfiguration'], [], '', false);
129 $mockController->expects($this->once())->method('resolveViewObjectName')->will($this->returnValue(false));
130 $mockController->_set('objectManager', $mockObjectManager);
131 $mockController->_set('controllerContext', $mockControllerContext);
132 $this->assertSame($mockFluidTemplateView, $mockController->_call('resolveView'));
133 }
134
135 /**
136 * @test
137 */
138 public function resolveViewObjectNameUsesViewObjectNamePatternToResolveViewObjectName()
139 {
140 $mockRequest = $this->createMock(Request::class);
141 $mockRequest->expects($this->once())->method('getControllerVendorName')->will($this->returnValue('MyVendor'));
142 $mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('MyPackage'));
143 $mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('MyController'));
144 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('MyAction'));
145 $mockRequest->expects($this->atLeastOnce())->method('getFormat')->will($this->returnValue('MyFormat'));
146 $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
147 $mockController = $this->getAccessibleMock(ActionController::class, ['dummy'], [], '', false);
148 $mockController->_set('request', $mockRequest);
149 $mockController->_set('objectManager', $mockObjectManager);
150 $mockController->_set('namespacesViewObjectNamePattern', 'RandomViewObject@vendor\@extension\View\@controller\@action@format');
151 $mockController->_call('resolveViewObjectName');
152 }
153
154 /**
155 * @test
156 */
157 public function resolveViewObjectNameUsesNamespacedViewObjectNamePatternForExtensionsWithVendor()
158 {
159 eval('namespace MyVendor\MyPackage\View\MyController; class MyActionMyFormat {}');
160
161 $mockRequest = $this->createMock(Request::class);
162 $mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('MyPackage'));
163 $mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('MyController'));
164 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('MyAction'));
165 $mockRequest->expects($this->once())->method('getControllerVendorName')->will($this->returnValue('MyVendor'));
166 $mockRequest->expects($this->atLeastOnce())->method('getFormat')->will($this->returnValue('MyFormat'));
167 $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
168 $mockController = $this->getAccessibleMock(ActionController::class, ['dummy'], [], '', false);
169 $mockController->_set('request', $mockRequest);
170 $mockController->_set('objectManager', $mockObjectManager);
171
172 $this->assertEquals(
173 'MyVendor\MyPackage\View\MyController\MyActionMyFormat',
174 $mockController->_call('resolveViewObjectName')
175 );
176 }
177
178 /**
179 * @test
180 */
181 public function resolveActionMethodNameReturnsTheCurrentActionMethodNameFromTheRequest()
182 {
183 $mockRequest = $this->createMock(Request::class);
184 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('fooBar'));
185 /** @var \TYPO3\CMS\Extbase\Mvc\Controller\ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface */
186 $mockController = $this->getAccessibleMock(ActionController::class, ['fooBarAction'], [], '', false);
187 $mockController->_set('request', $mockRequest);
188 $this->assertEquals('fooBarAction', $mockController->_call('resolveActionMethodName'));
189 }
190
191 /**
192 * @test
193 */
194 public function resolveActionMethodNameThrowsAnExceptionIfTheActionDefinedInTheRequestDoesNotExist()
195 {
196 $this->expectException(NoSuchActionException::class);
197 $this->expectExceptionCode(1186669086);
198 $mockRequest = $this->createMock(Request::class);
199 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('fooBar'));
200 /** @var \TYPO3\CMS\Extbase\Mvc\Controller\ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface */
201 $mockController = $this->getAccessibleMock(ActionController::class, ['otherBarAction'], [], '', false);
202 $mockController->_set('request', $mockRequest);
203 $mockController->_call('resolveActionMethodName');
204 }
205
206 /**
207 * @test
208 */
209 public function initializeActionMethodArgumentsRegistersArgumentsFoundInTheSignatureOfTheCurrentActionMethod()
210 {
211 $mockRequest = $this->createMock(Request::class);
212 $mockArguments = $this->getMockBuilder(Arguments::class)
213 ->setMethods(['addNewArgument', 'removeAll'])
214 ->getMock();
215 $mockArguments->expects($this->at(0))->method('addNewArgument')->with('stringArgument', 'string', true);
216 $mockArguments->expects($this->at(1))->method('addNewArgument')->with('integerArgument', 'integer', true);
217 $mockArguments->expects($this->at(2))->method('addNewArgument')->with('objectArgument', 'F3_Foo_Bar', true);
218 $mockController = $this->getAccessibleMock(ActionController::class, ['fooAction', 'evaluateDontValidateAnnotations'], [], '', false);
219 $methodParameters = [
220 'stringArgument' => [
221 'position' => 0,
222 'byReference' => false,
223 'array' => false,
224 'optional' => false,
225 'allowsNull' => false,
226 'type' => 'string',
227 'hasDefaultValue' => false
228 ],
229 'integerArgument' => [
230 'position' => 1,
231 'byReference' => false,
232 'array' => false,
233 'optional' => false,
234 'allowsNull' => false,
235 'type' => 'integer',
236 'hasDefaultValue' => false
237 ],
238 'objectArgument' => [
239 'position' => 2,
240 'byReference' => false,
241 'array' => false,
242 'optional' => false,
243 'allowsNull' => false,
244 'type' => 'F3_Foo_Bar',
245 'hasDefaultValue' => false
246 ]
247 ];
248 $mockReflectionService = $this->createMock(ReflectionService::class);
249 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
250 $mockController->_set('reflectionService', $mockReflectionService);
251 $mockController->_set('request', $mockRequest);
252 $mockController->_set('arguments', $mockArguments);
253 $mockController->_set('actionMethodName', 'fooAction');
254 $mockController->_call('initializeActionMethodArguments');
255 }
256
257 /**
258 * @test
259 */
260 public function initializeActionMethodArgumentsRegistersOptionalArgumentsAsSuch()
261 {
262 $mockRequest = $this->createMock(Request::class);
263 $mockArguments = $this->createMock(Arguments::class);
264 $mockArguments->expects($this->at(0))->method('addNewArgument')->with('arg1', 'string', true);
265 $mockArguments->expects($this->at(1))->method('addNewArgument')->with('arg2', 'array', false, [21]);
266 $mockArguments->expects($this->at(2))->method('addNewArgument')->with('arg3', 'string', false, 42);
267 $mockController = $this->getAccessibleMock(ActionController::class, ['fooAction', 'evaluateDontValidateAnnotations'], [], '', false);
268 $methodParameters = [
269 'arg1' => [
270 'position' => 0,
271 'byReference' => false,
272 'array' => false,
273 'optional' => false,
274 'allowsNull' => false,
275 'type' => 'string',
276 'hasDefaultValue' => false
277 ],
278 'arg2' => [
279 'position' => 1,
280 'byReference' => false,
281 'array' => true,
282 'optional' => true,
283 'defaultValue' => [21],
284 'allowsNull' => false,
285 'hasDefaultValue' => true
286 ],
287 'arg3' => [
288 'position' => 2,
289 'byReference' => false,
290 'array' => false,
291 'optional' => true,
292 'defaultValue' => 42,
293 'allowsNull' => false,
294 'type' => 'string',
295 'hasDefaultValue' => true
296 ]
297 ];
298 $mockReflectionService = $this->createMock(ReflectionService::class);
299 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
300 $mockController->_set('reflectionService', $mockReflectionService);
301 $mockController->_set('request', $mockRequest);
302 $mockController->_set('arguments', $mockArguments);
303 $mockController->_set('actionMethodName', 'fooAction');
304 $mockController->_call('initializeActionMethodArguments');
305 }
306
307 /**
308 * @test
309 */
310 public function initializeActionMethodArgumentsThrowsExceptionIfDataTypeWasNotSpecified()
311 {
312 $this->expectException(InvalidArgumentTypeException::class);
313 $this->expectExceptionCode(1253175643);
314 $mockRequest = $this->createMock(Request::class);
315 $mockArguments = $this->createMock(Arguments::class);
316 $mockController = $this->getAccessibleMock(ActionController::class, ['fooAction'], [], '', false);
317 $methodParameters = [
318 'arg1' => [
319 'position' => 0,
320 'byReference' => false,
321 'array' => false,
322 'optional' => false,
323 'allowsNull' => false
324 ]
325 ];
326 $mockReflectionService = $this->createMock(ReflectionService::class);
327 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
328 $mockController->_set('reflectionService', $mockReflectionService);
329 $mockController->_set('request', $mockRequest);
330 $mockController->_set('arguments', $mockArguments);
331 $mockController->_set('actionMethodName', 'fooAction');
332 $mockController->_call('initializeActionMethodArguments');
333 }
334
335 /**
336 * @test
337 * @dataProvider templateRootPathDataProvider
338 * @param array $configuration
339 * @param array $expected
340 */
341 public function setViewConfigurationResolvesTemplateRootPathsForTemplateRootPath($configuration, $expected)
342 {
343 /** @var ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $mockController */
344 $mockController = $this->getAccessibleMock(ActionController::class, ['dummy'], [], '', false);
345 /** @var ConfigurationManagerInterface|\PHPUnit_Framework_MockObject_MockObject $mockConfigurationManager */
346 $mockConfigurationManager = $this->createMock(ConfigurationManagerInterface::class);
347 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
348 $mockController->injectConfigurationManager($mockConfigurationManager);
349 $mockController->_set('request', $this->createMock(Request::class), ['getControllerExtensionKey']);
350 $view = $this->getMockBuilder(ViewInterface::class)
351 ->setMethods(['setControllerContext', 'assign', 'assignMultiple', 'canRender', 'render', 'initializeView', 'setTemplateRootPaths'])
352 ->getMock();
353 $view->expects($this->once())->method('setTemplateRootPaths')->with($expected);
354 $mockController->_call('setViewConfiguration', $view);
355 }
356
357 /**
358 * @return array
359 */
360 public function templateRootPathDataProvider()
361 {
362 return [
363 'text keys' => [
364 [
365 'view' => [
366 'templateRootPaths' => [
367 'default' => 'some path',
368 'extended' => 'some other path'
369 ]
370 ]
371 ],
372 [
373 'extended' => 'some other path',
374 'default' => 'some path'
375 ]
376 ],
377 'numerical keys' => [
378 [
379 'view' => [
380 'templateRootPaths' => [
381 '10' => 'some path',
382 '20' => 'some other path',
383 '15' => 'intermediate specific path'
384 ]
385 ]
386 ],
387 [
388 '20' => 'some other path',
389 '15' => 'intermediate specific path',
390 '10' => 'some path'
391 ]
392 ],
393 'mixed keys' => [
394 [
395 'view' => [
396 'templateRootPaths' => [
397 '10' => 'some path',
398 'very_specific' => 'some other path',
399 '15' => 'intermediate specific path'
400 ]
401 ]
402 ],
403 [
404 '15' => 'intermediate specific path',
405 'very_specific' => 'some other path',
406 '10' => 'some path'
407 ]
408 ],
409 ];
410 }
411
412 /**
413 * @test
414 * @dataProvider layoutRootPathDataProvider
415 *
416 * @param array $configuration
417 * @param array $expected
418 */
419 public function setViewConfigurationResolvesLayoutRootPathsForLayoutRootPath($configuration, $expected)
420 {
421 /** @var ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $mockController */
422 $mockController = $this->getAccessibleMock(ActionController::class, ['dummy'], [], '', false);
423 /** @var ConfigurationManagerInterface|\PHPUnit_Framework_MockObject_MockObject $mockConfigurationManager */
424 $mockConfigurationManager = $this->createMock(ConfigurationManagerInterface::class);
425 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
426 $mockController->injectConfigurationManager($mockConfigurationManager);
427 $mockController->_set('request', $this->createMock(Request::class), ['getControllerExtensionKey']);
428 $view = $this->getMockBuilder(ViewInterface::class)
429 ->setMethods(['setControllerContext', 'assign', 'assignMultiple', 'canRender', 'render', 'initializeView', 'setlayoutRootPaths'])
430 ->getMock();
431 $view->expects($this->once())->method('setlayoutRootPaths')->with($expected);
432 $mockController->_call('setViewConfiguration', $view);
433 }
434
435 /**
436 * @return array
437 */
438 public function layoutRootPathDataProvider()
439 {
440 return [
441 'text keys' => [
442 [
443 'view' => [
444 'layoutRootPaths' => [
445 'default' => 'some path',
446 'extended' => 'some other path'
447 ]
448 ]
449 ],
450 [
451 'extended' => 'some other path',
452 'default' => 'some path'
453 ]
454 ],
455 'numerical keys' => [
456 [
457 'view' => [
458 'layoutRootPaths' => [
459 '10' => 'some path',
460 '20' => 'some other path',
461 '15' => 'intermediate specific path'
462 ]
463 ]
464 ],
465 [
466 '20' => 'some other path',
467 '15' => 'intermediate specific path',
468 '10' => 'some path'
469 ]
470 ],
471 'mixed keys' => [
472 [
473 'view' => [
474 'layoutRootPaths' => [
475 '10' => 'some path',
476 'very_specific' => 'some other path',
477 '15' => 'intermediate specific path'
478 ]
479 ]
480 ],
481 [
482 '15' => 'intermediate specific path',
483 'very_specific' => 'some other path',
484 '10' => 'some path'
485 ]
486 ],
487 ];
488 }
489
490 /**
491 * @test
492 * @dataProvider partialRootPathDataProvider
493 *
494 * @param array $configuration
495 * @param array $expected
496 */
497 public function setViewConfigurationResolvesPartialRootPathsForPartialRootPath($configuration, $expected)
498 {
499 /** @var ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $mockController */
500 $mockController = $this->getAccessibleMock(ActionController::class, ['dummy'], [], '', false);
501 /** @var ConfigurationManagerInterface|\PHPUnit_Framework_MockObject_MockObject $mockConfigurationManager */
502 $mockConfigurationManager = $this->createMock(ConfigurationManagerInterface::class);
503 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
504 $mockController->injectConfigurationManager($mockConfigurationManager);
505 $mockController->_set('request', $this->createMock(Request::class), ['getControllerExtensionKey']);
506 $view = $this->getMockBuilder(ViewInterface::class)
507 ->setMethods(['setControllerContext', 'assign', 'assignMultiple', 'canRender', 'render', 'initializeView', 'setpartialRootPaths'])
508 ->getMock();
509 $view->expects($this->once())->method('setpartialRootPaths')->with($expected);
510 $mockController->_call('setViewConfiguration', $view);
511 }
512
513 /**
514 * @return array
515 */
516 public function partialRootPathDataProvider()
517 {
518 return [
519 'text keys' => [
520 [
521 'view' => [
522 'partialRootPaths' => [
523 'default' => 'some path',
524 'extended' => 'some other path'
525 ]
526 ]
527 ],
528 [
529 'extended' => 'some other path',
530 'default' => 'some path'
531 ]
532 ],
533 'numerical keys' => [
534 [
535 'view' => [
536 'partialRootPaths' => [
537 '10' => 'some path',
538 '20' => 'some other path',
539 '15' => 'intermediate specific path'
540 ]
541 ]
542 ],
543 [
544 '20' => 'some other path',
545 '15' => 'intermediate specific path',
546 '10' => 'some path'
547 ]
548 ],
549 'mixed keys' => [
550 [
551 'view' => [
552 'partialRootPaths' => [
553 '10' => 'some path',
554 'very_specific' => 'some other path',
555 '15' => 'intermediate specific path'
556 ]
557 ]
558 ],
559 [
560 '15' => 'intermediate specific path',
561 'very_specific' => 'some other path',
562 '10' => 'some path'
563 ]
564 ],
565 ];
566 }
567
568 /**
569 * @param FluidTemplateView $viewMock
570 * @param string|null $expectedHeader
571 * @param string|null $expectedFooter
572 * @test
573 * @dataProvider headerAssetDataProvider
574 */
575 public function rendersAndAssignsAssetsFromViewIntoPageRenderer($viewMock, $expectedHeader, $expectedFooter)
576 {
577 $this->mockObjectManager = $this->getMockBuilder(ObjectManager::class)->setMethods(['get'])->getMock();
578 $pageRendererMock = $this->getMockBuilder(PageRenderer::class)->setMethods(['addHeaderData', 'addFooterData'])->getMock();
579 if (!$viewMock instanceof FluidTemplateView) {
580 $this->mockObjectManager->expects($this->never())->method('get');
581 } else {
582 $this->mockObjectManager->expects($this->any())->method('get')->with(PageRenderer::class)->willReturn($pageRendererMock);
583 }
584 if (!empty(trim($expectedHeader ?? ''))) {
585 $pageRendererMock->expects($this->once())->method('addHeaderData')->with($expectedHeader);
586 } else {
587 $pageRendererMock->expects($this->never())->method('addHeaderData');
588 }
589 if (!empty(trim($expectedFooter ?? ''))) {
590 $pageRendererMock->expects($this->once())->method('addFooterData')->with($expectedFooter);
591 } else {
592 $pageRendererMock->expects($this->never())->method('addFooterData');
593 }
594 $requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
595 $subject = new ActionController();
596 $viewProperty = new \ReflectionProperty($subject, 'view');
597 $viewProperty->setAccessible(true);
598 $viewProperty->setValue($subject, $viewMock);
599 $objectManagerProperty = new \ReflectionProperty($subject, 'objectManager');
600 $objectManagerProperty->setAccessible(true);
601 $objectManagerProperty->setValue($subject, $this->mockObjectManager);
602
603 $method = new \ReflectionMethod($subject, 'renderAssetsForRequest');
604 $method->setAccessible(true);
605 $method->invokeArgs($subject, [$requestMock]);
606 }
607
608 /**
609 * @return array
610 */
611 public function headerAssetDataProvider()
612 {
613 $viewWithHeaderData = $this->getMockBuilder(FluidTemplateView::class)->setMethods(['renderSection'])->disableOriginalConstructor()->getMock();
614 $viewWithHeaderData->expects($this->at(0))->method('renderSection')->with('HeaderAssets', $this->anything(), true)->willReturn('custom-header-data');
615 $viewWithHeaderData->expects($this->at(1))->method('renderSection')->with('FooterAssets', $this->anything(), true)->willReturn(null);
616 $viewWithFooterData = $this->getMockBuilder(FluidTemplateView::class)->setMethods(['renderSection'])->disableOriginalConstructor()->getMock();
617 $viewWithFooterData->expects($this->at(0))->method('renderSection')->with('HeaderAssets', $this->anything(), true)->willReturn(null);
618 $viewWithFooterData->expects($this->at(1))->method('renderSection')->with('FooterAssets', $this->anything(), true)->willReturn('custom-footer-data');
619 $viewWithBothData = $this->getMockBuilder(FluidTemplateView::class)->setMethods(['renderSection'])->disableOriginalConstructor()->getMock();
620 $viewWithBothData->expects($this->at(0))->method('renderSection')->with('HeaderAssets', $this->anything(), true)->willReturn('custom-header-data');
621 $viewWithBothData->expects($this->at(1))->method('renderSection')->with('FooterAssets', $this->anything(), true)->willReturn('custom-footer-data');
622 $invalidView = $this->getMockBuilder(AbstractTemplateView::class)->disableOriginalConstructor()->getMockForAbstractClass();
623 return [
624 [$viewWithHeaderData, 'custom-header-data', null],
625 [$viewWithFooterData, null, 'custom-footer-data'],
626 [$viewWithBothData, 'custom-header-data', 'custom-footer-data'],
627 [$invalidView, null, null]
628 ];
629 }
630 }