[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / RequestTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc;
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 use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException;
17 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18
19 /**
20 * Test case
21 */
22 class RequestTest extends UnitTestCase
23 {
24 /**
25 * @test
26 */
27 public function aSingleArgumentCanBeSetWithSetArgumentAndRetrievedWithGetArgument()
28 {
29 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
30 $request->setArgument('someArgumentName', 'theValue');
31 $this->assertEquals('theValue', $request->getArgument('someArgumentName'));
32 }
33
34 /**
35 * @test
36 */
37 public function setArgumentThrowsExceptionIfTheGivenArgumentNameIsNoString()
38 {
39 $this->expectException(InvalidArgumentNameException::class);
40 $this->expectExceptionCode(1210858767);
41 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
42 $request->setArgument(123, 'theValue');
43 }
44
45 /**
46 * @test
47 */
48 public function setArgumentThrowsExceptionIfTheGivenArgumentNameIsAnEmptyString()
49 {
50 $this->expectException(InvalidArgumentNameException::class);
51 $this->expectExceptionCode(1210858767);
52 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
53 $request->setArgument('', 'theValue');
54 }
55
56 /**
57 * @test
58 */
59 public function setArgumentsOverridesAllExistingArguments()
60 {
61 $arguments = ['key1' => 'value1', 'key2' => 'value2'];
62 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
63 $request->setArgument('someKey', 'shouldBeOverridden');
64 $request->setArguments($arguments);
65 $actualResult = $request->getArguments();
66 $this->assertEquals($arguments, $actualResult);
67 }
68
69 /**
70 * @test
71 */
72 public function setArgumentsCallsSetArgumentForEveryArrayEntry()
73 {
74 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
75 ->setMethods(['setArgument'])
76 ->getMock();
77 $request->expects($this->at(0))->method('setArgument')->with('key1', 'value1');
78 $request->expects($this->at(1))->method('setArgument')->with('key2', 'value2');
79 $request->setArguments(['key1' => 'value1', 'key2' => 'value2']);
80 }
81
82 /**
83 * @test
84 */
85 public function setArgumentShouldSetControllerExtensionNameIfPackageKeyIsGiven()
86 {
87 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
88 ->setMethods(['setControllerExtensionName'])
89 ->getMock();
90 $request->expects($this->any())->method('setControllerExtensionName')->with('MyExtension');
91 $request->setArgument('@extension', 'MyExtension');
92 $this->assertFalse($request->hasArgument('@extension'));
93 }
94
95 /**
96 * @test
97 */
98 public function setArgumentShouldSetControllerSubpackageKeyIfSubpackageKeyIsGiven()
99 {
100 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
101 ->setMethods(['setControllerSubpackageKey'])
102 ->getMock();
103 $request->expects($this->any())->method('setControllerSubpackageKey')->with('MySubPackage');
104 $request->setArgument('@subpackage', 'MySubPackage');
105 $this->assertFalse($request->hasArgument('@subpackage'));
106 }
107
108 /**
109 * @test
110 */
111 public function setArgumentShouldSetControllerNameIfControllerIsGiven()
112 {
113 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
114 ->setMethods(['setControllerName'])
115 ->getMock();
116 $request->expects($this->any())->method('setControllerName')->with('MyController');
117 $request->setArgument('@controller', 'MyController');
118 $this->assertFalse($request->hasArgument('@controller'));
119 }
120
121 /**
122 * @test
123 */
124 public function setArgumentShouldSetControllerActionNameIfActionIsGiven()
125 {
126 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
127 ->setMethods(['setControllerActionName'])
128 ->getMock();
129 $request->expects($this->any())->method('setControllerActionName')->with('foo');
130 $request->setArgument('@action', 'foo');
131 $this->assertFalse($request->hasArgument('@action'));
132 }
133
134 /**
135 * @test
136 */
137 public function setArgumentShouldSetFormatIfFormatIsGiven()
138 {
139 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
140 ->setMethods(['setFormat'])
141 ->getMock();
142 $request->expects($this->any())->method('setFormat')->with('txt');
143 $request->setArgument('@format', 'txt');
144 $this->assertFalse($request->hasArgument('@format'));
145 }
146
147 /**
148 * @test
149 */
150 public function setArgumentShouldSetVendorIfVendorIsGiven()
151 {
152 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
153 ->setMethods(['setFormat', 'setVendor'])
154 ->getMock();
155 $request->expects($this->any())->method('setVendor')->with('VENDOR');
156 $request->setArgument('@vendor', 'VENDOR');
157 $this->assertFalse($request->hasArgument('@vendor'));
158 }
159
160 /**
161 * @test
162 */
163 public function internalArgumentsShouldNotBeReturnedAsNormalArgument()
164 {
165 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
166 $request->setArgument('__referrer', 'foo');
167 $this->assertFalse($request->hasArgument('__referrer'));
168 }
169
170 /**
171 * @test
172 */
173 public function internalArgumentsShouldBeStoredAsInternalArguments()
174 {
175 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
176 $request->setArgument('__referrer', 'foo');
177 $this->assertSame('foo', $request->getInternalArgument('__referrer'));
178 }
179
180 /**
181 * @test
182 */
183 public function hasInternalArgumentShouldReturnNullIfArgumentNotFound()
184 {
185 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
186 $this->assertNull($request->getInternalArgument('__nonExistingInternalArgument'));
187 }
188
189 /**
190 * @test
191 */
192 public function setArgumentAcceptsObjectIfArgumentIsInternal()
193 {
194 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
195 $object = new \stdClass();
196 $request->setArgument('__theKey', $object);
197 $this->assertSame($object, $request->getInternalArgument('__theKey'));
198 }
199
200 /**
201 * @test
202 */
203 public function multipleArgumentsCanBeSetWithSetArgumentsAndRetrievedWithGetArguments()
204 {
205 $arguments = [
206 'firstArgument' => 'firstValue',
207 'dænishÅrgument' => 'görman välju',
208 '3a' => '3v'
209 ];
210 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
211 $request->setArguments($arguments);
212 $this->assertEquals($arguments, $request->getArguments());
213 }
214
215 /**
216 * @test
217 */
218 public function hasArgumentTellsIfAnArgumentExists()
219 {
220 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
221 $request->setArgument('existingArgument', 'theValue');
222 $this->assertTrue($request->hasArgument('existingArgument'));
223 $this->assertFalse($request->hasArgument('notExistingArgument'));
224 }
225
226 /**
227 * @test
228 */
229 public function theActionNameCanBeSetAndRetrieved()
230 {
231 $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)
232 ->setMethods(['getControllerObjectName'])
233 ->disableOriginalConstructor()
234 ->getMock();
235 $request->expects($this->once())->method('getControllerObjectName')->will($this->returnValue(''));
236 $request->setControllerActionName('theAction');
237 $this->assertEquals('theAction', $request->getControllerActionName());
238 }
239
240 /**
241 * @test
242 */
243 public function theRepresentationFormatCanBeSetAndRetrieved()
244 {
245 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
246 $request->setFormat('html');
247 $this->assertEquals('html', $request->getFormat());
248 }
249
250 /**
251 * @test
252 */
253 public function aFlagCanBeSetIfTheRequestNeedsToBeDispatchedAgain()
254 {
255 $request = new \TYPO3\CMS\Extbase\Mvc\Request();
256 $this->assertFalse($request->isDispatched());
257 $request->setDispatched(true);
258 $this->assertTrue($request->isDispatched());
259 }
260
261 /**
262 * DataProvider for explodeObjectControllerName
263 *
264 * @return array
265 */
266 public function controllerArgumentsAndExpectedObjectName()
267 {
268 return [
269 'Vendor TYPO3\CMS, extension, controller given' => [
270 [
271 'vendorName' => 'TYPO3\\CMS',
272 'extensionName' => 'Ext',
273 'subpackageKey' => '',
274 'controllerName' => 'Foo',
275 ],
276 'TYPO3\\CMS\\Ext\\Controller\\FooController',
277 ],
278 'Vendor TYPO3\CMS, extension, subpackage, controlle given' => [
279 [
280 'vendorName' => 'TYPO3\\CMS',
281 'extensionName' => 'Fluid',
282 'subpackageKey' => 'ViewHelpers\\Widget',
283 'controllerName' => 'Paginate',
284 ],
285 \TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController::class,
286 ],
287 'Vendor VENDOR, extension, controller given' => [
288 [
289 'vendorName' => 'VENDOR',
290 'extensionName' => 'Ext',
291 'subpackageKey' => '',
292 'controllerName' => 'Foo',
293 ],
294 'VENDOR\\Ext\\Controller\\FooController',
295 ],
296 'Vendor VENDOR, extension subpackage, controller given' => [
297 [
298 'vendorName' => 'VENDOR',
299 'extensionName' => 'Ext',
300 'subpackageKey' => 'ViewHelpers\\Widget',
301 'controllerName' => 'Foo',
302 ],
303 'VENDOR\\Ext\\ViewHelpers\\Widget\\Controller\\FooController',
304 ],
305 ];
306 }
307
308 /**
309 * @dataProvider controllerArgumentsAndExpectedObjectName
310 *
311 * @param array $controllerArguments
312 * @param string $controllerObjectName
313 * @test
314 */
315 public function getControllerObjectNameResolvesControllerObjectNameCorrectly($controllerArguments, $controllerObjectName)
316 {
317 /** @var $request \TYPO3\CMS\Extbase\Mvc\Request */
318 $request = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Request::class, ['dummy']);
319 $request->_set('controllerVendorName', $controllerArguments['vendorName']);
320 $request->_set('controllerExtensionName', $controllerArguments['extensionName']);
321 $request->_set('controllerSubpackageKey', $controllerArguments['subpackageKey']);
322 $request->_set('controllerName', $controllerArguments['controllerName']);
323
324 $this->assertEquals($controllerObjectName, $request->getControllerObjectName());
325 }
326
327 /**
328 * @dataProvider controllerArgumentsAndExpectedObjectName
329 *
330 * @param array $controllerArguments
331 * @param string $controllerObjectName
332 * @test
333 */
334 public function setControllerObjectNameResolvesControllerObjectNameArgumentsCorrectly($controllerArguments, $controllerObjectName)
335 {
336 /** @var $request \TYPO3\CMS\Extbase\Mvc\Request */
337 $request = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Request::class, ['dummy']);
338 $request->setControllerObjectName($controllerObjectName);
339
340 $actualControllerArguments = [
341 'vendorName' => $request->_get('controllerVendorName'),
342 'extensionName' => $request->_get('controllerExtensionName'),
343 'subpackageKey' => $request->_get('controllerSubpackageKey'),
344 'controllerName' => $request->_get('controllerName'),
345 ];
346
347 $this->assertSame($controllerArguments, $actualControllerArguments);
348 }
349 }