[CLEANUP] Rework/simplify copyright header and remove @package
[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 file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test case
19 */
20 class CommandTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 /**
23 * @var \TYPO3\CMS\Extbase\Mvc\Cli\Command|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
24 */
25 protected $command;
26
27 /**
28 * @var \TYPO3\CMS\Extbase\Reflection\MethodReflection
29 */
30 protected $mockMethodReflection;
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
34 */
35 protected $mockObjectManager;
36
37 /**
38 * @return void
39 */
40 public function setUp() {
41 $this->command = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', array('getCommandMethodReflection'), array(), '', FALSE);
42 $this->mockMethodReflection = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\MethodReflection', array(), array(), '', FALSE);
43 $this->command->expects($this->any())->method('getCommandMethodReflection')->will($this->returnValue($this->mockMethodReflection));
44 $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
45 $this->command->_set('objectManager', $this->mockObjectManager);
46 }
47
48 /**
49 * @return array
50 * @author Robert Lemke <robert@typo3.org>
51 */
52 public function commandIdentifiers() {
53 return array(
54 array('Tx_ExtensionKey_Command_CacheCommandController', 'flush', 'extension_key:cache:flush'),
55 array('Tx_Ext_Command_CookieCommandController', 'bake', 'ext:cookie:bake')
56 );
57 }
58
59 /**
60 * @test
61 * @dataProvider commandIdentifiers
62 * @author Robert Lemke <robert@typo3.org>
63 * @param string $controllerClassName
64 * @param string $commandName
65 * @param string $expectedCommandIdentifier
66 */
67 public function constructRendersACommandIdentifierByTheGivenControllerAndCommandName($controllerClassName, $commandName, $expectedCommandIdentifier) {
68 $command = new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, $commandName);
69 $this->assertEquals($expectedCommandIdentifier, $command->getCommandIdentifier());
70 }
71
72 /**
73 * @return array
74 */
75 public function invalidCommandClassNames() {
76 return array(
77 array(''),
78 // CommandClassName must not be empty
79 array('Tx_OtherExtensionKey_Foo_Faa_Fuuum_Command_CoffeeCommandController'),
80 // CommandControllers in subpackages are not supported
81 array('Foo')
82 );
83 }
84
85 /**
86 * @test
87 * @dataProvider invalidCommandClassNames
88 * @expectedException \InvalidArgumentException
89 * @param string $controllerClassName
90 */
91 public function constructThrowsExceptionIfCommandClassNameIsInvalid($controllerClassName) {
92 new \TYPO3\CMS\Extbase\Mvc\Cli\Command($controllerClassName, 'foo');
93 }
94
95 /**
96 * @test
97 * @author Bastian Waidelich <bastian@typo3.org>
98 */
99 public function hasArgumentsReturnsFalseIfCommandExpectsNoArguments() {
100 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array()));
101 $this->assertFalse($this->command->hasArguments());
102 }
103
104 /**
105 * @test
106 * @author Bastian Waidelich <bastian@typo3.org>
107 */
108 public function hasArgumentsReturnsTrueIfCommandExpectsArguments() {
109 $mockParameterReflection = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ParameterReflection', array(), array(), '', FALSE);
110 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array($mockParameterReflection)));
111 $this->assertTrue($this->command->hasArguments());
112 }
113
114 /**
115 * @test
116 * @author Bastian Waidelich <bastian@typo3.org>
117 */
118 public function getArgumentDefinitionsReturnsEmptyArrayIfCommandExpectsNoArguments() {
119 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array()));
120 $this->assertSame(array(), $this->command->getArgumentDefinitions());
121 }
122
123 /**
124 * @test
125 * @author Bastian Waidelich <bastian@typo3.org>
126 */
127 public function getArgumentDefinitionsReturnsArrayOfArgumentDefinitionIfCommandExpectsArguments() {
128 $mockParameterReflection = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ParameterReflection', array(), array(), '', FALSE);
129 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService');
130 $mockMethodParameters = array('argument1' => array('optional' => FALSE), 'argument2' => array('optional' => TRUE));
131 $mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue($mockMethodParameters));
132 $this->command->_set('reflectionService', $mockReflectionService);
133 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getParameters')->will($this->returnValue(array($mockParameterReflection)));
134 $this->mockMethodReflection->expects($this->atLeastOnce())->method('getTagsValues')->will($this->returnValue(array('param' => array('@param $argument1 argument1 description', '@param $argument2 argument2 description'))));
135 $mockCommandArgumentDefinition1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandArgumentDefinition', array(), array(), '', FALSE);
136 $mockCommandArgumentDefinition2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandArgumentDefinition', array(), array(), '', FALSE);
137 $this->mockObjectManager->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandArgumentDefinition', 'argument1', TRUE, 'argument1 description')->will($this->returnValue($mockCommandArgumentDefinition1));
138 $this->mockObjectManager->expects($this->at(1))->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandArgumentDefinition', 'argument2', FALSE, 'argument2 description')->will($this->returnValue($mockCommandArgumentDefinition2));
139 $expectedResult = array($mockCommandArgumentDefinition1, $mockCommandArgumentDefinition2);
140 $actualResult = $this->command->getArgumentDefinitions();
141 $this->assertSame($expectedResult, $actualResult);
142 }
143 }