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