TYPO3 API  SVNRelease
AbstractControllerTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 class Tx_Extbase_Tests_Unit_MVC_Controller_AbstractControllerTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
00029 
00030     /**
00031      * @test
00032      */
00033     public function theExtensionNameIsInitialized() {
00034         $extensionName = uniqid('Test');
00035         $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('initializeObjects'), array(), 'Tx_' . $extensionName . '_Controller');
00036         $this->assertSame($extensionName, $controller->_get('extensionName'));
00037     }
00038 
00039     /**
00040      * @test
00041      * @expectedException Tx_Extbase_MVC_Exception_UnsupportedRequestType
00042      */
00043     public function processRequestWillThrowAnExceptionIfTheGivenRequestIsNotSupported() {
00044         $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
00045         $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
00046 
00047         $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('mapRequestArgumentsToControllerArguments'), array(), '', FALSE);
00048         $controller->_set('supportedRequestTypes', array('Tx_Something_Request'));
00049         $controller->processRequest($mockRequest, $mockResponse);
00050     }
00051 
00052     /**
00053      * @test
00054      */
00055     public function processRequestSetsTheDispatchedFlagOfTheRequest() {
00056         $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
00057         $mockRequest->expects($this->once())->method('setDispatched')->with(TRUE);
00058 
00059         $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
00060 
00061         $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
00062         $mockObjectManager->expects($this->once())->method('create')
00063             ->with('Tx_Extbase_MVC_Web_Routing_UriBuilder')
00064             ->will($this->returnValue($this->getMock('Tx_Extbase_MVC_Web_Routing_UriBuilder')));
00065 
00066         $controller = $this->getAccessibleMock(
00067             'Tx_Extbase_MVC_Controller_AbstractController',
00068             array('initializeArguments', 'initializeControllerArgumentsBaseValidators', 'mapRequestArgumentsToControllerArguments', 'buildControllerContext'),
00069             array(), '', FALSE
00070         );
00071         $controller->_set('objectManager', $mockObjectManager);
00072 
00073         $controller->processRequest($mockRequest, $mockResponse);
00074     }
00075 
00076     /**
00077      * @test
00078      * @expectedException Tx_Extbase_MVC_Exception_StopAction
00079      */
00080     public function forwardThrowsAStopActionException() {
00081         $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
00082         $mockRequest->expects($this->once())->method('setDispatched')->with(FALSE);
00083         $mockRequest->expects($this->once())->method('setControllerActionName')->with('foo');
00084 
00085         $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
00086         $controller->_set('request', $mockRequest);
00087         $controller->_call('forward', 'foo');
00088     }
00089 
00090     /**
00091      * @test
00092      * @expectedException Tx_Extbase_MVC_Exception_StopAction
00093      */
00094     public function forwardSetsControllerAndArgumentsAtTheRequestObjectIfTheyAreSpecified() {
00095         $arguments = array('foo' => 'bar');
00096 
00097         $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
00098         $mockRequest->expects($this->once())->method('setControllerActionName')->with('foo');
00099         $mockRequest->expects($this->once())->method('setControllerName')->with('Bar');
00100         $mockRequest->expects($this->once())->method('setControllerExtensionName')->with('Baz');
00101         $mockRequest->expects($this->once())->method('setArguments')->with($arguments);
00102 
00103         $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
00104         $controller->_set('request', $mockRequest);
00105         $controller->_call('forward', 'foo', 'Bar', 'Baz', $arguments);
00106     }
00107 
00108     /**
00109      * @test
00110      */
00111     public function redirectRedirectsToTheSpecifiedAction() {
00112         $arguments = array('foo' => 'bar');
00113 
00114         $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
00115         $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
00116 
00117         $mockUriBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_UriBuilder');
00118         $mockUriBuilder->expects($this->once())->method('reset')->will($this->returnValue($mockUriBuilder));
00119         $mockUriBuilder->expects($this->once())->method('setTargetPageUid')->with(123)->will($this->returnValue($mockUriBuilder));
00120         $mockUriBuilder->expects($this->once())->method('uriFor')->with('theActionName', $arguments, 'TheControllerName', 'TheExtensionName')->will($this->returnValue('the uri'));
00121 
00122         $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('redirectToURI'), array(), '', FALSE);
00123         $controller->expects($this->once())->method('redirectToURI')->with('the uri');
00124         $controller->_set('request', $mockRequest);
00125         $controller->_set('response', $mockResponse);
00126         $controller->_set('uriBuilder', $mockUriBuilder);
00127         $controller->_call('redirect', 'theActionName', 'TheControllerName', 'TheExtensionName', $arguments, 123);
00128     }
00129 
00130     /**
00131      * @test
00132      */
00133     public function theBaseUriIsAddedIfNotAlreadyExists() {
00134         $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
00135         $mockRequest->expects($this->any())->method('getBaseURI')->will($this->returnValue('http://www.example.com/foo/'));
00136 
00137         $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
00138         $controller->_set('request', $mockRequest);
00139         $actualResult = $controller->_call('addBaseUriIfNecessary', 'bar/baz/boom.html');
00140         $expectedResult = 'http://www.example.com/foo/bar/baz/boom.html';
00141 
00142         $this->assertEquals($expectedResult, $actualResult);
00143     }
00144 
00145     /**
00146      * @test
00147      */
00148     public function theBaseUriIsNotAddedIfAlreadyExists() {
00149         $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
00150         $mockRequest->expects($this->any())->method('getBaseURI')->will($this->returnValue('http://www.example.com/foo/'));
00151 
00152         $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
00153         $controller->_set('request', $mockRequest);
00154         $actualResult = $controller->_call('addBaseUriIfNecessary', 'http://www.example.com/foo/bar/baz/boom.html');
00155         $expectedResult = 'http://www.example.com/foo/bar/baz/boom.html';
00156 
00157         $this->assertEquals($expectedResult, $actualResult);
00158     }
00159 
00160     /**
00161      * @test
00162      * @expectedException Tx_Extbase_MVC_Exception_StopAction
00163      */
00164     public function throwStatusSetsTheSpecifiedStatusHeaderAndStopsTheCurrentAction() {
00165         $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
00166         $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
00167         $mockResponse->expects($this->once())->method('setStatus')->with(404, 'File Really Not Found');
00168         $mockResponse->expects($this->once())->method('setContent')->with('<h1>All wrong!</h1><p>Sorry, the file does not exist.</p>');
00169 
00170         $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
00171         $controller->_set('request', $mockRequest);
00172         $controller->_set('response', $mockResponse);
00173         $controller->_call('throwStatus', 404, 'File Really Not Found', '<h1>All wrong!</h1><p>Sorry, the file does not exist.</p>');
00174     }
00175 
00176     /**
00177      * @test
00178      */
00179     public function initializeControllerArgumentsBaseValidatorsRegistersValidatorsDeclaredInTheArgumentModels() {
00180         $mockValidators = array(
00181             'foo' => $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface'),
00182         );
00183 
00184         $mockValidatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array(), array(), '', FALSE);
00185         $mockValidatorResolver->expects($this->at(0))->method('getBaseValidatorConjunction')->with('FooType')->will($this->returnValue($mockValidators['foo']));
00186         $mockValidatorResolver->expects($this->at(1))->method('getBaseValidatorConjunction')->with('BarType')->will($this->returnValue(NULL));
00187 
00188         $mockArgumentFoo = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('foo'), '', FALSE);
00189         $mockArgumentFoo->expects($this->once())->method('getDataType')->will($this->returnValue('FooType'));
00190         $mockArgumentFoo->expects($this->once())->method('setValidator')->with($mockValidators['foo']);
00191 
00192         $mockArgumentBar = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('bar'), '', FALSE);
00193         $mockArgumentBar->expects($this->once())->method('getDataType')->will($this->returnValue('BarType'));
00194         $mockArgumentBar->expects($this->never())->method('setValidator');
00195 
00196         $mockArguments = new Tx_Extbase_MVC_Controller_Arguments();
00197         $mockArguments->addArgument($mockArgumentFoo);
00198         $mockArguments->addArgument($mockArgumentBar);
00199 
00200         $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
00201         $controller->_set('arguments', $mockArguments);
00202         $controller->injectValidatorResolver($mockValidatorResolver);
00203         $controller->_call('initializeControllerArgumentsBaseValidators');
00204     }
00205 
00206     /**
00207      * @test
00208      */
00209     public function mapRequestArgumentsToControllerArgumentsPreparesInformationAndValidatorsAndMapsAndValidates() {
00210         $mockValidator = $this->getMock('Tx_Extbase_MVC_Controller_ArgumentsValidator');
00211         $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
00212         $mockObjectManager->expects($this->once())->method('create')
00213             ->with('Tx_Extbase_MVC_Controller_ArgumentsValidator')
00214             ->will($this->returnValue($mockValidator));
00215 
00216         $mockArgumentFoo = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('foo'), '', FALSE);
00217         $mockArgumentFoo->expects($this->any())->method('getName')->will($this->returnValue('foo'));
00218         $mockArgumentBar = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('bar'), '', FALSE);
00219         $mockArgumentBar->expects($this->any())->method('getName')->will($this->returnValue('bar'));
00220 
00221         $mockArguments = new Tx_Extbase_MVC_Controller_Arguments();
00222         $mockArguments->addArgument($mockArgumentFoo);
00223         $mockArguments->addArgument($mockArgumentBar);
00224 
00225         $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
00226         $mockRequest->expects($this->once())->method('getArguments')->will($this->returnValue(array('requestFoo', 'requestBar')));
00227 
00228         $mockMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults');
00229 
00230         $mockPropertyMapper = $this->getMock('Tx_Extbase_Property_Mapper', array(), array(), '', FALSE);
00231         $mockPropertyMapper->expects($this->once())->method('mapAndValidate')
00232             ->with(array('foo', 'bar'), array('requestFoo', 'requestBar'), $mockArguments, array(), $mockValidator)
00233             ->will($this->returnValue(TRUE));
00234         $mockPropertyMapper->expects($this->once())->method('getMappingResults')->will($this->returnValue($mockMappingResults));
00235 
00236         $controller = $this->getAccessibleMock(
00237             'Tx_Extbase_MVC_Controller_AbstractController',
00238             array('dummy'), array(), '', FALSE
00239         );
00240 
00241         $controller->_set('arguments', $mockArguments);
00242         $controller->_set('request', $mockRequest);
00243         $controller->_set('propertyMapper', $mockPropertyMapper);
00244         $controller->_set('objectManager', $mockObjectManager);
00245 
00246         $controller->_call('mapRequestArgumentsToControllerArguments');
00247 
00248         $this->assertSame($mockMappingResults, $controller->_get('argumentsMappingResults'));
00249     }
00250 }
00251 ?>