Skip to content

Commit

Permalink
TASK: Avoid tests writing unnecessary state into controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Jan 30, 2024
1 parent 66dc876 commit 58ae790
Showing 1 changed file with 16 additions and 35 deletions.
51 changes: 16 additions & 35 deletions Neos.Flow/Tests/Unit/Mvc/Controller/ActionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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'));
Expand All @@ -190,40 +179,35 @@ 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);
}

/**
* @test
*/
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'));
Expand All @@ -240,15 +224,14 @@ 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);

$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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down

0 comments on commit 58ae790

Please sign in to comment.