|
TYPO3 API
SVNRelease
|
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_Web_RequestBuilderTest extends Tx_Extbase_Tests_Unit_BaseTestCase { 00029 00030 /** 00031 * @var Tx_Extbase_MVC_Web_RequestBuilder 00032 */ 00033 protected $requestBuilder; 00034 00035 /** 00036 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface 00037 */ 00038 protected $mockConfigurationManager; 00039 00040 /** 00041 * @var array 00042 */ 00043 protected $configuration; 00044 00045 /** 00046 * @var Tx_Extbase_Object_ObjectManagerInterface 00047 */ 00048 protected $mockObjectManager; 00049 00050 /** 00051 * @var Tx_Extbase_MVC_Web_Request 00052 */ 00053 protected $mockRequest; 00054 00055 /** 00056 * @var array 00057 */ 00058 protected $getBackup = array(); 00059 00060 /** 00061 * @var array 00062 */ 00063 protected $postBackup = array(); 00064 00065 /** 00066 * @var array 00067 */ 00068 protected $serverBackup = array(); 00069 00070 public function setUp() { 00071 $this->requestBuilder = $this->getAccessibleMock('Tx_Extbase_MVC_Web_RequestBuilder', array('dummy')); 00072 $this->configuration = array( 00073 'userFunc' => 'Tx_Extbase_Dispatcher->dispatch', 00074 'pluginName' => 'Pi1', 00075 'extensionName' => 'MyExtension', 00076 'controller' => 'TheFirstController', 00077 'action' => 'show', 00078 'controllerConfiguration' => array( 00079 'TheFirstController' => array( 00080 'actions' => array('show', 'index', 'new', 'create', 'delete', 'edit', 'update', 'setup', 'test') 00081 ), 00082 'TheSecondController' => array( 00083 'actions' => array('show', 'index') 00084 ), 00085 'TheThirdController' => array( 00086 'actions' => array('delete', 'create') 00087 ) 00088 ) 00089 ); 00090 $this->mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface'); 00091 $this->mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request'); 00092 $this->mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface'); 00093 00094 $this->getBackup = $_GET; 00095 $this->postBackup = $_POST; 00096 $this->serverBackup = $_SERVER; 00097 } 00098 00099 /** 00100 * @return void 00101 */ 00102 protected function injectDependencies() { 00103 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration)); 00104 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager); 00105 00106 $this->mockObjectManager->expects($this->any())->method('create')->with('Tx_Extbase_MVC_Web_Request')->will($this->returnValue($this->mockRequest)); 00107 $this->requestBuilder->injectObjectManager($this->mockObjectManager); 00108 } 00109 00110 public function tearDown() { 00111 $_GET = $this->getBackup; 00112 $_POST = $this->postBackup; 00113 $_SERVER = $this->serverBackup; 00114 } 00115 00116 /** 00117 * @test 00118 */ 00119 public function buildReturnsAWebRequestObject() { 00120 $this->injectDependencies(); 00121 $request = $this->requestBuilder->build(); 00122 $this->assertSame($this->mockRequest, $request); 00123 } 00124 00125 /** 00126 * @test 00127 */ 00128 public function buildSetsRequestPluginName() { 00129 $this->injectDependencies(); 00130 $this->mockRequest->expects($this->once())->method('setPluginName')->with('Pi1'); 00131 $this->requestBuilder->build(); 00132 } 00133 00134 /** 00135 * @test 00136 */ 00137 public function buildSetsRequestControllerExtensionName() { 00138 $this->injectDependencies(); 00139 $this->mockRequest->expects($this->once())->method('setControllerExtensionName')->with('MyExtension'); 00140 $this->requestBuilder->build(); 00141 } 00142 00143 /** 00144 * @test 00145 */ 00146 public function buildSetsRequestControllerName() { 00147 $this->injectDependencies(); 00148 $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController'); 00149 $this->requestBuilder->build(); 00150 } 00151 00152 /** 00153 * @test 00154 */ 00155 public function buildSetsRequestControllerActionName() { 00156 $this->injectDependencies(); 00157 $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('show'); 00158 $this->requestBuilder->build(); 00159 } 00160 00161 /** 00162 * @test 00163 */ 00164 public function buildSetsRequestRequestURI() { 00165 $this->injectDependencies(); 00166 $expectedRequestUri = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'); 00167 $this->mockRequest->expects($this->once())->method('setRequestURI')->with($expectedRequestUri); 00168 $this->requestBuilder->build(); 00169 } 00170 00171 /** 00172 * @test 00173 */ 00174 public function buildSetsRequestBaseURI() { 00175 $this->injectDependencies(); 00176 $expectedBaseUri = t3lib_div::getIndpEnv('TYPO3_SITE_URL'); 00177 $this->mockRequest->expects($this->once())->method('setBaseURI')->with($expectedBaseUri); 00178 $this->requestBuilder->build(); 00179 } 00180 00181 /** 00182 * @test 00183 */ 00184 public function buildSetsRequestMethod() { 00185 $this->injectDependencies(); 00186 $_SERVER['REQUEST_METHOD'] = 'SomeRequestMethod'; 00187 $expectedMethod = 'SomeRequestMethod'; 00188 $this->mockRequest->expects($this->once())->method('setMethod')->with($expectedMethod); 00189 $this->requestBuilder->build(); 00190 } 00191 00192 00193 /** 00194 * @test 00195 * @expectedException Tx_Extbase_MVC_Exception 00196 */ 00197 public function buildThrowsExceptionIfExtensionNameIsNotConfigured() { 00198 unset($this->configuration['extensionName']); 00199 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface'); 00200 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration)); 00201 $this->requestBuilder->injectConfigurationManager($mockConfigurationManager); 00202 $this->requestBuilder->build(); 00203 } 00204 00205 /** 00206 * @test 00207 * @expectedException Tx_Extbase_MVC_Exception 00208 */ 00209 public function buildThrowsExceptionIfPluginNameIsNotConfigured() { 00210 unset($this->configuration['pluginName']); 00211 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface'); 00212 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration)); 00213 $this->requestBuilder->injectConfigurationManager($mockConfigurationManager); 00214 $this->requestBuilder->build(); 00215 } 00216 00217 /** 00218 * @test 00219 * @expectedException Tx_Extbase_MVC_Exception 00220 */ 00221 public function buildThrowsExceptionIfControllerConfigurationIsEmptyOrNotSet() { 00222 unset($this->configuration['controllerConfiguration']); 00223 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration)); 00224 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager); 00225 $this->requestBuilder->build(); 00226 } 00227 00228 /** 00229 * @test 00230 * @expectedException Tx_Extbase_MVC_Exception 00231 */ 00232 public function buildThrowsExceptionIfControllerConfigurationHasNoDefaultActionDefined() { 00233 $this->configuration['controllerConfiguration']['TheFirstController'] = array(); 00234 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration)); 00235 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager); 00236 $this->requestBuilder->build(); 00237 } 00238 00239 /** 00240 * @test 00241 */ 00242 public function buildSetsParametersFromGetAndPostVariables() { 00243 $this->configuration['extensionName'] = 'SomeExtensionName'; 00244 $this->configuration['pluginName'] = 'SomePluginName'; 00245 $this->injectDependencies(); 00246 00247 $_GET = array( 00248 'tx_someotherextensionname_somepluginname' => array( 00249 'foo' => 'bar' 00250 ), 00251 'tx_someextensionname_somepluginname' => array( 00252 'parameter1' => 'valueGetsOverwritten', 00253 'parameter2' => array( 00254 'parameter3' => 'value3' 00255 ) 00256 ) 00257 ); 00258 $_POST = array( 00259 'tx_someextensionname_someotherpluginname' => array( 00260 'foo' => 'bar' 00261 ), 00262 'tx_someextensionname_somepluginname' => array( 00263 'parameter1' => 'value1', 00264 'parameter2' => array( 00265 'parameter4' => 'value4' 00266 ) 00267 ) 00268 ); 00269 $this->mockRequest->expects($this->at(7))->method('setArgument')->with('parameter1', 'value1'); 00270 $this->mockRequest->expects($this->at(8))->method('setArgument')->with('parameter2', array('parameter3' => 'value3', 'parameter4' => 'value4')); 00271 00272 $this->requestBuilder->build(); 00273 } 00274 00275 /** 00276 * @test 00277 */ 00278 public function buildSetsFormatFromGetAndPostVariables() { 00279 $this->configuration['extensionName'] = 'SomeExtensionName'; 00280 $this->configuration['pluginName'] = 'SomePluginName'; 00281 $this->injectDependencies(); 00282 00283 $_GET = array( 00284 'tx_someextensionname_somepluginname' => array( 00285 'format' => 'GET', 00286 ) 00287 ); 00288 $_POST = array( 00289 'tx_someextensionname_somepluginname' => array( 00290 'format' => 'POST', 00291 ) 00292 ); 00293 $this->mockRequest->expects($this->at(7))->method('setFormat')->with('POST'); 00294 00295 $this->requestBuilder->build(); 00296 } 00297 00298 /** 00299 * @test 00300 */ 00301 public function buildCorrectlySetsAllowedControllerActions() { 00302 $this->injectDependencies(); 00303 $expectedResult = array( 00304 'TheFirstController' => array( 00305 'show', 'index', 'new', 'create', 'delete', 'edit', 'update', 'setup', 'test' 00306 ), 00307 'TheSecondController' => array( 00308 'show', 'index' 00309 ), 00310 'TheThirdController' => array( 00311 'delete', 'create' 00312 ) 00313 ); 00314 $this->requestBuilder->build(); 00315 $actualResult = $this->requestBuilder->_get('allowedControllerActions'); 00316 $this->assertEquals($expectedResult, $actualResult); 00317 } 00318 } 00319 ?>
1.8.0