|
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_Controller_ArgumentsTest extends Tx_Extbase_Tests_Unit_BaseTestCase { 00029 00030 /** 00031 * @test 00032 */ 00033 public function argumentsObjectIsOfScopePrototype() { 00034 $arguments1 = new Tx_Extbase_MVC_Controller_Arguments(); 00035 $arguments2 = new Tx_Extbase_MVC_Controller_Arguments(); 00036 $this->assertNotSame($arguments1, $arguments2, 'The arguments object is not of scope prototype!'); 00037 } 00038 00039 /** 00040 * @test 00041 */ 00042 public function addingAnArgumentManuallyWorks() { 00043 $arguments = new Tx_Extbase_MVC_Controller_Arguments(); 00044 $newArgument = new Tx_Extbase_MVC_Controller_Argument('argumentName1234', 'dummyValue'); 00045 00046 $arguments->addArgument($newArgument); 00047 $this->assertSame($newArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.'); 00048 } 00049 00050 /** 00051 * @test 00052 */ 00053 public function addingAnArgumentReplacesArgumentWithSameName() { 00054 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00055 00056 $mockFirstArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE); 00057 $mockFirstArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234')); 00058 $arguments->addArgument($mockFirstArgument); 00059 00060 $mockSecondArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE); 00061 $mockSecondArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234')); 00062 $arguments->addArgument($mockSecondArgument); 00063 00064 $this->assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.'); 00065 } 00066 00067 /** 00068 * @test 00069 */ 00070 public function addNewArgumentProvidesFluentInterface() { 00071 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array(), '', FALSE); 00072 00073 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface'); 00074 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Controller_Argument')->will($this->returnValue($mockArgument)); 00075 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00076 $arguments->injectObjectManager($mockObjectManager); 00077 00078 $newArgument = $arguments->addNewArgument('someArgument'); 00079 $this->assertSame($newArgument, $mockArgument); 00080 } 00081 00082 /** 00083 * @test 00084 */ 00085 public function addingArgumentThroughArrayAccessWorks() { 00086 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE); 00087 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234')); 00088 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00089 00090 $arguments[] = $mockArgument; 00091 $this->assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.'); 00092 $this->assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.'); 00093 } 00094 00095 /** 00096 * @test 00097 */ 00098 public function retrievingArgumentThroughArrayAccessWorks() { 00099 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE); 00100 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234')); 00101 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00102 00103 $arguments[] = $mockArgument; 00104 $this->assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.'); 00105 } 00106 00107 /** 00108 * @test 00109 */ 00110 public function getArgumentWithNonExistingArgumentNameThrowsException() { 00111 $arguments = new Tx_Extbase_MVC_Controller_Arguments(); 00112 try { 00113 $arguments->getArgument('someArgument'); 00114 $this->fail('getArgument() did not throw an exception although the specified argument does not exist.'); 00115 } catch (Tx_Extbase_MVC_Exception_NoSuchArgument $exception) { 00116 } 00117 } 00118 00119 /** 00120 * @test 00121 */ 00122 public function issetReturnsCorrectResult() { 00123 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE); 00124 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234')); 00125 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00126 00127 $this->assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.'); 00128 $arguments[] = $mockArgument; 00129 $this->assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.'); 00130 } 00131 00132 /** 00133 * @test 00134 */ 00135 public function getArgumentNamesReturnsNamesOfAddedArguments() { 00136 $mockArgument1 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE); 00137 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1')); 00138 $mockArgument2 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE); 00139 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2')); 00140 $mockArgument3 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE); 00141 $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3')); 00142 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00143 $arguments[] = $mockArgument1; 00144 $arguments[] = $mockArgument2; 00145 $arguments[] = $mockArgument3; 00146 00147 $expectedArgumentNames = array('argumentName1', 'argumentName2', 'argumentName3'); 00148 $this->assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.'); 00149 } 00150 00151 /** 00152 * @test 00153 */ 00154 public function getArgumentShortNamesReturnsShortNamesOfAddedArguments() { 00155 $mockArgument1 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getShortName'), array(), '', FALSE); 00156 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1')); 00157 $mockArgument1->expects($this->any())->method('getShortName')->will($this->returnValue('a')); 00158 $mockArgument2 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getShortName'), array(), '', FALSE); 00159 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2')); 00160 $mockArgument2->expects($this->any())->method('getShortName')->will($this->returnValue('b')); 00161 $mockArgument3 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getShortName'), array(), '', FALSE); 00162 $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3')); 00163 $mockArgument3->expects($this->any())->method('getShortName')->will($this->returnValue('c')); 00164 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00165 $arguments[] = $mockArgument1; 00166 $arguments[] = $mockArgument2; 00167 $arguments[] = $mockArgument3; 00168 00169 $expectedShortNames = array('a', 'b', 'c'); 00170 $this->assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.'); 00171 } 00172 00173 /** 00174 * @test 00175 */ 00176 public function addNewArgumentCreatesAndAddsNewArgument() { 00177 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE); 00178 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName')); 00179 00180 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface'); 00181 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Controller_Argument')->will($this->returnValue($mockArgument)); 00182 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00183 $arguments->injectObjectManager($mockObjectManager); 00184 00185 $addedArgument = $arguments->addNewArgument('dummyName'); 00186 $this->assertType('Tx_Extbase_MVC_Controller_Argument', $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.'); 00187 00188 $retrievedArgument = $arguments['dummyName']; 00189 $this->assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.'); 00190 } 00191 00192 /** 00193 * @test 00194 */ 00195 public function addNewArgumentAssumesTextDataTypeByDefault() { 00196 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE); 00197 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName')); 00198 00199 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface'); 00200 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Controller_Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument)); 00201 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00202 $arguments->injectObjectManager($mockObjectManager); 00203 $arguments->addNewArgument('dummyName'); 00204 } 00205 00206 /** 00207 * @test 00208 */ 00209 public function addNewArgumentCanAddArgumentsMarkedAsRequired() { 00210 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'setRequired'), array(), '', FALSE); 00211 $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName')); 00212 $mockArgument->expects($this->once())->method('setRequired')->with(TRUE); 00213 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface'); 00214 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Controller_Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument)); 00215 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00216 $arguments->injectObjectManager($mockObjectManager); 00217 $arguments->addNewArgument('dummyName', 'Text', TRUE); 00218 } 00219 00220 /** 00221 * @test 00222 */ 00223 public function addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues() { 00224 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'setRequired', 'setDefaultValue'), array(), '', FALSE); 00225 $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName')); 00226 $mockArgument->expects($this->once())->method('setRequired')->with(FALSE); 00227 $mockArgument->expects($this->once())->method('setDefaultValue')->with('someDefaultValue'); 00228 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface'); 00229 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Controller_Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument)); 00230 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00231 $arguments->injectObjectManager($mockObjectManager); 00232 $arguments->addNewArgument('dummyName', 'Text', FALSE, 'someDefaultValue'); 00233 } 00234 00235 /** 00236 * @test 00237 * @expectedException LogicException 00238 * @author Bastian Waidelich <bastian@typo3.org> 00239 */ 00240 public function callingInvalidMethodThrowsException() { 00241 $arguments = new Tx_Extbase_MVC_Controller_Arguments(); 00242 $arguments->nonExistingMethod(); 00243 } 00244 00245 /** 00246 * @test 00247 * @author Christopher Hlubek <hlubek@networkteam.com> 00248 */ 00249 public function removeAllClearsAllArguments() { 00250 $mockArgument1 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getShortName'), array(), '', FALSE); 00251 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1')); 00252 $mockArgument2 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getShortName'), array(), '', FALSE); 00253 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2')); 00254 $arguments = new Tx_Extbase_MVC_Controller_Arguments; 00255 $arguments[] = $mockArgument1; 00256 $arguments[] = $mockArgument2; 00257 00258 $this->assertTrue($arguments->hasArgument('argumentName2')); 00259 $arguments->removeAll(); 00260 $this->assertFalse($arguments->hasArgument('argumentName2')); 00261 } 00262 } 00263 ?>
1.8.0