Extbase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / MVC / Controller / ActionController_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 class Tx_Extbase_MVC_Controller_ActionController_testcase extends Tx_Extbase_Base_testcase {
26
27 /**
28 * @test
29 */
30 public function processRequestSticksToSpecifiedSequence() {
31 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
32 $mockRequest->expects($this->once())->method('setDispatched')->with(TRUE);
33
34 $mockResponse = $this->getMock('Tx_Extbase_MVC_Response', array(), array(), '', FALSE);
35
36 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array(
37 'initializeFooAction', 'initializeAction', 'resolveActionMethodName', 'initializeActionMethodArguments', 'initializeActionMethodValidators', 'mapRequestArgumentsToControllerArguments', 'initializeControllerArgumentsBaseValidators', 'initializeView', 'callActionMethod'));
38 $mockController->expects($this->at(0))->method('resolveActionMethodName')->will($this->returnValue('fooAction'));
39 $mockController->expects($this->at(1))->method('initializeActionMethodArguments');
40 $mockController->expects($this->at(2))->method('initializeActionMethodValidators');
41 $mockController->expects($this->at(3))->method('initializeAction');
42 $mockController->expects($this->at(4))->method('initializeFooAction');
43 $mockController->expects($this->at(5))->method('initializeControllerArgumentsBaseValidators');
44 $mockController->expects($this->at(6))->method('mapRequestArgumentsToControllerArguments');
45 $mockController->expects($this->at(7))->method('initializeView');
46 $mockController->expects($this->at(8))->method('callActionMethod');
47
48 $mockController->processRequest($mockRequest, $mockResponse);
49 $this->assertSame($mockRequest, $mockController->_get('request'));
50 $this->assertSame($mockResponse, $mockController->_get('response'));
51 }
52
53 /**
54 * @test
55 */
56 public function callActionMethodAppendsStringsReturnedByActionMethodToTheResponseObject() {
57 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
58
59 $mockResponse = $this->getMock('Tx_Extbase_MVC_Response', array(), array(), '', FALSE);
60 $mockResponse->expects($this->once())->method('appendContent')->with('the returned string');
61
62 $mockArguments = $this->getMock('Tx_Extbase_Controller_Arguments', array('areValid'), array(), '', FALSE);
63 $mockArguments->expects($this->any())->method('areValid')->will($this->returnValue(TRUE));
64
65 $mockArgumentMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults', array(), array(), '', FALSE);
66 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
67
68 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction', 'initializeAction'), array(), '', FALSE);
69 $mockController->expects($this->once())->method('fooAction')->will($this->returnValue('the returned string'));
70 $mockController->_set('request', $mockRequest);
71 $mockController->_set('response', $mockResponse);
72 $mockController->_set('arguments', $mockArguments);
73 $mockController->_set('actionMethodName', 'fooAction');
74 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
75 $mockController->_call('callActionMethod');
76 }
77
78 /**
79 * @test
80 */
81 public function callActionMethodRendersTheViewAutomaticallyIfTheActionReturnedNullAndAViewExists() {
82 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
83
84 $mockResponse = $this->getMock('Tx_Extbase_MVC_Response', array(), array(), '', FALSE);
85 $mockResponse->expects($this->once())->method('appendContent')->with('the view output');
86
87 $mockView = $this->getMock('Tx_Extbase_MVC_View_ViewInterface');
88 $mockView->expects($this->once())->method('render')->will($this->returnValue('the view output'));
89
90 $mockArguments = $this->getMock('Tx_Extbase_Controller_Arguments', array('areValid'), array(), '', FALSE);
91 $mockArguments->expects($this->any())->method('areValid')->will($this->returnValue(TRUE));
92
93 $mockArgumentMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults', array(), array(), '', FALSE);
94 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
95
96 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction', 'initializeAction'), array(), '', FALSE);
97 $mockController->expects($this->once())->method('fooAction');
98 $mockController->_set('request', $mockRequest);
99 $mockController->_set('response', $mockResponse);
100 $mockController->_set('arguments', $mockArguments);
101 $mockController->_set('actionMethodName', 'fooAction');
102 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
103 $mockController->_set('view', $mockView);
104 $mockController->_call('callActionMethod');
105 }
106
107 /**
108 * @test
109 */
110 public function callActionMethodCallsTheErrorActionIfTheMappingResultsHaveErrors() {
111 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
112
113 $mockResponse = $this->getMock('Tx_Extbase_MVC_Response', array(), array(), '', FALSE);
114 $mockResponse->expects($this->once())->method('appendContent')->with('the returned string');
115
116 $mockArguments = $this->getMock('Tx_Extbase_Controller_Arguments', array('areValid'), array(), '', FALSE);
117 $mockArguments->expects($this->any())->method('areValid')->will($this->returnValue(TRUE));
118
119 $mockArgumentMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults', array(), array(), '', FALSE);
120 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
121
122 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction', 'initializeAction'), array(), '', FALSE);
123 $mockController->expects($this->once())->method('fooAction')->will($this->returnValue('the returned string'));
124 $mockController->_set('request', $mockRequest);
125 $mockController->_set('response', $mockResponse);
126 $mockController->_set('arguments', $mockArguments);
127 $mockController->_set('actionMethodName', 'fooAction');
128 $mockController->_set('errorMethodName', 'barAction');
129 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
130 $mockController->_call('callActionMethod');
131 }
132
133 /**
134 * @test
135 */
136 public function callActionMethodPassesDefaultValuesAsArguments() {
137 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
138
139 $mockResponse = $this->getMock('Tx_Extbase_MVC_Response', array(), array(), '', FALSE);
140
141 $mockArguments = $this->getMock('ArrayObject', array('areValid'), array(), '', FALSE);
142 $mockArguments->expects($this->any())->method('areValid')->will($this->returnValue(TRUE));
143 $optionalArgument = new Tx_Extbase_MVC_Controller_Argument('name1', 'Text');
144 $optionalArgument->setDefaultValue('Default value');
145 $mockArguments[] = $optionalArgument;
146
147 $mockArgumentMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults', array(), array(), '', FALSE);
148 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
149
150 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction', 'initializeAction'), array(), '', FALSE);
151 $mockController->expects($this->once())->method('fooAction')->with('Default value');
152 $mockController->_set('request', $mockRequest);
153 $mockController->_set('response', $mockResponse);
154 $mockController->_set('arguments', $mockArguments);
155 $mockController->_set('actionMethodName', 'fooAction');
156 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
157 $mockController->_call('callActionMethod');
158 }
159
160 /**
161 * @test
162 */
163 public function initializeViewPreparesTheViewSpecifiedInTheRequestObjectAndUsesTheEmptyViewIfNoneCouldBeFound() {
164 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
165 // $mockRequest->expects($this->at(0))->method('getControllerExtensionName')->will($this->returnValue('Foo'));
166 // $mockRequest->expects($this->at(1))->method('getControllerName')->will($this->returnValue('Test'));
167 // $mockRequest->expects($this->at(2))->method('getControllerActionName')->will($this->returnValue('list'));
168
169 $mockView = $this->getMock('Tx_Extbase_MVC_View_ViewInterface');
170 // $mockView->expects($this->exactly(1))->method('setRequest')->with($mockRequest);
171
172 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('dummy'), array(), '', FALSE);
173 $mockController->_set('request', $mockRequest);
174
175 $mockController->_call('initializeView');
176
177 $this->assertTrue(in_array('Tx_Extbase_MVC_View_ViewInterface', class_implements($mockController->_get('view'))));
178 }
179
180 /**
181 * @test
182 */
183 public function resolveViewObjectNameUsesViewObjectNamePatternToResolveViewObjectName() {
184 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
185 $mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('MyExtension'));
186 $mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('MyController'));
187 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('myAction'));
188
189 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('dummy'), array(), '', FALSE);
190 $mockController->_set('request', $mockRequest);
191 // The viewObjectNamePattern of the Action should also be tested (#3253)
192 // $mockController->_set('viewObjectNamePattern', 'Tx_@extension_View_@controller_@action');
193
194 eval('class Tx_MyExtension_View_MyController_MyAction {}');
195
196 $this->assertEquals('Tx_MyExtension_View_MyController_MyAction', $mockController->_call('resolveViewObjectName'));
197 }
198
199 /**
200 * @test
201 */
202 public function resolveViewObjectNameReturnsStandardViewObjectNameIfNoCustomViewCanBeFound() {
203 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
204
205 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('dummy'), array(), '', FALSE);
206 $mockController->_set('request', $mockRequest);
207 $mockController->_set('objectManager', $mockObjectManager);
208 $mockController->_set('standardViewObjectName', 'MyStandardViewObjectName');
209
210 eval('class MyStandardViewObjectName {}');
211
212 $this->assertEquals('MyStandardViewObjectName', $mockController->_call('resolveViewObjectName'));
213 }
214
215
216 /**
217 * @test
218 */
219 public function initializeActionMethodArgumentsRegistersArgumentsFoundInTheSignatureOfTheCurrentActionMethod() {
220 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
221
222 $mockArguments = $this->getMock('Tx_Extbase_MVC_Controller_Arguments', array(), array(), '', FALSE);
223 $mockArguments->expects($this->at(0))->method('addNewArgument')->with('stringArgument', 'string', TRUE);
224 $mockArguments->expects($this->at(1))->method('addNewArgument')->with('integerArgument', 'integer', TRUE);
225 $mockArguments->expects($this->at(2))->method('addNewArgument')->with('objectArgument', 'Tx_Foo_Bar', TRUE);
226
227 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction'), array(), '', FALSE);
228
229 $methodParameters = array(
230 'stringArgument' => array(
231 'position' => 0,
232 'byReference' => FALSE,
233 'array' => FALSE,
234 'optional' => FALSE,
235 'allowsNull' => FALSE,
236 'type' => 'string'
237 ),
238 'integerArgument' => array(
239 'position' => 1,
240 'byReference' => FALSE,
241 'array' => FALSE,
242 'optional' => FALSE,
243 'allowsNull' => FALSE,
244 'type' => 'integer'
245 ),
246 'objectArgument' => array(
247 'position' => 2,
248 'byReference' => FALSE,
249 'array' => FALSE,
250 'optional' => FALSE,
251 'allowsNull' => FALSE,
252 'type' => 'Tx_Foo_Bar'
253 )
254 );
255
256 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
257 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
258
259 $mockController->injectReflectionService($mockReflectionService);
260 $mockController->_set('request', $mockRequest);
261 $mockController->_set('arguments', $mockArguments);
262 $mockController->_set('actionMethodName', 'fooAction');
263 $mockController->_call('initializeActionMethodArguments');
264 }
265
266 /**
267 * @test
268 */
269 public function initializeActionMethodArgumentsRegistersOptionalArgumentsAsSuch() {
270 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
271
272 $mockArguments = $this->getMock('Tx_Extbase_MVC_Controller_Arguments', array(), array(), '', FALSE);
273 $mockArguments->expects($this->at(0))->method('addNewArgument')->with('arg1', 'Text', TRUE);
274 $mockArguments->expects($this->at(1))->method('addNewArgument')->with('arg2', 'array', FALSE, array(21));
275 $mockArguments->expects($this->at(2))->method('addNewArgument')->with('arg3', 'Text', FALSE, 42);
276
277 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction'), array(), '', FALSE);
278
279 $methodParameters = array(
280 'arg1' => array(
281 'position' => 0,
282 'byReference' => FALSE,
283 'array' => FALSE,
284 'optional' => FALSE,
285 'allowsNull' => FALSE
286 ),
287 'arg2' => array(
288 'position' => 1,
289 'byReference' => FALSE,
290 'array' => TRUE,
291 'optional' => TRUE,
292 'defaultValue' => array(21),
293 'allowsNull' => FALSE
294 ),
295 'arg3' => array(
296 'position' => 2,
297 'byReference' => FALSE,
298 'array' => FALSE,
299 'optional' => TRUE,
300 'defaultValue' => 42,
301 'allowsNull' => FALSE
302 )
303 );
304
305 $methodTagsValues = array(
306 'param' => array(
307 'string $arg1',
308 'array $arg2',
309 'string $arg3'
310 )
311 );
312
313 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
314 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
315
316 $mockController->injectReflectionService($mockReflectionService);
317 $mockController->_set('request', $mockRequest);
318 $mockController->_set('arguments', $mockArguments);
319 $mockController->_set('actionMethodName', 'fooAction');
320 $mockController->_call('initializeActionMethodArguments');
321 }
322
323 /**
324 * @test
325 */
326 public function initializeActionMethodValidatorsDetectsValidateAnnotationsAndRegistersNewValidatorsForEachArgument() {
327 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction'), array(), '', FALSE);
328
329 $chain1 = $this->getMock('Tx_Extbase_Validation_Validator_ChainValidator', array(), array(), '', FALSE);
330 $chain2 = $this->getMock('Tx_Extbase_Validation_Validator_ChainValidator', array(), array(), '', FALSE);
331
332 $validatorChains = array(
333 'arg1' => $chain1,
334 'arg2' => $chain2
335 );
336
337 $mockValidatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array(), array(), '', FALSE);
338 $mockValidatorResolver->expects($this->once())->method('buildMethodArgumentsValidatorChains')->with(get_class($mockController), 'fooAction')->will($this->returnValue($validatorChains));
339
340 $mockArgument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array(), '', FALSE);
341 $mockArgument->expects($this->at(0))->method('setValidator')->with($chain1);
342 $mockArgument->expects($this->at(1))->method('setValidator')->with($chain1);
343
344 $mockArguments = $this->getMock('Tx_Extbase_MVC_Controller_Arguments', array(), array(), '', FALSE);
345 $mockArguments->expects($this->at(0))->method('offsetExists')->with('arg1')->will($this->returnValue(TRUE));
346 $mockArguments->expects($this->at(1))->method('offsetGet')->with('arg1')->will($this->returnValue($mockArgument));
347 $mockArguments->expects($this->at(2))->method('offsetExists')->with('arg2')->will($this->returnValue(TRUE));
348 $mockArguments->expects($this->at(3))->method('offsetGet')->with('arg2')->will($this->returnValue($mockArgument));
349
350 $mockController->injectValidatorResolver($mockValidatorResolver);
351 $mockController->_set('actionMethodName', 'fooAction');
352 $mockController->_set('arguments', $mockArguments);
353 $mockController->_call('initializeActionMethodValidators');
354 }
355
356 }
357 ?>