TYPO3 CMS  TYPO3_6-2
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 }
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)