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