[!!!][TASK] Remove support for non namespaced classes in Extbase
[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 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Extbase\Mvc\Cli\Command;
26 use TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition;
27 use TYPO3\CMS\Extbase\Object\ObjectManager;
28 use TYPO3\CMS\Extbase\Tests\Unit\Mvc\Cli\Fixture\Command\MockCCommandController;
29
30 /**
31 * Test case
32 */
33 class CommandTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
34 {
35 /**
36 * @var array
37 */
38 protected $singletonInstances;
39
40 protected function setUp()
41 {
42 $this->singletonInstances = GeneralUtility::getSingletonInstances();
43 GeneralUtility::purgeInstances();
44 }
45
46 protected function tearDown()
47 {
48 GeneralUtility::resetSingletonInstances($this->singletonInstances);
49 parent::tearDown();
50 }
51
52 /**
53 * @return array
54 */
55 public function commandIdentifiers()
56 {
57 return [
58
59 ['Tx\ExtensionKey\Command\CacheCommandController', 'flush', 'extension_key:cache:flush'],
60 ['Tx\Ext\Command\CookieCommandController', 'bake', 'ext:cookie:bake'],
61 ['Tx\OtherExtensionKey\Foo\Faa\Fuuum\Command\CoffeeCommandController', 'brew', 'other_extension_key:coffee:brew'],
62 ];
63 }
64
65 /**
66 * @test
67 * @dataProvider commandIdentifiers
68 * @param string $controllerClassName
69 * @param string $commandName
70 * @param string $expectedCommandIdentifier
71 */
72 public function constructRendersACommandIdentifierByTheGivenControllerAndCommandName($controllerClassName, $commandName, $expectedCommandIdentifier)
73 {
74 $command = new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, $commandName);
75 $this->assertEquals($expectedCommandIdentifier, $command->getCommandIdentifier());
76 }
77
78 /**
79 * @return array
80 */
81 public function invalidCommandClassNames()
82 {
83 return [
84 [''],
85 // CommandClassName must not be empty
86 ['Foo']
87 ];
88 }
89
90 /**
91 * @test
92 * @dataProvider invalidCommandClassNames
93 * @param string $controllerClassName
94 */
95 public function constructThrowsExceptionIfCommandClassNameIsInvalid($controllerClassName)
96 {
97 $this->expectException(\InvalidArgumentException::class);
98 $this->expectExceptionCode(1438782187);
99 new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, 'foo');
100 }
101
102 public function testIsInternal()
103 {
104 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
105 Command::class,
106 MockCCommandController::class,
107 'empty'
108 );
109
110 static::assertFalse($commandController->isInternal());
111
112 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
113 Command::class,
114 MockCCommandController::class,
115 'internal'
116 );
117
118 static::assertTrue($commandController->isInternal());
119 }
120
121 public function testIsCliOnly()
122 {
123 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
124 Command::class,
125 MockCCommandController::class,
126 'empty'
127 );
128
129 static::assertFalse($commandController->isCliOnly());
130
131 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
132 Command::class,
133 MockCCommandController::class,
134 'cliOnly'
135 );
136
137 static::assertTrue($commandController->isCliOnly());
138 }
139
140 public function testIsFlushinCaches()
141 {
142 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
143 Command::class,
144 MockCCommandController::class,
145 'empty'
146 );
147
148 static::assertFalse($commandController->isFlushingCaches());
149
150 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
151 Command::class,
152 MockCCommandController::class,
153 'flushingCaches'
154 );
155
156 static::assertTrue($commandController->isFlushingCaches());
157 }
158
159 public function testHasArguments()
160 {
161 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
162 Command::class,
163 MockCCommandController::class,
164 'empty'
165 );
166
167 static::assertFalse($commandController->hasArguments());
168
169 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
170 Command::class,
171 MockCCommandController::class,
172 'withArguments'
173 );
174
175 static::assertTrue($commandController->hasArguments());
176 }
177
178 public function testGetArgumentDefinitions()
179 {
180 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
181 Command::class,
182 MockCCommandController::class,
183 'empty'
184 );
185
186 static::assertSame([], $commandController->getArgumentDefinitions());
187
188 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
189 Command::class,
190 MockCCommandController::class,
191 'withArguments'
192 );
193
194 $expected = [
195 new CommandArgumentDefinition('foo', true, 'FooParamDescription'),
196 new CommandArgumentDefinition('bar', false, 'BarParamDescription'),
197 ];
198
199 static::assertEquals($expected, $commandController->getArgumentDefinitions());
200 }
201
202 public function testGetDescription()
203 {
204 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
205 Command::class,
206 MockCCommandController::class,
207 'empty'
208 );
209
210 static::assertSame('', $commandController->getDescription());
211
212 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
213 Command::class,
214 MockCCommandController::class,
215 'withDescription'
216 );
217
218 $expected = 'Longer Description' . PHP_EOL .
219 'Multine' . PHP_EOL . PHP_EOL .
220 'Much Multiline';
221
222 static::assertEquals($expected, $commandController->getDescription());
223 }
224
225 public function testGetShortDescription()
226 {
227 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
228 Command::class,
229 MockCCommandController::class,
230 'empty'
231 );
232
233 static::assertSame('', $commandController->getShortDescription());
234
235 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
236 Command::class,
237 MockCCommandController::class,
238 'withDescription'
239 );
240
241 $expected = 'Short Description';
242
243 static::assertEquals($expected, $commandController->getShortDescription());
244 }
245
246 public function testGetRelatedCommandIdentifiers()
247 {
248 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
249 Command::class,
250 MockCCommandController::class,
251 'empty'
252 );
253
254 static::assertSame([], $commandController->getRelatedCommandIdentifiers());
255
256 $commandController = GeneralUtility::makeInstance(ObjectManager::class)->get(
257 Command::class,
258 MockCCommandController::class,
259 'relatedCommandIdentifiers'
260 );
261
262 $expected = ['Foo:Bar:Baz'];
263 static::assertEquals($expected, $commandController->getRelatedCommandIdentifiers());
264 }
265 }