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