[+FEATURE] Backport CommandController Implementation
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / MVC / CLI / CommandManagerTest.php
1 <?php
2 /* *
3 * This script belongs to the Extbase framework. *
4 * *
5 * It is free software; you can redistribute it and/or modify it under *
6 * the terms of the GNU Lesser General Public License as published by the *
7 * Free Software Foundation, either version 3 of the License, or (at your *
8 * option) any later version. *
9 * *
10 * This script is distributed in the hope that it will be useful, but *
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
12 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
13 * General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Lesser General Public *
16 * License along with the script. *
17 * If not, see http://www.gnu.org/licenses/lgpl.html *
18 * *
19 * The TYPO3 project - inspiring people to share! *
20 * */
21
22 require_once(__DIR__ . '/../Fixture/CLI/Command/MockCommandController.php');
23
24 /**
25 * Testcase for the CLI CommandManager class
26 */
27 class Tx_Extbase_Tests_Unit_MVC_CLI_CommandManagerTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
28
29 /**
30 * @var array
31 */
32 protected $commandControllerBackup = array();
33
34 /**
35 * @var Tx_Extbase_Object_ObjectManagerInterface
36 */
37 protected $mockObjectManager;
38
39 /**
40 * @var Tx_Extbase_MVC_CLI_CommandManager
41 */
42 protected $commandManager;
43
44 public function setUp() {
45 $this->commandControllerBackup = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'];
46 $this->commandManager = $this->getMock('Tx_Extbase_MVC_CLI_CommandManager', array('getAvailableCommands'));
47 $this->mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
48 $this->commandManager->injectObjectManager($this->mockObjectManager);
49 }
50
51 public function tearDown() {
52 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] = $this->commandControllerBackup;
53 }
54
55 /**
56 * @test
57 * @author Bastian Waidelich <bastian@typo3.org>
58 */
59 public function getAvailableCommandsReturnsAllAvailableCommands() {
60 $commandManager = new Tx_Extbase_MVC_CLI_CommandManager();
61 $commandManager->injectObjectManager($this->mockObjectManager);
62 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] = array(
63 'Tx_Extbase_MVC_Fixture_CLI_Command_MockACommandController',
64 'Tx_Extbase_MVC_Fixture_CLI_Command_MockBCommandController'
65 );
66 $mockCommand1 = $this->getMock('Tx_Extbase_MVC_CLI_Command', array(), array(), '', FALSE);
67 $mockCommand2 = $this->getMock('Tx_Extbase_MVC_CLI_Command', array(), array(), '', FALSE);
68 $mockCommand3 = $this->getMock('Tx_Extbase_MVC_CLI_Command', array(), array(), '', FALSE);
69 $this->mockObjectManager->expects($this->at(0))->method('get')->with('Tx_Extbase_MVC_CLI_Command', 'Tx_Extbase_MVC_Fixture_CLI_Command_MockACommandController', 'foo')->will($this->returnValue($mockCommand1));
70 $this->mockObjectManager->expects($this->at(1))->method('get')->with('Tx_Extbase_MVC_CLI_Command', 'Tx_Extbase_MVC_Fixture_CLI_Command_MockACommandController', 'bar')->will($this->returnValue($mockCommand2));
71 $this->mockObjectManager->expects($this->at(2))->method('get')->with('Tx_Extbase_MVC_CLI_Command', 'Tx_Extbase_MVC_Fixture_CLI_Command_MockBCommandController', 'baz')->will($this->returnValue($mockCommand3));
72
73 $commands = $commandManager->getAvailableCommands();
74 $this->assertEquals(3, count($commands));
75 $this->assertSame($mockCommand1, $commands[0]);
76 $this->assertSame($mockCommand2, $commands[1]);
77 $this->assertSame($mockCommand3, $commands[2]);
78 }
79
80 /**
81 * @test
82 * @author Bastian Waidelich <bastian@typo3.org>
83 */
84 public function getCommandByIdentifierReturnsCommandIfIdentifierIsEqual() {
85 $mockCommand = $this->getMock('Tx_Extbase_MVC_CLI_Command', array(), array(), '', FALSE);
86 $mockCommand->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
87 $mockCommands = array($mockCommand);
88 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
89
90 $this->assertSame($mockCommand, $this->commandManager->getCommandByIdentifier('extensionkey:controller:command'));
91 }
92
93 /**
94 * @test
95 * @author Bastian Waidelich <bastian@typo3.org>
96 */
97 public function getCommandByIdentifierWorksCaseInsensitive() {
98 $mockCommand = $this->getMock('Tx_Extbase_MVC_CLI_Command', array(), array(), '', FALSE);
99 $mockCommand->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
100 $mockCommands = array($mockCommand);
101 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
102
103 $this->assertSame($mockCommand, $this->commandManager->getCommandByIdentifier(' ExtensionKey:conTroLler:Command '));
104 }
105
106 /**
107 * @test
108 * @expectedException Tx_Extbase_MVC_Exception_NoSuchCommand
109 * @author Bastian Waidelich <bastian@typo3.org>
110 */
111 public function getCommandByIdentifierThrowsExceptionIfNoMatchingCommandWasFound() {
112 $mockCommand = $this->getMock('Tx_Extbase_MVC_CLI_Command', array(), array(), '', FALSE);
113 $mockCommand->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
114 $mockCommands = array($mockCommand);
115 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
116
117 $this->commandManager->getCommandByIdentifier('extensionkey:controller:someothercommand');
118 }
119
120 /**
121 * @test
122 * @expectedException Tx_Extbase_MVC_Exception_AmbiguousCommandIdentifier
123 * @author Bastian Waidelich <bastian@typo3.org>
124 */
125 public function getCommandByIdentifierThrowsExceptionIfMoreThanOneMatchingCommandWasFound() {
126 $mockCommand1 = $this->getMock('Tx_Extbase_MVC_CLI_Command', array(), array(), '', FALSE);
127 $mockCommand1->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command'));
128 $mockCommand2 = $this->getMock('Tx_Extbase_MVC_CLI_Command', array(), array(), '', FALSE);
129 $mockCommand2->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('otherextensionkey:controller:command'));
130 $mockCommands = array($mockCommand1, $mockCommand2);
131 $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands));
132
133 $this->commandManager->getCommandByIdentifier('controller:command');
134 }
135
136 }
137 ?>