From 58ae790fe80c2e2fd4c73cf3c447a03009c762f1 Mon Sep 17 00:00:00 2001 From: mhsdesign <85400359+mhsdesign@users.noreply.github.com> Date: Tue, 30 Jan 2024 01:00:40 +0100 Subject: [PATCH] TASK: Avoid tests writing unnecessary state into controller --- .../Mvc/Controller/ActionControllerTest.php | 51 ++++++------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/Neos.Flow/Tests/Unit/Mvc/Controller/ActionControllerTest.php b/Neos.Flow/Tests/Unit/Mvc/Controller/ActionControllerTest.php index 16a4378948..959d754d97 100644 --- a/Neos.Flow/Tests/Unit/Mvc/Controller/ActionControllerTest.php +++ b/Neos.Flow/Tests/Unit/Mvc/Controller/ActionControllerTest.php @@ -64,14 +64,10 @@ protected function setUp(): void $this->mockRequest->expects(self::any())->method('getFormat')->will(self::returnValue('theFormat')); $this->mockRequest->expects(self::any())->method('getControllerName')->will(self::returnValue('TheController')); $this->mockRequest->expects(self::any())->method('getControllerActionName')->will(self::returnValue('theAction')); - $this->inject($this->actionController, 'request', $this->mockRequest); $this->mockObjectManager = $this->createMock(ObjectManagerInterface::class); $this->inject($this->actionController, 'objectManager', $this->mockObjectManager); - $this->mockControllerContext = $this->getMockBuilder(Mvc\Controller\ControllerContext::class)->disableOriginalConstructor()->getMock(); - $this->inject($this->actionController, 'controllerContext', $this->mockControllerContext); - $this->mockViewConfigurationManager = $this->createMock(Mvc\ViewConfigurationManager::class); $this->inject($this->actionController, 'viewConfigurationManager', $this->mockViewConfigurationManager); } @@ -143,19 +139,14 @@ public function processRequestThrowsExceptionIfRequestedActionIsNotCallable() $this->actionController = new ActionController(); $this->inject($this->actionController, 'objectManager', $this->mockObjectManager); - $this->inject($this->actionController, 'controllerContext', $this->mockControllerContext); $mockRequest = $this->getMockBuilder(Mvc\ActionRequest::class)->disableOriginalConstructor()->getMock(); $mockRequest->expects(self::any())->method('getControllerActionName')->will(self::returnValue('nonExisting')); - $this->inject($this->actionController, 'arguments', new Arguments([])); - - $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock(); + $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); $mockRequest->expects(self::any())->method('getHttpRequest')->will(self::returnValue($mockHttpRequest)); - $mockResponse = new Mvc\ActionResponse; - - $this->actionController->processRequest($mockRequest, $mockResponse); + $this->actionController->processRequest($mockRequest); } /** @@ -167,8 +158,6 @@ public function processRequestThrowsExceptionIfRequestedActionIsNotPublic() $this->actionController = new ActionController(); $this->inject($this->actionController, 'objectManager', $this->mockObjectManager); - $this->inject($this->actionController, 'controllerContext', $this->mockControllerContext); - $this->inject($this->actionController, 'arguments', new Arguments([])); $mockRequest = $this->getMockBuilder(Mvc\ActionRequest::class)->disableOriginalConstructor()->getMock(); $mockRequest->expects(self::any())->method('getControllerActionName')->will(self::returnValue('initialize')); @@ -190,12 +179,10 @@ public function processRequestThrowsExceptionIfRequestedActionIsNotPublic() return $this->createMock($classname); })); - $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock(); + $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); $mockRequest->expects(self::any())->method('getHttpRequest')->will(self::returnValue($mockHttpRequest)); - $mockResponse = new Mvc\ActionResponse; - - $this->actionController->processRequest($mockRequest, $mockResponse); + $this->actionController->processRequest($mockRequest); } /** @@ -203,27 +190,24 @@ public function processRequestThrowsExceptionIfRequestedActionIsNotPublic() */ public function processRequestInjectsControllerContextToView() { - $this->actionController = $this->getAccessibleMock(ActionController::class, ['resolveActionMethodName', 'initializeActionMethodArguments', 'initializeActionMethodValidators', 'resolveView', 'callActionMethod', 'initializeController']); + $this->actionController = $this->getAccessibleMock(ActionController::class, ['resolveActionMethodName', 'initializeActionMethodArguments', 'initializeActionMethodValidators', 'resolveView', 'callActionMethod']); $this->actionController->method('resolveActionMethodName')->willReturn('indexAction'); - $this->inject($this->actionController, 'objectManager', $this->mockObjectManager); - $this->inject($this->actionController, 'controllerContext', $this->mockControllerContext); - $this->inject($this->actionController, 'request', $this->mockRequest); + $mockResponse = new Mvc\ActionResponse; + $mockResponse->setContentType('text/plain'); - $this->inject($this->actionController, 'arguments', new Arguments([])); + $this->inject($this->actionController, 'objectManager', $this->mockObjectManager); $mockMvcPropertyMappingConfigurationService = $this->createMock(Mvc\Controller\MvcPropertyMappingConfigurationService::class); $this->inject($this->actionController, 'mvcPropertyMappingConfigurationService', $mockMvcPropertyMappingConfigurationService); - $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock(); + $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); $this->mockRequest->expects(self::any())->method('getHttpRequest')->will(self::returnValue($mockHttpRequest)); - $mockResponse = new Mvc\ActionResponse; - $mockResponse->setContentType('text/plain'); - $this->inject($this->actionController, 'response', $mockResponse); - $mockView = $this->createMock(Mvc\View\ViewInterface::class); - $mockView->expects(self::once())->method('setControllerContext')->with($this->mockControllerContext); + $mockView->expects(self::once())->method('setControllerContext')->with(self::callback( + fn(Mvc\Controller\ControllerContext $controllerContext) => $controllerContext->getRequest() === $this->mockRequest + )); $this->actionController->expects(self::once())->method('resolveView')->with($this->mockRequest)->will(self::returnValue($mockView)); $this->actionController->expects(self::once())->method('callActionMethod')->willReturn($mockResponse); $this->actionController->expects(self::once())->method('resolveActionMethodName')->with($this->mockRequest)->will(self::returnValue('someAction')); @@ -240,7 +224,6 @@ public function processRequestInjectsSettingsToView() $this->actionController->method('resolveActionMethodName')->willReturn('indexAction'); $this->inject($this->actionController, 'objectManager', $this->mockObjectManager); - $this->inject($this->actionController, 'controllerContext', $this->mockControllerContext); $mockSettings = ['foo', 'bar']; $this->inject($this->actionController, 'settings', $mockSettings); @@ -248,7 +231,7 @@ public function processRequestInjectsSettingsToView() $mockMvcPropertyMappingConfigurationService = $this->createMock(Mvc\Controller\MvcPropertyMappingConfigurationService::class); $this->inject($this->actionController, 'mvcPropertyMappingConfigurationService', $mockMvcPropertyMappingConfigurationService); - $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock(); + $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); $this->mockRequest->expects(self::any())->method('getHttpRequest')->will(self::returnValue($mockHttpRequest)); $mockResponse = new Mvc\ActionResponse(); @@ -285,7 +268,7 @@ public function processRequestSetsNegotiatedContentTypeOnResponse($supportedMedi $mockMvcPropertyMappingConfigurationService = $this->createMock(Mvc\Controller\MvcPropertyMappingConfigurationService::class); $this->inject($this->actionController, 'mvcPropertyMappingConfigurationService', $mockMvcPropertyMappingConfigurationService); - $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock(); + $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); $mockHttpRequest->method('getHeaderLine')->with('Accept')->willReturn($acceptHeader); $this->mockRequest->method('getHttpRequest')->willReturn($mockHttpRequest); @@ -317,7 +300,7 @@ public function processRequestUsesContentTypeFromActionResponse($supportedMediaT $mockMvcPropertyMappingConfigurationService = $this->createMock(Mvc\Controller\MvcPropertyMappingConfigurationService::class); $this->inject($this->actionController, 'mvcPropertyMappingConfigurationService', $mockMvcPropertyMappingConfigurationService); - $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock(); + $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); $mockHttpRequest->method('getHeaderLine')->with('Accept')->willReturn('application/xml'); $this->mockRequest->method('getHttpRequest')->willReturn($mockHttpRequest); @@ -342,13 +325,11 @@ public function processRequestUsesContentTypeFromRenderedView($supportedMediaTyp $mockMvcPropertyMappingConfigurationService = $this->createMock(Mvc\Controller\MvcPropertyMappingConfigurationService::class); $this->inject($this->actionController, 'mvcPropertyMappingConfigurationService', $mockMvcPropertyMappingConfigurationService); - $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->disableOriginalConstructor()->getMock(); + $mockHttpRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); $mockHttpRequest->method('getHeaderLine')->with('Accept')->willReturn('application/xml'); $mockHttpRequest->method('getHeaderLine')->with('Accept')->willReturn('application/xml'); $this->mockRequest->method('getHttpRequest')->willReturn($mockHttpRequest); - $mockResponse = new Mvc\ActionResponse; - $this->inject($this->actionController, 'supportedMediaTypes', ['application/xml']); $mockView = $this->createMock(Mvc\View\ViewInterface::class);