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