TYPO3 CMS  TYPO3_8-7
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 
27 class CommandTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
28 {
32  protected $command;
33 
38 
42  protected $mockObjectManager;
43 
46  protected function setUp()
47  {
48  $this->command = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, ['getCommandMethodReflection'], [], '', false);
49  $this->mockMethodReflection = $this->createMock(\TYPO3\CMS\Extbase\Reflection\MethodReflection::class);
50  $this->command->expects($this->any())->method('getCommandMethodReflection')->will($this->returnValue($this->mockMethodReflection));
51  $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
52  $this->command->_set('objectManager', $this->mockObjectManager);
53  }
54 
58  public function commandIdentifiers()
59  {
60  return [
61  ['Tx_ExtensionKey_Command_CacheCommandController', 'flush', 'extension_key:cache:flush'],
62  ['Tx_Ext_Command_CookieCommandController', 'bake', 'ext:cookie:bake'],
63  ['Tx_OtherExtensionKey_Foo_Faa_Fuuum_Command_CoffeeCommandController', 'brew', 'other_extension_key:coffee:brew'],
64  ];
65  }
66 
74  public function constructRendersACommandIdentifierByTheGivenControllerAndCommandName($controllerClassName, $commandName, $expectedCommandIdentifier)
75  {
76  $command = new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, $commandName);
77  $this->assertEquals($expectedCommandIdentifier, $command->getCommandIdentifier());
78  }
79 
83  public function invalidCommandClassNames()
84  {
85  return [
86  [''],
87  // CommandClassName must not be empty
88  ['Foo']
89  ];
90  }
91 
97  public function constructThrowsExceptionIfCommandClassNameIsInvalid($controllerClassName)
98  {
99  $this->expectException(\InvalidArgumentException::class);
100  $this->expectExceptionCode(1438782187);
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->createMock(\TYPO3\CMS\Extbase\Reflection\ParameterReflection::class);
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->createMock(\TYPO3\CMS\Extbase\Reflection\ParameterReflection::class);
138  $mockReflectionService = $this->createMock(\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->createMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class);
145  $mockCommandArgumentDefinition2 = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class);
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:74
constructThrowsExceptionIfCommandClassNameIsInvalid($controllerClassName)
Definition: CommandTest.php:97