TYPO3 CMS  TYPO3_7-6
CommandTest.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 $command;
33 
38 
42  protected $mockObjectManager;
43 
47  protected function setUp()
48  {
49  $this->command = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, ['getCommandMethodReflection'], [], '', false);
50  $this->mockMethodReflection = $this->getMock(\TYPO3\CMS\Extbase\Reflection\MethodReflection::class, [], [], '', false);
51  $this->command->expects($this->any())->method('getCommandMethodReflection')->will($this->returnValue($this->mockMethodReflection));
52  $this->mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
53  $this->command->_set('objectManager', $this->mockObjectManager);
54  }
55 
59  public function commandIdentifiers()
60  {
61  return [
62  ['Tx_ExtensionKey_Command_CacheCommandController', 'flush', 'extension_key:cache:flush'],
63  ['Tx_Ext_Command_CookieCommandController', 'bake', 'ext:cookie:bake'],
64  ['Tx_OtherExtensionKey_Foo_Faa_Fuuum_Command_CoffeeCommandController', 'brew', 'other_extension_key:coffee:brew'],
65  ];
66  }
67 
75  public function constructRendersACommandIdentifierByTheGivenControllerAndCommandName($controllerClassName, $commandName, $expectedCommandIdentifier)
76  {
77  $command = new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, $commandName);
78  $this->assertEquals($expectedCommandIdentifier, $command->getCommandIdentifier());
79  }
80 
84  public function invalidCommandClassNames()
85  {
86  return [
87  [''],
88  // CommandClassName must not be empty
89  ['Foo']
90  ];
91  }
92 
99  public function constructThrowsExceptionIfCommandClassNameIsInvalid($controllerClassName)
100  {
101  new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, 'foo');
102  }
103 
108  {
109  $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue([]));
110  $this->assertFalse($this->command->hasArguments());
111  }
112 
117  {
118  $mockParameterReflection = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ParameterReflection::class, [], [], '', false);
119  $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue([$mockParameterReflection]));
120  $this->assertTrue($this->command->hasArguments());
121  }
122 
127  {
128  $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue([]));
129  $this->assertSame([], $this->command->getArgumentDefinitions());
130  }
131 
136  {
137  $mockParameterReflection = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ParameterReflection::class, [], [], '', false);
138  $mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
139  $mockMethodParameters = ['argument1' => ['optional' => false], 'argument2' => ['optional' => true]];
140  $mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue($mockMethodParameters));
141  $this->command->_set('reflectionService', $mockReflectionService);
142  $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue([$mockParameterReflection]));
143  $this->mockMethodReflection->expects($this->atLeastOnce())->method('getTagsValues')->will($this->returnValue(['param' => ['@param $argument1 argument1 description', '@param $argument2 argument2 description']]));
144  $mockCommandArgumentDefinition1 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, [], [], '', false);
145  $mockCommandArgumentDefinition2 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, [], [], '', false);
146  $this->mockObjectManager->expects($this->at(0))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, 'argument1', true, 'argument1 description')->will($this->returnValue($mockCommandArgumentDefinition1));
147  $this->mockObjectManager->expects($this->at(1))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, 'argument2', false, 'argument2 description')->will($this->returnValue($mockCommandArgumentDefinition2));
148  $expectedResult = [$mockCommandArgumentDefinition1, $mockCommandArgumentDefinition2];
149  $actualResult = $this->command->getArgumentDefinitions();
150  $this->assertSame($expectedResult, $actualResult);
151  }
152 }
constructRendersACommandIdentifierByTheGivenControllerAndCommandName($controllerClassName, $commandName, $expectedCommandIdentifier)
Definition: CommandTest.php:75
constructThrowsExceptionIfCommandClassNameIsInvalid($controllerClassName)
Definition: CommandTest.php:99
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)