[TASK] Use name-resolution instead of strings where possible: 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Cli / CommandTest.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
24 /**
25 * Test case
26 */
27 class CommandTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Mvc\Cli\Command|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
31 */
32 protected $command;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Reflection\MethodReflection
36 */
37 protected $mockMethodReflection;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
41 */
42 protected $mockObjectManager;
43
44 /**
45 * @return void
46 */
47 public function setUp() {
48 $this->command = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, array('getCommandMethodReflection'), array(), '', FALSE);
49 $this->mockMethodReflection = $this->getMock(\TYPO3\CMS\Extbase\Reflection\MethodReflection::class, array(), array(), '', FALSE);
50 $this->command->expects($this->any())->method('getCommandMethodReflection')->will($this->returnValue($this->mockMethodReflection));
51 $this->mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
52 $this->command->_set('objectManager', $this->mockObjectManager);
53 }
54
55 /**
56 * @return array
57 * @author Robert Lemke <robert@typo3.org>
58 */
59 public function commandIdentifiers() {
60 return array(
61 array('Tx_ExtensionKey_Command_CacheCommandController', 'flush', 'extension_key:cache:flush'),
62 array('Tx_Ext_Command_CookieCommandController', 'bake', 'ext:cookie:bake')
63 );
64 }
65
66 /**
67 * @test
68 * @dataProvider commandIdentifiers
69 * @author Robert Lemke <robert@typo3.org>
70 * @param string $controllerClassName
71 * @param string $commandName
72 * @param string $expectedCommandIdentifier
73 */
74 public function constructRendersACommandIdentifierByTheGivenControllerAndCommandName($controllerClassName, $commandName, $expectedCommandIdentifier) {
75 $command = new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, $commandName);
76 $this->assertEquals($expectedCommandIdentifier, $command->getCommandIdentifier());
77 }
78
79 /**
80 * @return array
81 */
82 public function invalidCommandClassNames() {
83 return array(
84 array(''),
85 // CommandClassName must not be empty
86 array('Tx_OtherExtensionKey_Foo_Faa_Fuuum_Command_CoffeeCommandController'),
87 // CommandControllers in subpackages are not supported
88 array('Foo')
89 );
90 }
91
92 /**
93 * @test
94 * @dataProvider invalidCommandClassNames
95 * @expectedException \InvalidArgumentException
96 * @param string $controllerClassName
97 */
98 public function constructThrowsExceptionIfCommandClassNameIsInvalid($controllerClassName) {
99 new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, 'foo');
100 }
101
102 /**
103 * @test
104 * @author Bastian Waidelich <bastian@typo3.org>
105 */
106 public function hasArgumentsReturnsFalseIfCommandExpectsNoArguments() {
107 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array()));
108 $this->assertFalse($this->command->hasArguments());
109 }
110
111 /**
112 * @test
113 * @author Bastian Waidelich <bastian@typo3.org>
114 */
115 public function hasArgumentsReturnsTrueIfCommandExpectsArguments() {
116 $mockParameterReflection = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ParameterReflection::class, array(), array(), '', FALSE);
117 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array($mockParameterReflection)));
118 $this->assertTrue($this->command->hasArguments());
119 }
120
121 /**
122 * @test
123 * @author Bastian Waidelich <bastian@typo3.org>
124 */
125 public function getArgumentDefinitionsReturnsEmptyArrayIfCommandExpectsNoArguments() {
126 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array()));
127 $this->assertSame(array(), $this->command->getArgumentDefinitions());
128 }
129
130 /**
131 * @test
132 * @author Bastian Waidelich <bastian@typo3.org>
133 */
134 public function getArgumentDefinitionsReturnsArrayOfArgumentDefinitionIfCommandExpectsArguments() {
135 $mockParameterReflection = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ParameterReflection::class, array(), array(), '', FALSE);
136 $mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
137 $mockMethodParameters = array('argument1' => array('optional' => FALSE), 'argument2' => array('optional' => TRUE));
138 $mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue($mockMethodParameters));
139 $this->command->_set('reflectionService', $mockReflectionService);
140 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array($mockParameterReflection)));
141 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getTagsValues')->will($this->returnValue(array('param' => array('@param $argument1 argument1 description', '@param $argument2 argument2 description'))));
142 $mockCommandArgumentDefinition1 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, array(), array(), '', FALSE);
143 $mockCommandArgumentDefinition2 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, array(), array(), '', FALSE);
144 $this->mockObjectManager->expects($this->at(0))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, 'argument1', TRUE, 'argument1 description')->will($this->returnValue($mockCommandArgumentDefinition1));
145 $this->mockObjectManager->expects($this->at(1))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, 'argument2', FALSE, 'argument2 description')->will($this->returnValue($mockCommandArgumentDefinition2));
146 $expectedResult = array($mockCommandArgumentDefinition1, $mockCommandArgumentDefinition2);
147 $actualResult = $this->command->getArgumentDefinitions();
148 $this->assertSame($expectedResult, $actualResult);
149 }
150 }