[+TASK] Extbase (Tests): Fixed Extbase unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / MVC / Controller / ArgumentsTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 class Tx_Extbase_Tests_Unit_MVC_Controller_ArgumentsTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
29
30 /**
31 * @test
32 */
33 public function argumentsObjectIsOfScopePrototype() {
34 $arguments1 = new Tx_Extbase_MVC_Controller_Arguments();
35 $arguments2 = new Tx_Extbase_MVC_Controller_Arguments();
36 $this->assertNotSame($arguments1, $arguments2, 'The arguments object is not of scope prototype!');
37 }
38
39 /**
40 * @test
41 */
42 public function addingAnArgumentManuallyWorks() {
43 $arguments = new Tx_Extbase_MVC_Controller_Arguments();
44 $newArgument = new Tx_Extbase_MVC_Controller_Argument('argumentName1234', 'dummyValue');
45
46 $arguments->addArgument($newArgument);
47 $this->assertSame($newArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
48 }
49
50 /**
51 * @test
52 */
53 public function addingAnArgumentReplacesArgumentWithSameName() {
54 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
55
56 $mockFirstArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
57 $mockFirstArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
58 $arguments->addArgument($mockFirstArgument);
59
60 $mockSecondArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
61 $mockSecondArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
62 $arguments->addArgument($mockSecondArgument);
63
64 $this->assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
65 }
66
67 /**
68 * @test
69 */
70 public function addNewArgumentProvidesFluentInterface() {
71 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array(), '', FALSE);
72
73 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
74 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Controller_Argument')->will($this->returnValue($mockArgument));
75 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
76 $arguments->injectObjectManager($mockObjectManager);
77
78 $newArgument = $arguments->addNewArgument('someArgument');
79 $this->assertSame($newArgument, $mockArgument);
80 }
81
82 /**
83 * @test
84 */
85 public function addingArgumentThroughArrayAccessWorks() {
86 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
87 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
88 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
89
90 $arguments[] = $mockArgument;
91 $this->assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.');
92 $this->assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.');
93 }
94
95 /**
96 * @test
97 */
98 public function retrievingArgumentThroughArrayAccessWorks() {
99 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
100 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
101 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
102
103 $arguments[] = $mockArgument;
104 $this->assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.');
105 }
106
107 /**
108 * @test
109 */
110 public function getArgumentWithNonExistingArgumentNameThrowsException() {
111 $arguments = new Tx_Extbase_MVC_Controller_Arguments();
112 try {
113 $arguments->getArgument('someArgument');
114 $this->fail('getArgument() did not throw an exception although the specified argument does not exist.');
115 } catch (Tx_Extbase_MVC_Exception_NoSuchArgument $exception) {
116 }
117 }
118
119 /**
120 * @test
121 */
122 public function issetReturnsCorrectResult() {
123 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
124 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
125 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
126
127 $this->assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.');
128 $arguments[] = $mockArgument;
129 $this->assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.');
130 }
131
132 /**
133 * @test
134 */
135 public function getArgumentNamesReturnsNamesOfAddedArguments() {
136 $mockArgument1 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
137 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
138 $mockArgument2 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
139 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
140 $mockArgument3 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
141 $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
142 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
143 $arguments[] = $mockArgument1;
144 $arguments[] = $mockArgument2;
145 $arguments[] = $mockArgument3;
146
147 $expectedArgumentNames = array('argumentName1', 'argumentName2', 'argumentName3');
148 $this->assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.');
149 }
150
151 /**
152 * @test
153 */
154 public function getArgumentShortNamesReturnsShortNamesOfAddedArguments() {
155 $mockArgument1 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getShortName'), array(), '', FALSE);
156 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
157 $mockArgument1->expects($this->any())->method('getShortName')->will($this->returnValue('a'));
158 $mockArgument2 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getShortName'), array(), '', FALSE);
159 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
160 $mockArgument2->expects($this->any())->method('getShortName')->will($this->returnValue('b'));
161 $mockArgument3 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getShortName'), array(), '', FALSE);
162 $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
163 $mockArgument3->expects($this->any())->method('getShortName')->will($this->returnValue('c'));
164 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
165 $arguments[] = $mockArgument1;
166 $arguments[] = $mockArgument2;
167 $arguments[] = $mockArgument3;
168
169 $expectedShortNames = array('a', 'b', 'c');
170 $this->assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.');
171 }
172
173 /**
174 * @test
175 */
176 public function addNewArgumentCreatesAndAddsNewArgument() {
177 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
178 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
179
180 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
181 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Controller_Argument')->will($this->returnValue($mockArgument));
182 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
183 $arguments->injectObjectManager($mockObjectManager);
184
185 $addedArgument = $arguments->addNewArgument('dummyName');
186 $this->assertType('Tx_Extbase_MVC_Controller_Argument', $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.');
187
188 $retrievedArgument = $arguments['dummyName'];
189 $this->assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.');
190 }
191
192 /**
193 * @test
194 */
195 public function addNewArgumentAssumesTextDataTypeByDefault() {
196 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
197 $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
198
199 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
200 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Controller_Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
201 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
202 $arguments->injectObjectManager($mockObjectManager);
203 $arguments->addNewArgument('dummyName');
204 }
205
206 /**
207 * @test
208 */
209 public function addNewArgumentCanAddArgumentsMarkedAsRequired() {
210 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'setRequired'), array(), '', FALSE);
211 $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
212 $mockArgument->expects($this->once())->method('setRequired')->with(TRUE);
213 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
214 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Controller_Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
215 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
216 $arguments->injectObjectManager($mockObjectManager);
217 $arguments->addNewArgument('dummyName', 'Text', TRUE);
218 }
219
220 /**
221 * @test
222 */
223 public function addNewArgumentCanAddArgumentsMarkedAsOptionalWithDefaultValues() {
224 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'setRequired', 'setDefaultValue'), array(), '', FALSE);
225 $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
226 $mockArgument->expects($this->once())->method('setRequired')->with(FALSE);
227 $mockArgument->expects($this->once())->method('setDefaultValue')->with('someDefaultValue');
228 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
229 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Controller_Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
230 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
231 $arguments->injectObjectManager($mockObjectManager);
232 $arguments->addNewArgument('dummyName', 'Text', FALSE, 'someDefaultValue');
233 }
234
235 /**
236 * @test
237 * @expectedException LogicException
238 * @author Bastian Waidelich <bastian@typo3.org>
239 */
240 public function callingInvalidMethodThrowsException() {
241 $arguments = new Tx_Extbase_MVC_Controller_Arguments();
242 $arguments->nonExistingMethod();
243 }
244
245 /**
246 * @test
247 * @author Christopher Hlubek <hlubek@networkteam.com>
248 */
249 public function removeAllClearsAllArguments() {
250 $mockArgument1 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getShortName'), array(), '', FALSE);
251 $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
252 $mockArgument2 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getShortName'), array(), '', FALSE);
253 $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
254 $arguments = new Tx_Extbase_MVC_Controller_Arguments;
255 $arguments[] = $mockArgument1;
256 $arguments[] = $mockArgument2;
257
258 $this->assertTrue($arguments->hasArgument('argumentName2'));
259 $arguments->removeAll();
260 $this->assertFalse($arguments->hasArgument('argumentName2'));
261 }
262 }
263 ?>