[FEATURE] Show extensions via CLI
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Cli / CommandManagerTest.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 use TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException;
24 use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException;
25
26 /**
27 * Test case
28 */
29 class CommandManagerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
30 {
31 /**
32 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
33 */
34 protected $mockObjectManager;
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
38 */
39 protected $commandManager;
40
41 protected function setUp()
42 {
43 $this->commandManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class, ['getAvailableCommands']);
44 $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
45 $this->commandManager->_set('objectManager', $this->mockObjectManager);
46 }
47
48 /**
49 * @test
50 */
51 public function getAvailableCommandsReturnsAllAvailableCommands()
52 {
53 /** @var \TYPO3\TestingFramework\Core\AccessibleObjectInterface $commandManager */
54 $commandManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class, ['dummy']);
55 $commandManager->_set('objectManager', $this->mockObjectManager);
56 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] = [
57 \TYPO3\CMS\Extbase\Tests\Unit\Mvc\Cli\Fixture\Command\MockACommandController::class,
58 \TYPO3\CMS\Extbase\Tests\Unit\Mvc\Cli\Fixture\Command\MockBCommandController::class
59 ];
60 $mockCommand1 = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class);
61 $mockCommand2 = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class);
62 $mockCommand3 = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class);
63 $this->mockObjectManager->expects($this->at(0))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, \TYPO3\CMS\Extbase\Tests\Unit\Mvc\Cli\Fixture\Command\MockACommandController::class, 'foo')->will($this->returnValue($mockCommand1));
64 $this->mockObjectManager->expects($this->at(1))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, \TYPO3\CMS\Extbase\Tests\Unit\Mvc\Cli\Fixture\Command\MockACommandController::class, 'bar')->will($this->returnValue($mockCommand2));
65 $this->mockObjectManager->expects($this->at(2))->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, \TYPO3\CMS\Extbase\Tests\Unit\Mvc\Cli\Fixture\Command\MockBCommandController::class, 'baz')->will($this->returnValue($mockCommand3));
66 $commands = $commandManager->getAvailableCommands();
67 $this->assertEquals(3, count($commands));
68 $this->assertSame($mockCommand1, $commands[0]);
69 $this->assertSame($mockCommand2, $commands[1]);
70 $this->assertSame($mockCommand3, $commands[2]);
71 }
72
73 /**
74 * @test
75 */
76 public function getCommandByIdentifierReturnsCommandIfIdentifierIsEqual()
77 {
78 $mockCommand = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class);
79 $mockCommand->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
80 $mockCommands = [$mockCommand];
81 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
82 $this->assertSame($mockCommand, $this->commandManager->getCommandByIdentifier('extensionkey:controller:command'));
83 }
84
85 /**
86 * @test
87 */
88 public function getCommandByIdentifierWorksCaseInsensitive()
89 {
90 $mockCommand = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class);
91 $mockCommand->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
92 $mockCommands = [$mockCommand];
93 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
94 $this->assertSame($mockCommand, $this->commandManager->getCommandByIdentifier(' ExtensionKey:conTroLler:Command '));
95 }
96
97 /**
98 * @test
99 */
100 public function getCommandByIdentifierThrowsExceptionIfNoMatchingCommandWasFound()
101 {
102 $this->expectException(NoSuchCommandException::class);
103 $this->expectExceptionCode(1310556663);
104 $mockCommand = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class);
105 $mockCommand->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
106 $mockCommands = [$mockCommand];
107 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
108 $this->commandManager->getCommandByIdentifier('extensionkey:controller:someothercommand');
109 }
110
111 /**
112 * @test
113 */
114 public function getCommandByIdentifierThrowsExceptionIfMoreThanOneMatchingCommandWasFound()
115 {
116 $this->expectException(AmbiguousCommandIdentifierException::class);
117 $this->expectExceptionCode(1310557169);
118 $mockCommand1 = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class);
119 $mockCommand1->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
120 $mockCommand2 = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class);
121 $mockCommand2->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('otherextensionkey:controller:command'));
122 $mockCommands = [$mockCommand1, $mockCommand2];
123 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
124 $this->commandManager->getCommandByIdentifier('controller:command');
125 }
126 }