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