[BUGFIX] Fix classes without or broken namespace
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Cli / CommandTest.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 Command class
25 */
26 class CommandTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
27
28 /**
29 * @var \TYPO3\CMS\Extbase\Mvc\Cli\Command
30 */
31 protected $command;
32
33 /**
34 * @var \TYPO3\CMS\Extbase\Reflection\MethodReflection
35 */
36 protected $mockMethodReflection;
37
38 /**
39 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
40 */
41 protected $mockObjectManager;
42
43 /**
44 * @return void
45 */
46 public function setUp() {
47 $this->command = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', array('getCommandMethodReflection'), array(), '', FALSE);
48 $this->mockMethodReflection = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\MethodReflection', array(), array(), '', FALSE);
49 $this->command->expects($this->any())->method('getCommandMethodReflection')->will($this->returnValue($this->mockMethodReflection));
50 $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
51 $this->command->injectObjectManager($this->mockObjectManager);
52 }
53
54 /**
55 * @return array
56 * @author Robert Lemke <robert@typo3.org>
57 */
58 public function commandIdentifiers() {
59 return array(
60 array('Tx_ExtensionKey_Command_CacheCommandController', 'flush', 'extension_key:cache:flush'),
61 array('Tx_Ext_Command_CookieCommandController', 'bake', 'ext:cookie:bake')
62 );
63 }
64
65 /**
66 * @test
67 * @dataProvider commandIdentifiers
68 * @author Robert Lemke <robert@typo3.org>
69 * @param string $controllerClassName
70 * @param string $commandName
71 * @param string $expectedCommandIdentifier
72 */
73 public function constructRendersACommandIdentifierByTheGivenControllerAndCommandName($controllerClassName, $commandName, $expectedCommandIdentifier) {
74 $command = new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, $commandName);
75 $this->assertEquals($expectedCommandIdentifier, $command->getCommandIdentifier());
76 }
77
78 /**
79 * @return array
80 */
81 public function invalidCommandClassNames() {
82 return array(
83 array(''),
84 // CommandClassName must not be empty
85 array('Tx_OtherExtensionKey_Foo_Faa_Fuuum_Command_CoffeeCommandController'),
86 // CommandControllers in subpackages are not supported
87 array('Foo')
88 );
89 }
90
91 /**
92 * @test
93 * @dataProvider invalidCommandClassNames
94 * @expectedException \InvalidArgumentException
95 * @param string $controllerClassName
96 */
97 public function constructThrowsExceptionIfCommandClassNameIsInvalid($controllerClassName) {
98 new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, 'foo');
99 }
100
101 /**
102 * @test
103 * @author Bastian Waidelich <bastian@typo3.org>
104 */
105 public function hasArgumentsReturnsFalseIfCommandExpectsNoArguments() {
106 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array()));
107 $this->assertFalse($this->command->hasArguments());
108 }
109
110 /**
111 * @test
112 * @author Bastian Waidelich <bastian@typo3.org>
113 */
114 public function hasArgumentsReturnsTrueIfCommandExpectsArguments() {
115 $mockParameterReflection = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ParameterReflection', array(), array(), '', FALSE);
116 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array($mockParameterReflection)));
117 $this->assertTrue($this->command->hasArguments());
118 }
119
120 /**
121 * @test
122 * @author Bastian Waidelich <bastian@typo3.org>
123 */
124 public function getArgumentDefinitionsReturnsEmptyArrayIfCommandExpectsNoArguments() {
125 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array()));
126 $this->assertSame(array(), $this->command->getArgumentDefinitions());
127 }
128
129 /**
130 * @test
131 * @author Bastian Waidelich <bastian@typo3.org>
132 */
133 public function getArgumentDefinitionsReturnsArrayOfArgumentDefinitionIfCommandExpectsArguments() {
134 $mockParameterReflection = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ParameterReflection', array(), array(), '', FALSE);
135 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\Service');
136 $mockMethodParameters = array('argument1' => array('optional' => FALSE), 'argument2' => array('optional' => TRUE));
137 $mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue($mockMethodParameters));
138 $this->command->injectReflectionService($mockReflectionService);
139 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array($mockParameterReflection)));
140 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getTagsValues')->will($this->returnValue(array('param' => array('@param $argument1 argument1 description', '@param $argument2 argument2 description'))));
141 $mockCommandArgumentDefinition1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandArgumentDefinition', array(), array(), '', FALSE);
142 $mockCommandArgumentDefinition2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandArgumentDefinition', array(), array(), '', FALSE);
143 $this->mockObjectManager->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandArgumentDefinition', 'argument1', TRUE, 'argument1 description')->will($this->returnValue($mockCommandArgumentDefinition1));
144 $this->mockObjectManager->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandArgumentDefinition', 'argument2', FALSE, 'argument2 description')->will($this->returnValue($mockCommandArgumentDefinition2));
145 $expectedResult = array($mockCommandArgumentDefinition1, $mockCommandArgumentDefinition2);
146 $actualResult = $this->command->getArgumentDefinitions();
147 $this->assertSame($expectedResult, $actualResult);
148 }
149
150 }
151
152
153 ?>