TYPO3 CMS  TYPO3_7-6
RequestBuilderTest.php
Go to the documentation of this file.
1 <?php
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 
28 {
32  protected $requestBuilder;
33 
37  protected $request;
38 
42  protected $mockObjectManager;
43 
47  protected $mockCommand;
48 
53 
58 
63 
67  protected function setUp()
68  {
69  $this->request = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Cli\Request::class, ['dummy']);
70  $this->mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
71  $this->mockObjectManager->expects($this->any())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\Request::class)->will($this->returnValue($this->request));
72  $this->mockCommand = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, [], [], '', false);
73  $this->mockCommand->expects($this->any())->method('getControllerClassName')->will($this->returnValue('Tx_SomeExtensionName_Command_DefaultCommandController'));
74  $this->mockCommand->expects($this->any())->method('getControllerCommandName')->will($this->returnValue('list'));
75  $this->mockCommandManager = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class);
76  $this->mockCommandManager->expects($this->any())->method('getCommandByIdentifier')->with('some_extension_name:default:list')->will($this->returnValue($this->mockCommand));
77  $this->mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
78  $this->mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
79  $this->requestBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder::class, ['dummy']);
80  $this->requestBuilder->_set('objectManager', $this->mockObjectManager);
81  $this->requestBuilder->_set('reflectionService', $this->mockReflectionService);
82  $this->requestBuilder->_set('commandManager', $this->mockCommandManager);
83  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
84  }
85 
92  {
93  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->will($this->returnValue([]));
94  $request = $this->requestBuilder->build('some_extension_name:default:list');
95  $this->assertSame('Tx_SomeExtensionName_Command_DefaultCommandController', $request->getControllerObjectName());
96  $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.');
97  }
98 
103  {
104  // The following call is only made to satisfy PHPUnit. For some weird reason PHPUnit complains that the
105  // mocked method ("getObjectNameByClassName") does not exist _if the mock object is not used_.
106  $this->mockCommandManager->getCommandByIdentifier('some_extension_name:default:list');
107  $mockCommandManager = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class);
108  $mockCommandManager->expects($this->any())->method('getCommandByIdentifier')->with('test:default:list')->will($this->throwException(new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException()));
109  $this->requestBuilder->_set('commandManager', $mockCommandManager);
110  $request = $this->requestBuilder->build('test:default:list');
111  $this->assertSame(\TYPO3\CMS\Extbase\Command\HelpCommandController::class, $request->getControllerObjectName());
112  }
113 
118  {
119  $methodParameters = [
120  'testArgument' => ['optional' => false, 'type' => 'string']
121  ];
122  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
123  $request = $this->requestBuilder->build('some_extension_name:default:list --test-argument=value');
124  $this->assertTrue($request->hasArgument('testArgument'), 'The given "testArgument" was not found in the built request.');
125  $this->assertSame($request->getArgument('testArgument'), 'value', 'The "testArgument" had not the given value.');
126  }
127 
134  {
135  $methodParameters = [
136  'testArgument' => ['optional' => false, 'type' => 'string'],
137  'testArgument2' => ['optional' => false, 'type' => 'string']
138  ];
139  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
140  $request = $this->requestBuilder->build('some_extension_name:default:list --test-argument=value --test-argument2=value2');
141  $this->assertTrue($request->hasArgument('testArgument'), 'The given "testArgument" was not found in the built request.');
142  $this->assertTrue($request->hasArgument('testArgument2'), 'The given "testArgument2" was not found in the built request.');
143  $this->assertEquals($request->getArgument('testArgument'), 'value', 'The "testArgument" had not the given value.');
144  $this->assertEquals($request->getArgument('testArgument2'), 'value2', 'The "testArgument2" had not the given value.');
145  }
146 
153  {
154  $methodParameters = [
155  'testArgument' => ['optional' => false, 'type' => 'string'],
156  'testArgument2' => ['optional' => false, 'type' => 'string'],
157  'testArgument3' => ['optional' => false, 'type' => 'string'],
158  'testArgument4' => ['optional' => false, 'type' => 'string']
159  ];
160  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
161  $request = $this->requestBuilder->build('some_extension_name:default:list --test-argument= value --test-argument2 =value2 --test-argument3 = value3 --test-argument4=value4');
162  $this->assertTrue($request->hasArgument('testArgument'), 'The given "testArgument" was not found in the built request.');
163  $this->assertTrue($request->hasArgument('testArgument2'), 'The given "testArgument2" was not found in the built request.');
164  $this->assertTrue($request->hasArgument('testArgument3'), 'The given "testArgument3" was not found in the built request.');
165  $this->assertTrue($request->hasArgument('testArgument4'), 'The given "testArgument4" was not found in the built request.');
166  $this->assertSame($request->getArgument('testArgument'), 'value', 'The "testArgument" had not the given value.');
167  $this->assertSame($request->getArgument('testArgument2'), 'value2', 'The "testArgument2" had not the given value.');
168  $this->assertSame($request->getArgument('testArgument3'), 'value3', 'The "testArgument3" had not the given value.');
169  $this->assertSame($request->getArgument('testArgument4'), 'value4', 'The "testArgument4" had not the given value.');
170  }
171 
178  {
179  $methodParameters = [
180  'a' => ['optional' => false, 'type' => 'string'],
181  'd' => ['optional' => false, 'type' => 'string'],
182  'f' => ['optional' => false, 'type' => 'string']
183  ];
184  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
185  $request = $this->requestBuilder->build('some_extension_name:default:list -d valued -f=valuef -a = valuea');
186  $this->assertTrue($request->hasArgument('d'), 'The given "d" was not found in the built request.');
187  $this->assertTrue($request->hasArgument('f'), 'The given "f" was not found in the built request.');
188  $this->assertTrue($request->hasArgument('a'), 'The given "a" was not found in the built request.');
189  $this->assertSame($request->getArgument('d'), 'valued', 'The "d" had not the given value.');
190  $this->assertSame($request->getArgument('f'), 'valuef', 'The "f" had not the given value.');
191  $this->assertSame($request->getArgument('a'), 'valuea', 'The "a" had not the given value.');
192  }
193 
201  {
202  $methodParameters = [
203  'testArgument' => ['optional' => false, 'type' => 'string'],
204  'testArgument2' => ['optional' => false, 'type' => 'string'],
205  'testArgument3' => ['optional' => false, 'type' => 'string'],
206  'testArgument4' => ['optional' => false, 'type' => 'string'],
207  'testArgument5' => ['optional' => false, 'type' => 'string'],
208  'testArgument6' => ['optional' => false, 'type' => 'string'],
209  'testArgument7' => ['optional' => false, 'type' => 'string'],
210  'f' => ['optional' => false, 'type' => 'string'],
211  'd' => ['optional' => false, 'type' => 'string'],
212  'a' => ['optional' => false, 'type' => 'string'],
213  'c' => ['optional' => false, 'type' => 'string'],
214  'j' => ['optional' => false, 'type' => 'string'],
215  'k' => ['optional' => false, 'type' => 'string'],
216  'm' => ['optional' => false, 'type' => 'string']
217  ];
218  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
219  $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');
220  $this->assertTrue($request->hasArgument('testArgument'), 'The given "testArgument" was not found in the built request.');
221  $this->assertTrue($request->hasArgument('testArgument2'), 'The given "testArgument2" was not found in the built request.');
222  $this->assertTrue($request->hasArgument('k'), 'The given "k" was not found in the built request.');
223  $this->assertTrue($request->hasArgument('testArgument3'), 'The given "testArgument3" was not found in the built request.');
224  $this->assertTrue($request->hasArgument('testArgument4'), 'The given "testArgument4" was not found in the built request.');
225  $this->assertTrue($request->hasArgument('f'), 'The given "f" was not found in the built request.');
226  $this->assertTrue($request->hasArgument('d'), 'The given "d" was not found in the built request.');
227  $this->assertTrue($request->hasArgument('a'), 'The given "a" was not found in the built request.');
228  $this->assertTrue($request->hasArgument('c'), 'The given "d" was not found in the built request.');
229  $this->assertTrue($request->hasArgument('testArgument7'), 'The given "testArgument7" was not found in the built request.');
230  $this->assertTrue($request->hasArgument('testArgument5'), 'The given "testArgument5" was not found in the built request.');
231  $this->assertTrue($request->hasArgument('testArgument6'), 'The given "testArgument6" was not found in the built request.');
232  $this->assertTrue($request->hasArgument('j'), 'The given "j" was not found in the built request.');
233  $this->assertTrue($request->hasArgument('m'), 'The given "m" was not found in the built request.');
234  $this->assertSame($request->getArgument('testArgument'), 'value', 'The "testArgument" had not the given value.');
235  $this->assertSame($request->getArgument('testArgument2'), 'value2', 'The "testArgument2" had not the given value.');
236  $this->assertSame($request->getArgument('testArgument3'), 'value3', 'The "testArgument3" had not the given value.');
237  $this->assertSame($request->getArgument('testArgument4'), 'value4', 'The "testArgument4" had not the given value.');
238  $this->assertSame($request->getArgument('f'), 'valuef', 'The "f" had not the given value.');
239  $this->assertSame($request->getArgument('d'), 'valued', 'The "d" had not the given value.');
240  $this->assertSame($request->getArgument('a'), 'valuea', 'The "a" had not the given value.');
241  $this->assertSame($request->getArgument('testArgument5'), '5', 'The "testArgument4" had not the given value.');
242  $this->assertSame($request->getArgument('j'), 'kjk', 'The "j" had not the given value.');
243  }
244 
249  {
250  $methodParameters = [
251  'testArgument1' => ['optional' => false, 'type' => 'string'],
252  'testArgument2' => ['optional' => false, 'type' => 'string']
253  ];
254  $this->mockReflectionService->expects($this->exactly(2))->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
255  $request = $this->requestBuilder->build('some_extension_name:default:list --test-argument1 firstArgumentValue --test-argument2 secondArgumentValue');
256  $this->assertSame('firstArgumentValue', $request->getArgument('testArgument1'));
257  $this->assertSame('secondArgumentValue', $request->getArgument('testArgument2'));
258  $request = $this->requestBuilder->build('some_extension_name:default:list firstArgumentValue secondArgumentValue');
259  $this->assertSame('firstArgumentValue', $request->getArgument('testArgument1'));
260  $this->assertSame('secondArgumentValue', $request->getArgument('testArgument2'));
261  }
262 
267  {
268  $methodParameters = [
269  'some' => ['optional' => true, 'type' => 'boolean'],
270  'option' => ['optional' => true, 'type' => 'string'],
271  'argument1' => ['optional' => false, 'type' => 'string'],
272  'argument2' => ['optional' => false, 'type' => 'string']
273  ];
274  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
275  $request = $this->requestBuilder->build('some_extension_name:default:list --some -option=value file1 file2');
276  $this->assertSame('list', $request->getControllerCommandName());
277  $this->assertTrue($request->getArgument('some'));
278  $this->assertSame('file1', $request->getArgument('argument1'));
279  $this->assertSame('file2', $request->getArgument('argument2'));
280  }
281 
286  {
287  $methodParameters = [
288  'testArgument1' => ['optional' => false, 'type' => 'string'],
289  'testArgument2' => ['optional' => false, 'type' => 'string']
290  ];
291  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
292  $expectedArguments = ['testArgument1' => 'firstArgumentValue', 'testArgument2' => 'secondArgumentValue'];
293  $request = $this->requestBuilder->build('some_extension_name:default:list --test-argument1=firstArgumentValue --test-argument2 secondArgumentValue exceedingArgument1');
294  $this->assertEquals($expectedArguments, $request->getArguments());
295  $this->assertEquals(['exceedingArgument1'], $request->getExceedingArguments());
296  }
297 
303  {
304  $methodParameters = [
305  'testArgument1' => ['optional' => false, 'type' => 'string'],
306  'testArgument2' => ['optional' => false, 'type' => 'string']
307  ];
308  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
309  $this->requestBuilder->build('some_extension_name:default:list --test-argument1 firstArgumentValue secondArgumentValue');
310  }
311 
317  {
318  $methodParameters = [
319  'requiredArgument1' => ['optional' => false, 'type' => 'string'],
320  'requiredArgument2' => ['optional' => false, 'type' => 'string']
321  ];
322  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
323  $this->requestBuilder->build('some_extension_name:default:list firstArgumentValue --required-argument2 secondArgumentValue');
324  }
325 
330  {
331  $methodParameters = [
332  'requiredArgument1' => ['optional' => false, 'type' => 'string'],
333  'requiredArgument2' => ['optional' => false, 'type' => 'string'],
334  'booleanOption' => ['optional' => true, 'type' => 'boolean']
335  ];
336  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
337  $expectedArguments = ['requiredArgument1' => 'firstArgumentValue', 'requiredArgument2' => 'secondArgumentValue', 'booleanOption' => true];
338  $request = $this->requestBuilder->build('some_extension_name:default:list --booleanOption firstArgumentValue secondArgumentValue');
339  $this->assertEquals($expectedArguments, $request->getArguments());
340  }
341 
346  {
347  $methodParameters = [
348  'b1' => ['optional' => true, 'type' => 'boolean'],
349  'b2' => ['optional' => true, 'type' => 'boolean'],
350  'b3' => ['optional' => true, 'type' => 'boolean'],
351  'b4' => ['optional' => true, 'type' => 'boolean'],
352  'b5' => ['optional' => true, 'type' => 'boolean'],
353  'b6' => ['optional' => true, 'type' => 'boolean']
354  ];
355  $this->mockReflectionService->expects($this->once())->method('getMethodParameters')->with('Tx_SomeExtensionName_Command_DefaultCommandController', 'listCommand')->will($this->returnValue($methodParameters));
356  $expectedArguments = ['b1' => true, 'b2' => true, 'b3' => true, 'b4' => false, 'b5' => false, 'b6' => false];
357  $request = $this->requestBuilder->build('some_extension_name:default:list --b2 y --b1 1 --b3 true --b4 false --b5 n --b6 0');
358  $this->assertEquals($expectedArguments, $request->getArguments());
359  }
360 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)