Merge "[Task] Update changelog and version to 6.0.0-dev"
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / MVC / CLI / RequestBuilderTest.php
1 <?php
2 /* *
3 * This script belongs to the Extbase framework. *
4 * *
5 * It is free software; you can redistribute it and/or modify it under *
6 * the terms of the GNU Lesser General Public License as published by the *
7 * Free Software Foundation, either version 3 of the License, or (at your *
8 * option) any later version. *
9 * *
10 * This script is distributed in the hope that it will be useful, but *
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
12 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
13 * General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Lesser General Public *
16 * License along with the script. *
17 * If not, see http://www.gnu.org/licenses/lgpl.html *
18 * *
19 * The TYPO3 project - inspiring people to share! *
20 * */
21
22 /**
23 * Testcase for the MVC CLI Request Builder
24 */
25 class Tx_Extbase_Tests_Unit_MVC_CLI_RequestBuilderTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
26
27 /**
28 * @var Tx_Extbase_MVC_CLI_RequestBuilder
29 */
30 protected $requestBuilder;
31
32 /**
33 * @var Tx_Extbase_MVC_CLI_Request
34 */
35 protected $request;
36
37 /**
38 * @var Tx_Extbase_Object_ObjectManagerInterface
39 */
40 protected $mockObjectManager;
41
42 /**
43 * @var Tx_Extbase_MVC_CLI_Command
44 */
45 protected $mockCommand;
46
47 /**
48 * @var Tx_Extbase_MVC_CLI_CommandManager
49 */
50 protected $mockCommandManager;
51
52 /**
53 * @var Tx_Extbase_Reflection_Service
54 */
55 protected $mockReflectionService;
56
57 /**
58 * Sets up this test case
59 *
60 * @author Robert Lemke <robert@typo3.org>
61 */
62 public function setUp() {
63 $this->request = $this->getAccessibleMock('Tx_Extbase_MVC_CLI_Request', array('dummy'));
64
65 $this->mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
66 $this->mockObjectManager->expects($this->any())->method('get')->with('Tx_Extbase_MVC_CLI_Request')->will($this->returnValue($this->request));
67
68 $this->mockCommand = $this->getMock('Tx_Extbase_MVC_CLI_Command', array(), array(), '', FALSE);
69 $this->mockCommand->expects($this->any())->method('getControllerClassName')->will($this->returnValue('Tx_SomeExtensionName_Command_DefaultCommandController'));
70 $this->mockCommand->expects($this->any())->method('getControllerCommandName')->will($this->returnValue('list'));
71
72 $this->mockCommandManager = $this->getMock('Tx_Extbase_MVC_CLI_CommandManager');
73 $this->mockCommandManager->expects($this->any())->method('getCommandByIdentifier')->with('some_extension_name:default:list')->will($this->returnValue($this->mockCommand));
74
75 $this->mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service');
76
77 $this->requestBuilder = new Tx_Extbase_MVC_CLI_RequestBuilder();
78 $this->requestBuilder->injectObjectManager($this->mockObjectManager);
79 $this->requestBuilder->injectReflectionService($this->mockReflectionService);
80 $this->requestBuilder->injectCommandManager($this->mockCommandManager);
81 }
82
83 /**
84 * Checks if a CLI request specifying a package, controller and action name results in the expected request object
85 *
86 * @test
87 * @author Robert Lemke <robert@typo3.org>
88 */
89 public function cliAccessWithExtensionControllerAndActionNameBuildsCorrectRequest() {
90 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->will($this->returnValue(array()));
91
92 $request = $this->requestBuilder->build('some_extension_name:default:list');
93 $this->assertEquals('Tx_SomeExtensionName_Command_DefaultCommandController', $request->getControllerObjectName());
94 $this->assertEquals('list', $request->getControllerCommandName(), 'The CLI request specifying a package, controller and action name did not return a request object pointing to the expected action.');
95 }
96
97 /**
98 * @test
99 * @author Robert Lemke <robert@typo3.org>
100 * @author Bastian Waidelich <bastian@typo3.org>
101 */
102 public function ifCommandCantBeResolvedTheHelpScreenIsShown() {
103 // The following call is only made to satisfy PHPUnit. For some weird reason PHPUnit complains that the
104 // mocked method ("getObjectNameByClassName") does not exist _if the mock object is not used_.
105 $this->mockCommandManager->getCommandByIdentifier('some_extension_name:default:list');
106 $mockCommandManager = $this->getMock('Tx_Extbase_MVC_CLI_CommandManager');
107 $mockCommandManager->expects($this->any())->method('getCommandByIdentifier')->with('test:default:list')->will($this->throwException(new Tx_Extbase_MVC_Exception_NoSuchCommand()));
108 $this->requestBuilder->injectCommandManager($mockCommandManager);
109
110 $request = $this->requestBuilder->build('test:default:list');
111 $this->assertEquals('Tx_Extbase_Command_HelpCommandController', $request->getControllerObjectName());
112 }
113
114 /**
115 * Checks if a CLI request specifying some "console style" (--my-argument=value) arguments results in the expected request object
116 *
117 * @test
118 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
119 */
120 public function cliAccessWithExtensionControllerActionAndArgumentsBuildsCorrectRequest() {
121 $methodParameters = array(
122 'testArgument' => array('optional' => FALSE, 'type' => 'string'),
123 'testArgument2' => array('optional' => FALSE, 'type' => 'string')
124 );
125 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
126
127 $request = $this->requestBuilder->build('some_extension_name:default:list --test-argument=value --test-argument2=value2');
128 $this->assertTrue($request->hasArgument('testArgument'), 'The given "testArgument" was not found in the built request.');
129 $this->assertTrue($request->hasArgument('testArgument2'), 'The given "testArgument2" was not found in the built request.');
130 $this->assertEquals($request->getArgument('testArgument'), 'value', 'The "testArgument" had not the given value.');
131 $this->assertEquals($request->getArgument('testArgument2'), 'value2', 'The "testArgument2" had not the given value.');
132 }
133
134 /**
135 * Checks if a CLI request specifying some "console style" (--my-argument =value) arguments with spaces between name and value results in the expected request object
136 *
137 * @test
138 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
139 */
140 public function checkIfCLIAccesWithPackageControllerActionAndArgumentsToleratesSpaces() {
141 $methodParameters = array(
142 'testArgument' => array('optional' => FALSE, 'type' => 'string'),
143 'testArgument2' => array('optional' => FALSE, 'type' => 'string'),
144 'testArgument3' => array('optional' => FALSE, 'type' => 'string'),
145 'testArgument4' => array('optional' => FALSE, 'type' => 'string')
146 );
147 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
148
149 $request = $this->requestBuilder->build('some_extension_name:default:list --test-argument= value --test-argument2 =value2 --test-argument3 = value3 --test-argument4=value4');
150 $this->assertTrue($request->hasArgument('testArgument'), 'The given "testArgument" was not found in the built request.');
151 $this->assertTrue($request->hasArgument('testArgument2'), 'The given "testArgument2" was not found in the built request.');
152 $this->assertTrue($request->hasArgument('testArgument3'), 'The given "testArgument3" was not found in the built request.');
153 $this->assertTrue($request->hasArgument('testArgument4'), 'The given "testArgument4" was not found in the built request.');
154 $this->assertEquals($request->getArgument('testArgument'), 'value', 'The "testArgument" had not the given value.');
155 $this->assertEquals($request->getArgument('testArgument2'), 'value2', 'The "testArgument2" had not the given value.');
156 $this->assertEquals($request->getArgument('testArgument3'), 'value3', 'The "testArgument3" had not the given value.');
157 $this->assertEquals($request->getArgument('testArgument4'), 'value4', 'The "testArgument4" had not the given value.');
158 }
159
160 /**
161 * Checks if a CLI request specifying some short "console style" (-c value or -c=value or -c = value) arguments results in the expected request object
162 *
163 * @test
164 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
165 */
166 public function CLIAccesWithShortArgumentsBuildsCorrectRequest() {
167 $methodParameters = array(
168 'a' => array('optional' => FALSE, 'type' => 'string'),
169 'd' => array('optional' => FALSE, 'type' => 'string'),
170 'f' => array('optional' => FALSE, 'type' => 'string'),
171 );
172 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
173
174 $request = $this->requestBuilder->build('some_extension_name:default:list -d valued -f=valuef -a = valuea');
175 $this->assertTrue($request->hasArgument('d'), 'The given "d" was not found in the built request.');
176 $this->assertTrue($request->hasArgument('f'), 'The given "f" was not found in the built request.');
177 $this->assertTrue($request->hasArgument('a'), 'The given "a" was not found in the built request.');
178 $this->assertEquals($request->getArgument('d'), 'valued', 'The "d" had not the given value.');
179 $this->assertEquals($request->getArgument('f'), 'valuef', 'The "f" had not the given value.');
180 $this->assertEquals($request->getArgument('a'), 'valuea', 'The "a" had not the given value.');
181 }
182
183 /**
184 * Checks if a CLI request specifying some mixed "console style" (-c or --my-argument -f=value) arguments with and
185 * without values results in the expected request object
186 *
187 * @test
188 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
189 */
190 public function CLIAccesWithArgumentsWithAndWithoutValuesBuildsCorrectRequest() {
191 $methodParameters = array(
192 'testArgument' => array('optional' => FALSE, 'type' => 'string'),
193 'testArgument2' => array('optional' => FALSE, 'type' => 'string'),
194 'testArgument3' => array('optional' => FALSE, 'type' => 'string'),
195 'testArgument4' => array('optional' => FALSE, 'type' => 'string'),
196 'testArgument5' => array('optional' => FALSE, 'type' => 'string'),
197 'testArgument6' => array('optional' => FALSE, 'type' => 'string'),
198 'testArgument7' => array('optional' => FALSE, 'type' => 'string'),
199 'f' => array('optional' => FALSE, 'type' => 'string'),
200 'd' => array('optional' => FALSE, 'type' => 'string'),
201 'a' => array('optional' => FALSE, 'type' => 'string'),
202 'c' => array('optional' => FALSE, 'type' => 'string'),
203 'j' => array('optional' => FALSE, 'type' => 'string'),
204 'k' => array('optional' => FALSE, 'type' => 'string'),
205 'm' => array('optional' => FALSE, 'type' => 'string'),
206 );
207 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
208
209 $request = $this->requestBuilder->build('some_extension_name:default:list --test-argument=value --test-argument2= value2 -k --test-argument-3 = value3 --test-argument4=value4 -f valuef -d=valued -a = valuea -c --testArgument7 --test-argument5 = 5 --test-argument6 -j kjk -m');
210 $this->assertTrue($request->hasArgument('testArgument'), 'The given "testArgument" was not found in the built request.');
211 $this->assertTrue($request->hasArgument('testArgument2'), 'The given "testArgument2" was not found in the built request.');
212 $this->assertTrue($request->hasArgument('k'), 'The given "k" was not found in the built request.');
213 $this->assertTrue($request->hasArgument('testArgument3'), 'The given "testArgument3" was not found in the built request.');
214 $this->assertTrue($request->hasArgument('testArgument4'), 'The given "testArgument4" was not found in the built request.');
215 $this->assertTrue($request->hasArgument('f'), 'The given "f" was not found in the built request.');
216 $this->assertTrue($request->hasArgument('d'), 'The given "d" was not found in the built request.');
217 $this->assertTrue($request->hasArgument('a'), 'The given "a" was not found in the built request.');
218 $this->assertTrue($request->hasArgument('c'), 'The given "d" was not found in the built request.');
219 $this->assertTrue($request->hasArgument('testArgument7'), 'The given "testArgument7" was not found in the built request.');
220 $this->assertTrue($request->hasArgument('testArgument5'), 'The given "testArgument5" was not found in the built request.');
221 $this->assertTrue($request->hasArgument('testArgument6'), 'The given "testArgument6" was not found in the built request.');
222 $this->assertTrue($request->hasArgument('j'), 'The given "j" was not found in the built request.');
223 $this->assertTrue($request->hasArgument('m'), 'The given "m" was not found in the built request.');
224 $this->assertEquals($request->getArgument('testArgument'), 'value', 'The "testArgument" had not the given value.');
225 $this->assertEquals($request->getArgument('testArgument2'), 'value2', 'The "testArgument2" had not the given value.');
226 $this->assertEquals($request->getArgument('testArgument3'), 'value3', 'The "testArgument3" had not the given value.');
227 $this->assertEquals($request->getArgument('testArgument4'), 'value4', 'The "testArgument4" had not the given value.');
228 $this->assertEquals($request->getArgument('f'), 'valuef', 'The "f" had not the given value.');
229 $this->assertEquals($request->getArgument('d'), 'valued', 'The "d" had not the given value.');
230 $this->assertEquals($request->getArgument('a'), 'valuea', 'The "a" had not the given value.');
231 $this->assertEquals($request->getArgument('testArgument5'), '5', 'The "testArgument4" had not the given value.');
232 $this->assertEquals($request->getArgument('j'), 'kjk', 'The "j" had not the given value.');
233 }
234
235 /**
236 * @test
237 * @author Robert Lemke <robert@typo3.org>
238 */
239 public function insteadOfNamedArgumentsTheArgumentsCanBePassedUnnamedInTheCorrectOrder() {
240 $methodParameters = array(
241 'testArgument1' => array('optional' => FALSE, 'type' => 'string'),
242 'testArgument2' => array('optional' => FALSE, 'type' => 'string'),
243 );
244 $this->mockReflectionService->expects($this->exactly(2))->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
245
246 $request = $this->requestBuilder->build('some_extension_name:default:list --test-argument1 firstArgumentValue --test-argument2 secondArgumentValue');
247 $this->assertEquals('firstArgumentValue', $request->getArgument('testArgument1'));
248 $this->assertEquals('secondArgumentValue', $request->getArgument('testArgument2'));
249
250 $request = $this->requestBuilder->build('some_extension_name:default:list firstArgumentValue secondArgumentValue');
251 $this->assertEquals('firstArgumentValue', $request->getArgument('testArgument1'));
252 $this->assertEquals('secondArgumentValue', $request->getArgument('testArgument2'));
253 }
254
255 /**
256 * @test
257 * @author Karsten Dambekalns <karsten@typo3.org>
258 */
259 public function argumentsAreDetectedAfterOptions() {
260 $methodParameters = array(
261 'some' => array('optional' => TRUE, 'type' => 'boolean'),
262 'option' => array('optional' => TRUE, 'type' => 'string'),
263 'argument1' => array('optional' => FALSE, 'type' => 'string'),
264 'argument2' => array('optional' => FALSE, 'type' => 'string'),
265 );
266 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
267
268 $request = $this->requestBuilder->build('some_extension_name:default:list --some -option=value file1 file2');
269 $this->assertEquals('list', $request->getControllerCommandName());
270 $this->assertTrue($request->getArgument('some'));
271 $this->assertEquals('file1', $request->getArgument('argument1'));
272 $this->assertEquals('file2', $request->getArgument('argument2'));
273 }
274
275 /**
276 * @test
277 * @author Robert Lemke <robert@typo3.org>
278 */
279 public function exceedingArgumentsMayBeSpecified() {
280 $methodParameters = array(
281 'testArgument1' => array('optional' => FALSE, 'type' => 'string'),
282 'testArgument2' => array('optional' => FALSE, 'type' => 'string'),
283 );
284 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
285
286 $expectedArguments = array('testArgument1' => 'firstArgumentValue', 'testArgument2' => 'secondArgumentValue');
287
288 $request = $this->requestBuilder->build('some_extension_name:default:list --test-argument1=firstArgumentValue --test-argument2 secondArgumentValue exceedingArgument1');
289 $this->assertEquals($expectedArguments, $request->getArguments());
290 $this->assertEquals(array('exceedingArgument1'), $request->getExceedingArguments());
291 }
292
293 /**
294 * @test
295 * @expectedException Tx_Extbase_MVC_Exception_InvalidArgumentMixing
296 * @author Robert Lemke <robert@typo3.org>
297 */
298 public function ifNamedArgumentsAreUsedAllRequiredArgumentsMustBeNamed() {
299 $methodParameters = array(
300 'testArgument1' => array('optional' => FALSE, 'type' => 'string'),
301 'testArgument2' => array('optional' => FALSE, 'type' => 'string'),
302 );
303 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
304
305 $this->requestBuilder->build('some_extension_name:default:list --test-argument1 firstArgumentValue secondArgumentValue');
306 }
307
308 /**
309 * @test
310 * @expectedException Tx_Extbase_MVC_Exception_InvalidArgumentMixing
311 * @author Robert Lemke <robert@typo3.org>
312 */
313 public function ifUnnamedArgumentsAreUsedAllRequiredArgumentsMustBeUnnamed() {
314 $methodParameters = array(
315 'requiredArgument1' => array('optional' => FALSE, 'type' => 'string'),
316 'requiredArgument2' => array('optional' => FALSE, 'type' => 'string'),
317 );
318 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
319
320 $this->requestBuilder->build('some_extension_name:default:list firstArgumentValue --required-argument2 secondArgumentValue');
321 }
322
323 /**
324 * @test
325 * @author Robert Lemke <robert@typo3.org>
326 */
327 public function booleanOptionsAreConsideredEvenIfAnUnnamedArgumentFollows() {
328 $methodParameters = array(
329 'requiredArgument1' => array('optional' => FALSE, 'type' => 'string'),
330 'requiredArgument2' => array('optional' => FALSE, 'type' => 'string'),
331 'booleanOption' => array('optional' => TRUE, 'type' => 'boolean'),
332 );
333 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
334
335 $expectedArguments = array('requiredArgument1' => 'firstArgumentValue', 'requiredArgument2' => 'secondArgumentValue', 'booleanOption' => TRUE);
336
337 $request = $this->requestBuilder->build('some_extension_name:default:list --booleanOption firstArgumentValue secondArgumentValue');
338 $this->assertEquals($expectedArguments, $request->getArguments());
339 }
340
341 /**
342 * @test
343 * @author Robert Lemke <robert@typo3.org>
344 */
345 public function booleanOptionsCanHaveOnlyCertainValuesIfTheValueIsAssignedWithoutEqualSign() {
346 $methodParameters = array(
347 'b1' => array('optional' => TRUE, 'type' => 'boolean'),
348 'b2' => array('optional' => TRUE, 'type' => 'boolean'),
349 'b3' => array('optional' => TRUE, 'type' => 'boolean'),
350 'b4' => array('optional' => TRUE, 'type' => 'boolean'),
351 'b5' => array('optional' => TRUE, 'type' => 'boolean'),
352 'b6' => array('optional' => TRUE, 'type' => 'boolean'),
353 );
354 $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
355
356 $expectedArguments = array('b1' => TRUE, 'b2' => TRUE, 'b3' => TRUE, 'b4' => FALSE, 'b5' => FALSE, 'b6' => FALSE);
357
358 $request = $this->requestBuilder->build('some_extension_name:default:list --b2 y --b1 1 --b3 true --b4 false --b5 n --b6 0');
359 $this->assertEquals($expectedArguments, $request->getArguments());
360 }
361 }
362
363 ?>