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