2 declare(strict_types = 1);
33 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
34 use TYPO3Fluid\Fluid\View\AbstractTemplateView;
35 use TYPO3Fluid\Fluid\View\TemplateView as FluidTemplateView;
72 $mockControllerContext = $this->createMock(ControllerContext::class);
73 $mockFluidTemplateView = $this->createMock(ViewInterface::class);
74 $mockFluidTemplateView->expects($this->once())->method(
'setControllerContext')->with($mockControllerContext);
75 $mockFluidTemplateView->expects($this->once())->method(
'canRender')->with($mockControllerContext)->will($this->returnValue(
true));
77 $mockObjectManager->expects($this->at(0))->method(
'get')->with(TemplateView::class)->will($this->returnValue($mockFluidTemplateView));
78 $mockController = $this->getAccessibleMock(ActionController::class, [
'buildControllerContext',
'resolveViewObjectName',
'setViewConfiguration'], [],
'',
false);
79 $mockController->expects($this->once())->method(
'resolveViewObjectName')->will($this->returnValue(
false));
81 $mockController->_set(
'controllerContext', $mockControllerContext);
82 $this->assertSame($mockFluidTemplateView, $mockController->_call(
'resolveView'));
90 $mockRequest = $this->createMock(Request::class);
91 $mockRequest->expects($this->once())->method(
'getControllerVendorName')->will($this->returnValue(
'MyVendor'));
92 $mockRequest->expects($this->once())->method(
'getControllerExtensionName')->will($this->returnValue(
'MyPackage'));
93 $mockRequest->expects($this->once())->method(
'getControllerName')->will($this->returnValue(
'MyController'));
94 $mockRequest->expects($this->once())->method(
'getControllerActionName')->will($this->returnValue(
'MyAction'));
95 $mockRequest->expects($this->atLeastOnce())->method(
'getFormat')->will($this->returnValue(
'MyFormat'));
97 $mockController = $this->getAccessibleMock(ActionController::class, [
'dummy'], [],
'',
false);
98 $mockController->_set(
'request', $mockRequest);
100 $mockController->_set(
'namespacesViewObjectNamePattern',
'RandomViewObject@vendor\@extension\View\@controller\@action@format');
101 $mockController->_call(
'resolveViewObjectName');
109 eval(
'namespace MyVendor\MyPackage\View\MyController; class MyActionMyFormat {}');
111 $mockRequest = $this->createMock(Request::class);
112 $mockRequest->expects($this->once())->method(
'getControllerExtensionName')->will($this->returnValue(
'MyPackage'));
113 $mockRequest->expects($this->once())->method(
'getControllerName')->will($this->returnValue(
'MyController'));
114 $mockRequest->expects($this->once())->method(
'getControllerActionName')->will($this->returnValue(
'MyAction'));
115 $mockRequest->expects($this->once())->method(
'getControllerVendorName')->will($this->returnValue(
'MyVendor'));
116 $mockRequest->expects($this->atLeastOnce())->method(
'getFormat')->will($this->returnValue(
'MyFormat'));
118 $mockController = $this->getAccessibleMock(ActionController::class, [
'dummy'], [],
'',
false);
119 $mockController->_set(
'request', $mockRequest);
123 'MyVendor\MyPackage\View\MyController\MyActionMyFormat',
124 $mockController->_call(
'resolveViewObjectName')
133 $mockRequest = $this->createMock(Request::class);
134 $mockRequest->expects($this->once())->method(
'getControllerActionName')->will($this->returnValue(
'fooBar'));
136 $mockController = $this->getAccessibleMock(ActionController::class, [
'fooBarAction'], [],
'',
false);
137 $mockController->_set(
'request', $mockRequest);
138 $this->assertEquals(
'fooBarAction', $mockController->_call(
'resolveActionMethodName'));
146 $this->expectException(NoSuchActionException::class);
147 $this->expectExceptionCode(1186669086);
148 $mockRequest = $this->createMock(Request::class);
149 $mockRequest->expects($this->once())->method(
'getControllerActionName')->will($this->returnValue(
'fooBar'));
151 $mockController = $this->getAccessibleMock(ActionController::class, [
'otherBarAction'], [],
'',
false);
152 $mockController->_set(
'request', $mockRequest);
153 $mockController->_call(
'resolveActionMethodName');
161 $mockRequest = $this->createMock(Request::class);
162 $mockArguments = $this->getMockBuilder(Arguments::class)
163 ->setMethods([
'addNewArgument',
'removeAll'])
165 $mockArguments->expects($this->at(0))->method(
'addNewArgument')->with(
'stringArgument',
'string',
true);
166 $mockArguments->expects($this->at(1))->method(
'addNewArgument')->with(
'integerArgument',
'integer',
true);
167 $mockArguments->expects($this->at(2))->method(
'addNewArgument')->with(
'objectArgument',
'F3_Foo_Bar',
true);
168 $mockController = $this->getAccessibleMock(ActionController::class, [
'fooAction',
'evaluateDontValidateAnnotations'], [],
'',
false);
169 $methodParameters = [
170 'stringArgument' => [
172 'byReference' =>
false,
175 'allowsNull' =>
false,
177 'hasDefaultValue' => false
179 'integerArgument' => [
181 'byReference' =>
false,
184 'allowsNull' =>
false,
186 'hasDefaultValue' => false
188 'objectArgument' => [
190 'byReference' =>
false,
193 'allowsNull' =>
false,
194 'type' =>
'F3_Foo_Bar',
195 'hasDefaultValue' => false
199 $classSchemaMock = $this->createMock(ClassSchema::class);
201 ->expects($this->any())
202 ->method(
'getMethod')
205 'params' => $methodParameters
208 $mockReflectionService = $this->createMock(ReflectionService::class);
209 $mockReflectionService
210 ->expects($this->any())
211 ->method(
'getClassSchema')
212 ->with(get_class($mockController))
213 ->willReturn($classSchemaMock);
214 $mockController->_set(
'reflectionService', $mockReflectionService);
215 $mockController->_set(
'request', $mockRequest);
216 $mockController->_set(
'arguments', $mockArguments);
217 $mockController->_set(
'actionMethodName',
'fooAction');
218 $mockController->_call(
'initializeActionMethodArguments');
226 $mockRequest = $this->createMock(Request::class);
227 $mockArguments = $this->createMock(Arguments::class);
228 $mockArguments->expects($this->at(0))->method(
'addNewArgument')->with(
'arg1',
'string',
true);
229 $mockArguments->expects($this->at(1))->method(
'addNewArgument')->with(
'arg2',
'array',
false, [21]);
230 $mockArguments->expects($this->at(2))->method(
'addNewArgument')->with(
'arg3',
'string',
false, 42);
231 $mockController = $this->getAccessibleMock(ActionController::class, [
'fooAction',
'evaluateDontValidateAnnotations'], [],
'',
false);
232 $methodParameters = [
235 'byReference' =>
false,
238 'allowsNull' =>
false,
240 'hasDefaultValue' => false
244 'byReference' =>
false,
247 'defaultValue' => [21],
248 'allowsNull' =>
false,
249 'hasDefaultValue' => true
253 'byReference' =>
false,
256 'defaultValue' => 42,
257 'allowsNull' =>
false,
259 'hasDefaultValue' => true
263 $classSchemaMock = $this->createMock(ClassSchema::class);
265 ->expects($this->any())
266 ->method(
'getMethod')
269 'params' => $methodParameters
272 $mockReflectionService = $this->createMock(ReflectionService::class);
273 $mockReflectionService
274 ->expects($this->any())
275 ->method(
'getClassSchema')
276 ->with(get_class($mockController))
277 ->willReturn($classSchemaMock);
278 $mockController->_set(
'reflectionService', $mockReflectionService);
279 $mockController->_set(
'request', $mockRequest);
280 $mockController->_set(
'arguments', $mockArguments);
281 $mockController->_set(
'actionMethodName',
'fooAction');
282 $mockController->_call(
'initializeActionMethodArguments');
290 $this->expectException(InvalidArgumentTypeException::class);
291 $this->expectExceptionCode(1253175643);
292 $mockRequest = $this->createMock(Request::class);
293 $mockArguments = $this->createMock(Arguments::class);
294 $mockController = $this->getAccessibleMock(ActionController::class, [
'fooAction'], [],
'',
false);
295 $methodParameters = [
298 'byReference' =>
false,
301 'allowsNull' => false
305 $classSchemaMock = $this->createMock(ClassSchema::class);
307 ->expects($this->any())
308 ->method(
'getMethod')
311 'params' => $methodParameters
314 $mockReflectionService = $this->createMock(ReflectionService::class);
315 $mockReflectionService
316 ->expects($this->any())
317 ->method(
'getClassSchema')
318 ->with(get_class($mockController))
319 ->willReturn($classSchemaMock);
320 $mockController->_set(
'reflectionService', $mockReflectionService);
321 $mockController->_set(
'request', $mockRequest);
322 $mockController->_set(
'arguments', $mockArguments);
323 $mockController->_set(
'actionMethodName',
'fooAction');
324 $mockController->_call(
'initializeActionMethodArguments');
336 $mockController = $this->getAccessibleMock(ActionController::class, [
'dummy'], [],
'',
false);
338 $mockConfigurationManager = $this->createMock(ConfigurationManagerInterface::class);
339 $mockConfigurationManager->expects($this->any())->method(
'getConfiguration')->will($this->returnValue($configuration));
340 $mockController->injectConfigurationManager($mockConfigurationManager);
341 $mockController->_set(
'request', $this->createMock(Request::class), [
'getControllerExtensionKey']);
342 $view = $this->getMockBuilder(ViewInterface::class)
343 ->setMethods([
'setControllerContext',
'assign',
'assignMultiple',
'canRender',
'render',
'initializeView',
'setTemplateRootPaths'])
345 $view->expects($this->once())->method(
'setTemplateRootPaths')->with($expected);
346 $mockController->_call(
'setViewConfiguration', $view);
358 'templateRootPaths' => [
359 'default' =>
'some path',
360 'extended' =>
'some other path'
365 'extended' =>
'some other path',
366 'default' =>
'some path'
369 'numerical keys' => [
372 'templateRootPaths' => [
374 '20' =>
'some other path',
375 '15' =>
'intermediate specific path'
380 '20' =>
'some other path',
381 '15' =>
'intermediate specific path',
388 'templateRootPaths' => [
390 'very_specific' =>
'some other path',
391 '15' =>
'intermediate specific path'
396 '15' =>
'intermediate specific path',
397 'very_specific' =>
'some other path',
414 $mockController = $this->getAccessibleMock(ActionController::class, [
'dummy'], [],
'',
false);
416 $mockConfigurationManager = $this->createMock(ConfigurationManagerInterface::class);
417 $mockConfigurationManager->expects($this->any())->method(
'getConfiguration')->will($this->returnValue($configuration));
418 $mockController->injectConfigurationManager($mockConfigurationManager);
419 $mockController->_set(
'request', $this->createMock(Request::class), [
'getControllerExtensionKey']);
420 $view = $this->getMockBuilder(ViewInterface::class)
421 ->setMethods([
'setControllerContext',
'assign',
'assignMultiple',
'canRender',
'render',
'initializeView',
'setlayoutRootPaths'])
423 $view->expects($this->once())->method(
'setlayoutRootPaths')->with($expected);
424 $mockController->_call(
'setViewConfiguration', $view);
436 'layoutRootPaths' => [
437 'default' =>
'some path',
438 'extended' =>
'some other path'
443 'extended' =>
'some other path',
444 'default' =>
'some path'
447 'numerical keys' => [
450 'layoutRootPaths' => [
452 '20' =>
'some other path',
453 '15' =>
'intermediate specific path'
458 '20' =>
'some other path',
459 '15' =>
'intermediate specific path',
466 'layoutRootPaths' => [
468 'very_specific' =>
'some other path',
469 '15' =>
'intermediate specific path'
474 '15' =>
'intermediate specific path',
475 'very_specific' =>
'some other path',
492 $mockController = $this->getAccessibleMock(ActionController::class, [
'dummy'], [],
'',
false);
494 $mockConfigurationManager = $this->createMock(ConfigurationManagerInterface::class);
495 $mockConfigurationManager->expects($this->any())->method(
'getConfiguration')->will($this->returnValue($configuration));
496 $mockController->injectConfigurationManager($mockConfigurationManager);
497 $mockController->_set(
'request', $this->createMock(Request::class), [
'getControllerExtensionKey']);
498 $view = $this->getMockBuilder(ViewInterface::class)
499 ->setMethods([
'setControllerContext',
'assign',
'assignMultiple',
'canRender',
'render',
'initializeView',
'setpartialRootPaths'])
501 $view->expects($this->once())->method(
'setpartialRootPaths')->with($expected);
502 $mockController->_call(
'setViewConfiguration', $view);
514 'partialRootPaths' => [
515 'default' =>
'some path',
516 'extended' =>
'some other path'
521 'extended' =>
'some other path',
522 'default' =>
'some path'
525 'numerical keys' => [
528 'partialRootPaths' => [
530 '20' =>
'some other path',
531 '15' =>
'intermediate specific path'
536 '20' =>
'some other path',
537 '15' =>
'intermediate specific path',
544 'partialRootPaths' => [
546 'very_specific' =>
'some other path',
547 '15' =>
'intermediate specific path'
552 '15' =>
'intermediate specific path',
553 'very_specific' =>
'some other path',
569 $this->mockObjectManager = $this->createMock(ObjectManager::class);
570 $pageRendererMock = $this->getMockBuilder(PageRenderer::class)->setMethods([
'addHeaderData',
'addFooterData'])->getMock();
571 if (!$viewMock instanceof FluidTemplateView) {
572 $this->mockObjectManager->expects($this->never())->method(
'get');
574 $this->mockObjectManager->expects($this->any())->method(
'get')->with(PageRenderer::class)->willReturn($pageRendererMock);
576 if (!empty(trim($expectedHeader ??
''))) {
577 $pageRendererMock->expects($this->once())->method(
'addHeaderData')->with($expectedHeader);
579 $pageRendererMock->expects($this->never())->method(
'addHeaderData');
581 if (!empty(trim($expectedFooter ??
''))) {
582 $pageRendererMock->expects($this->once())->method(
'addFooterData')->with($expectedFooter);
584 $pageRendererMock->expects($this->never())->method(
'addFooterData');
586 $requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass();
587 $subject =
new ActionController();
588 $viewProperty = new \ReflectionProperty($subject,
'view');
589 $viewProperty->setAccessible(
true);
590 $viewProperty->setValue($subject, $viewMock);
591 $objectManagerProperty = new \ReflectionProperty($subject,
'objectManager');
592 $objectManagerProperty->setAccessible(
true);
593 $objectManagerProperty->setValue($subject, $this->mockObjectManager);
595 $method = new \ReflectionMethod($subject,
'renderAssetsForRequest');
596 $method->setAccessible(
true);
597 $method->invokeArgs($subject, [$requestMock]);
605 $viewWithHeaderData = $this->getMockBuilder(FluidTemplateView::class)->setMethods([
'renderSection'])->disableOriginalConstructor()->getMock();
606 $viewWithHeaderData->expects($this->at(0))->method(
'renderSection')->with(
'HeaderAssets', $this->anything(),
true)->willReturn(
'custom-header-data');
607 $viewWithHeaderData->expects($this->at(1))->method(
'renderSection')->with(
'FooterAssets', $this->anything(),
true)->willReturn(
null);
608 $viewWithFooterData = $this->getMockBuilder(FluidTemplateView::class)->setMethods([
'renderSection'])->disableOriginalConstructor()->getMock();
609 $viewWithFooterData->expects($this->at(0))->method(
'renderSection')->with(
'HeaderAssets', $this->anything(),
true)->willReturn(
null);
610 $viewWithFooterData->expects($this->at(1))->method(
'renderSection')->with(
'FooterAssets', $this->anything(),
true)->willReturn(
'custom-footer-data');
611 $viewWithBothData = $this->getMockBuilder(FluidTemplateView::class)->setMethods([
'renderSection'])->disableOriginalConstructor()->getMock();
612 $viewWithBothData->expects($this->at(0))->method(
'renderSection')->with(
'HeaderAssets', $this->anything(),
true)->willReturn(
'custom-header-data');
613 $viewWithBothData->expects($this->at(1))->method(
'renderSection')->with(
'FooterAssets', $this->anything(),
true)->willReturn(
'custom-footer-data');
614 $invalidView = $this->getMockBuilder(AbstractTemplateView::class)->disableOriginalConstructor()->getMockForAbstractClass();
616 [$viewWithHeaderData,
'custom-header-data',
null],
617 [$viewWithFooterData,
null,
'custom-footer-data'],
618 [$viewWithBothData,
'custom-header-data',
'custom-footer-data'],
619 [$invalidView,
null,
null]