[BUGFIX] CLI argument handling chokes on = use
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / MVC / RequestTest.php
1 <?php
2
3 /* *
4 * This script belongs to the Extbase framework. *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * Testcase for the MVC Generic Request
25 *
26 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
27 */
28 class Tx_Extbase_Tests_Unit_MVC_RequestTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
29
30 /**
31 * @test
32 * @author Robert Lemke <robert@typo3.org>
33 */
34 public function aSingleArgumentCanBeSetWithSetArgumentAndRetrievedWithGetArgument() {
35 $request = new Tx_Extbase_MVC_Request();
36 $request->setArgument('someArgumentName', 'theValue');
37 $this->assertEquals('theValue', $request->getArgument('someArgumentName'));
38 }
39
40 /**
41 * @test
42 * @expectedException Tx_Extbase_MVC_Exception_InvalidArgumentName
43 * @author Bastian Waidelich <bastian@typo3.org>
44 */
45 public function setArgumentThrowsExceptionIfTheGivenArgumentNameIsNoString() {
46 $request = new Tx_Extbase_MVC_Request();
47 $request->setArgument(123, 'theValue');
48 }
49
50 /**
51 * @test
52 * @expectedException Tx_Extbase_MVC_Exception_InvalidArgumentName
53 * @author Bastian Waidelich <bastian@typo3.org>
54 */
55 public function setArgumentThrowsExceptionIfTheGivenArgumentNameIsAnEmptyString() {
56 $request = new Tx_Extbase_MVC_Request();
57 $request->setArgument('', 'theValue');
58 }
59
60 /**
61 * @test
62 * @expectedException Tx_Extbase_MVC_Exception_InvalidArgumentTypeException
63 * @author Bastian Waidelich <bastian@typo3.org>
64 */
65 public function setArgumentThrowsExceptionIfTheGivenArgumentValueIsAnObject() {
66 $this->markTestSkipped('Differing behavior from FLOW3 because of backwards compatibility reasons.');
67 $request = new Tx_Extbase_MVC_Request();
68 $request->setArgument('theKey', new stdClass());
69 }
70
71 /**
72 * @test
73 * @author Bastian Waidelich <bastian@typo3.org>
74 */
75 public function setArgumentsOverridesAllExistingArguments() {
76 $arguments = array('key1' => 'value1', 'key2' => 'value2');
77 $request = new Tx_Extbase_MVC_Request();
78 $request->setArgument('someKey', 'shouldBeOverridden');
79 $request->setArguments($arguments);
80
81 $actualResult = $request->getArguments();
82 $this->assertEquals($arguments, $actualResult);
83 }
84
85 /**
86 * @test
87 * @author Bastian Waidelich <bastian@typo3.org>
88 */
89 public function setArgumentsCallsSetArgumentForEveryArrayEntry() {
90 $request = $this->getMock('Tx_Extbase_MVC_Request', array('setArgument'));
91 $request->expects($this->at(0))->method('setArgument')->with('key1', 'value1');
92 $request->expects($this->at(1))->method('setArgument')->with('key2', 'value2');
93 $request->setArguments(array('key1' => 'value1', 'key2' => 'value2'));
94 }
95
96 /**
97 * @test
98 */
99 public function setArgumentShouldSetControllerExtensionNameIfPackageKeyIsGiven() {
100 $request = $this->getMock('Tx_Extbase_MVC_Request', array('setControllerExtensionName'));
101 $request->expects($this->any())->method('setControllerExtensionName')->with('MyExtension');
102 $request->setArgument('@extension', 'MyExtension');
103 $this->assertFalse($request->hasArgument('@extension'));
104 }
105
106 /**
107 * @test
108 */
109 public function setArgumentShouldSetControllerSubpackageKeyIfSubpackageKeyIsGiven() {
110 $request = $this->getMock('Tx_Extbase_MVC_Request', array('setControllerSubpackageKey'));
111 $request->expects($this->any())->method('setControllerSubpackageKey')->with('MySubPackage');
112 $request->setArgument('@subpackage', 'MySubPackage');
113 $this->assertFalse($request->hasArgument('@subpackage'));
114 }
115
116 /**
117 * @test
118 */
119 public function setArgumentShouldSetControllerNameIfControllerIsGiven() {
120 $request = $this->getMock('Tx_Extbase_MVC_Request', array('setControllerName'));
121 $request->expects($this->any())->method('setControllerName')->with('MyController');
122 $request->setArgument('@controller', 'MyController');
123 $this->assertFalse($request->hasArgument('@controller'));
124 }
125
126 /**
127 * @test
128 */
129 public function setArgumentShouldSetControllerActionNameIfActionIsGiven() {
130 $request = $this->getMock('Tx_Extbase_MVC_Request', array('setControllerActionName'));
131 $request->expects($this->any())->method('setControllerActionName')->with('foo');
132 $request->setArgument('@action', 'foo');
133 $this->assertFalse($request->hasArgument('@action'));
134 }
135
136 /**
137 * @test
138 */
139 public function setArgumentShouldSetFormatIfFormatIsGiven() {
140 $request = $this->getMock('Tx_Extbase_MVC_Request', array('setFormat'));
141 $request->expects($this->any())->method('setFormat')->with('txt');
142 $request->setArgument('@format', 'txt');
143 $this->assertFalse($request->hasArgument('@format'));
144 }
145
146 /**
147 * @test
148 */
149 public function internalArgumentsShouldNotBeReturnedAsNormalArgument() {
150 $request = new Tx_Extbase_MVC_Request();
151 $request->setArgument('__referrer', 'foo');
152 $this->assertFalse($request->hasArgument('__referrer'));
153 }
154
155 /**
156 * @test
157 */
158 public function internalArgumentsShouldBeStoredAsInternalArguments() {
159 $request = new Tx_Extbase_MVC_Request();
160 $request->setArgument('__referrer', 'foo');
161 $this->assertSame('foo', $request->getInternalArgument('__referrer'));
162 }
163
164 /**
165 * @test
166 */
167 public function hasInternalArgumentShouldReturnNullIfArgumentNotFound() {
168 $request = new Tx_Extbase_MVC_Request();
169 $this->assertNull($request->getInternalArgument('__nonExistingInternalArgument'));
170 }
171
172 /**
173 * @test
174 * @author Sebastian Kurfürst <sebastian@typo3.org>
175 */
176 public function setArgumentAcceptsObjectIfArgumentIsInternal() {
177 $request = new Tx_Extbase_MVC_Request();
178 $object = new stdClass();
179 $request->setArgument('__theKey', $object);
180 $this->assertSame($object, $request->getInternalArgument('__theKey'));
181 }
182
183 /**
184 * @test
185 * @author Robert Lemke <robert@typo3.org>
186 */
187 public function multipleArgumentsCanBeSetWithSetArgumentsAndRetrievedWithGetArguments() {
188 $arguments = array(
189 'firstArgument' => 'firstValue',
190 'dænishÅrgument' => 'görman välju',
191 '3a' => '3v'
192 );
193 $request = new Tx_Extbase_MVC_Request();
194 $request->setArguments($arguments);
195 $this->assertEquals($arguments, $request->getArguments());
196 }
197
198 /**
199 * @test
200 * @author Robert Lemke <robert@typo3.org>
201 */
202 public function hasArgumentTellsIfAnArgumentExists() {
203 $request = new Tx_Extbase_MVC_Request();
204 $request->setArgument('existingArgument', 'theValue');
205
206 $this->assertTrue($request->hasArgument('existingArgument'));
207 $this->assertFalse($request->hasArgument('notExistingArgument'));
208 }
209
210 /**
211 * @test
212 * @author Robert Lemke <robert@typo3.org>
213 */
214 public function theActionNameCanBeSetAndRetrieved() {
215 $request = $this->getMock('Tx_Extbase_MVC_Request', array('getControllerObjectName'), array(), '', FALSE);
216 $request->expects($this->once())->method('getControllerObjectName')->will($this->returnValue(''));
217
218 $request->setControllerActionName('theAction');
219 $this->assertEquals('theAction', $request->getControllerActionName());
220 }
221
222 /**
223 * @test
224 * @author Robert Lemke <robert@typo3.org>
225 */
226 public function theRepresentationFormatCanBeSetAndRetrieved() {
227 $request = new Tx_Extbase_MVC_Request();
228 $request->setFormat('html');
229 $this->assertEquals('html', $request->getFormat());
230 }
231
232 /**
233 * @test
234 */
235 public function theRepresentationFormatIsAutomaticallyLowercased() {
236 $this->markTestSkipped('Different behavior from FLOW3 because of backwards compatibility.');
237 $request = new Tx_Extbase_MVC_Request();
238 $request->setFormat('hTmL');
239 $this->assertEquals('html', $request->getFormat());
240 }
241
242 /**
243 * @test
244 * @author Robert Lemke <robert@typo3.org>
245 */
246 public function aFlagCanBeSetIfTheRequestNeedsToBeDispatchedAgain() {
247 $request = new Tx_Extbase_MVC_Request();
248 $this->assertFalse($request->isDispatched());
249
250 $request->setDispatched(TRUE);
251 $this->assertTrue($request->isDispatched());
252 }
253 }
254
255 ?>