32aab9551fb9237fd43cfdc98f24c3da84aea110
[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->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandManager', array('getAvailableCommands'));
46 $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
47 $this->commandManager->injectObjectManager($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 $commandManager = new \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager();
60 $commandManager->injectObjectManager($this->mockObjectManager);
61 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] = array(
62 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Mvc\\Cli\\Fixture\\Command\\MockACommandController',
63 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Mvc\\Cli\\Fixture\\Command\\MockBCommandController'
64 );
65 $mockCommand1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', array(), array(), '', FALSE);
66 $mockCommand2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', array(), array(), '', FALSE);
67 $mockCommand3 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', array(), array(), '', FALSE);
68 $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));
69 $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));
70 $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));
71 $commands = $commandManager->getAvailableCommands();
72 $this->assertEquals(3, count($commands));
73 $this->assertSame($mockCommand1, $commands[0]);
74 $this->assertSame($mockCommand2, $commands[1]);
75 $this->assertSame($mockCommand3, $commands[2]);
76 }
77
78 /**
79 * @test
80 * @author Bastian Waidelich <bastian@typo3.org>
81 */
82 public function getCommandByIdentifierReturnsCommandIfIdentifierIsEqual() {
83 $mockCommand = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', array(), array(), '', FALSE);
84 $mockCommand->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
85 $mockCommands = array($mockCommand);
86 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
87 $this->assertSame($mockCommand, $this->commandManager->getCommandByIdentifier('extensionkey:controller:command'));
88 }
89
90 /**
91 * @test
92 * @author Bastian Waidelich <bastian@typo3.org>
93 */
94 public function getCommandByIdentifierWorksCaseInsensitive() {
95 $mockCommand = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', array(), array(), '', FALSE);
96 $mockCommand->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
97 $mockCommands = array($mockCommand);
98 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
99 $this->assertSame($mockCommand, $this->commandManager->getCommandByIdentifier(' ExtensionKey:conTroLler:Command '));
100 }
101
102 /**
103 * @test
104 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException
105 * @author Bastian Waidelich <bastian@typo3.org>
106 */
107 public function getCommandByIdentifierThrowsExceptionIfNoMatchingCommandWasFound() {
108 $mockCommand = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', array(), array(), '', FALSE);
109 $mockCommand->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
110 $mockCommands = array($mockCommand);
111 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
112 $this->commandManager->getCommandByIdentifier('extensionkey:controller:someothercommand');
113 }
114
115 /**
116 * @test
117 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException
118 * @author Bastian Waidelich <bastian@typo3.org>
119 */
120 public function getCommandByIdentifierThrowsExceptionIfMoreThanOneMatchingCommandWasFound() {
121 $mockCommand1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', array(), array(), '', FALSE);
122 $mockCommand1->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
123 $mockCommand2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', array(), array(), '', FALSE);
124 $mockCommand2->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('otherextensionkey:controller:command'));
125 $mockCommands = array($mockCommand1, $mockCommand2);
126 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
127 $this->commandManager->getCommandByIdentifier('controller:command');
128 }
129 }
130
131 ?>