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