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