27df15b92a70e1e6cc66de3760d925f926dcf556
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / MVC / Controller / AbstractController_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 class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 class Tx_Extbase_MVC_Controller_AbstractController_testcase extends Tx_Extbase_Base_testcase {
29
30 /**
31 * @test
32 */
33 public function initializeObjectSetsCurrentPackage() {
34 $extensionName = uniqid('Test');
35 $controller = $this->getMock('Tx_Extbase_MVC_Controller_AbstractController', array(), array(), 'Tx_' . $extensionName . '_Controller');
36 $this->assertSame($extensionName, $this->readAttribute($controller, 'extensionName'));
37 }
38
39 /**
40 * @test
41 * @expectedException Tx_Extbase_MVC_Exception_UnsupportedRequestType
42 */
43 public function processRequestWillThrowAnExceptionIfTheGivenRequestIsNotSupported() {
44 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
45 $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
46
47 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('mapRequestArgumentsToControllerArguments'), array(), '', FALSE);
48 $controller->_set('supportedRequestTypes', array('Tx_Something_Request'));
49 $controller->processRequest($mockRequest, $mockResponse);
50 }
51
52 /**
53 * @test
54 */
55 public function processRequestSetsTheDispatchedFlagOfTheRequest() {
56 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
57 $mockRequest->expects($this->once())->method('setDispatched')->with(TRUE);
58
59 $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
60
61 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('initializeArguments', 'initializeControllerArgumentsBaseValidators', 'mapRequestArgumentsToControllerArguments'), array());
62 $controller->processRequest($mockRequest, $mockResponse);
63 }
64
65 /**
66 * @test
67 * @expectedException Tx_Extbase_Exception_StopAction
68 */
69 public function forwardThrowsAStopActionException() {
70 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
71 $mockRequest->expects($this->once())->method('setDispatched')->with(FALSE);
72 $mockRequest->expects($this->once())->method('setControllerActionName')->with('foo');
73
74 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
75 $controller->_set('request', $mockRequest);
76 $controller->_call('forward', 'foo');
77 }
78
79 /**
80 * @test
81 * @expectedException Tx_Extbase_Exception_StopAction
82 */
83 public function forwardSetsControllerAndArgumentsAtTheRequestObjectIfTheyAreSpecified() {
84 $arguments = array('foo' => 'bar');
85
86 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
87 $mockRequest->expects($this->once())->method('setControllerActionName')->with('foo');
88 $mockRequest->expects($this->once())->method('setControllerName')->with('Bar');
89 $mockRequest->expects($this->once())->method('setControllerExtensionName')->with('Baz');
90 $mockRequest->expects($this->once())->method('setArguments')->with($arguments);
91
92 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
93 $controller->_set('request', $mockRequest);
94 $controller->_call('forward', 'foo', 'Bar', 'Baz', $arguments);
95 }
96
97 /**
98 * @test
99 */
100 public function redirectRedirectsToTheSpecifiedAction() {
101 $arguments = array('foo' => 'bar');
102
103 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
104 $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
105
106 $mockURIHelper = $this->getMock('Tx_Extbase_MVC_View_Helper_URIHelper');
107 $mockURIHelper->expects($this->once())->method('URIFor')->with(123, 'theActionName', $arguments, 'TheControllerName', 'TheExtensionName')->will($this->returnValue('the uri'));
108
109 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('redirectToURI'), array(), '', FALSE);
110 $controller->expects($this->once())->method('redirectToURI')->with('the uri');
111 $controller->_set('request', $mockRequest);
112 $controller->_set('response', $mockResponse);
113 $controller->_set('URIHelper', $mockURIHelper);
114 $controller->_call('redirect', 'theActionName', 'TheControllerName', 'TheExtensionName', $arguments, 123);
115 }
116
117 /**
118 * @test
119 * @expectedException Tx_Extbase_Exception_StopAction
120 */
121 public function throwStatusSetsTheSpecifiedStatusHeaderAndStopsTheCurrentAction() {
122 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
123 $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
124 $mockResponse->expects($this->once())->method('setStatus')->with(404, 'File Really Not Found');
125 $mockResponse->expects($this->once())->method('setContent')->with('<h1>All wrong!</h1><p>Sorry, the file does not exist.</p>');
126
127 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
128 $controller->_set('request', $mockRequest);
129 $controller->_set('response', $mockResponse);
130 $controller->_call('throwStatus', 404, 'File Really Not Found', '<h1>All wrong!</h1><p>Sorry, the file does not exist.</p>');
131 }
132
133 /**
134 * @test
135 */
136 public function initializeControllerArgumentsBaseValidatorsRegistersValidatorsDeclaredInTheArgumentModels() {
137 $mockValidators = array(
138 'foo' => $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface'),
139 );
140
141 $mockValidatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array(), array(), '', FALSE);
142 $mockValidatorResolver->expects($this->at(0))->method('getBaseValidatorChain')->with('FooType')->will($this->returnValue($mockValidators['foo']));
143 $mockValidatorResolver->expects($this->at(1))->method('getBaseValidatorChain')->with('BarType')->will($this->returnValue(NULL));
144
145 $mockArgumentFoo = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('foo'));
146 $mockArgumentFoo->expects($this->once())->method('getDataType')->will($this->returnValue('FooType'));
147 $mockArgumentFoo->expects($this->once())->method('setValidator')->with($mockValidators['foo']);
148
149 $mockArgumentBar = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('bar'));
150 $mockArgumentBar->expects($this->once())->method('getDataType')->will($this->returnValue('BarType'));
151 $mockArgumentBar->expects($this->never())->method('setValidator');
152
153 $mockArguments = new Tx_Extbase_MVC_Controller_Arguments();
154 $mockArguments->addArgument($mockArgumentFoo);
155 $mockArguments->addArgument($mockArgumentBar);
156
157 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
158 $controller->_set('arguments', $mockArguments);
159 $controller->injectValidatorResolver($mockValidatorResolver);
160 $controller->_call('initializeControllerArgumentsBaseValidators');
161 }
162
163 /**
164 * @test
165 */
166 public function mapRequestArgumentsToControllerArgumentsPreparesInformationAndValidatorsAndMapsAndValidates() {
167 $mockValidator = new Tx_Extbase_MVC_Controller_ArgumentsValidator(); // FIXME see original FLOW3 code
168
169 $mockArgumentFoo = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('foo'));
170 $mockArgumentFoo->expects($this->any())->method('getName')->will($this->returnValue('foo'));
171 $mockArgumentBar = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('bar'));
172 $mockArgumentBar->expects($this->any())->method('getName')->will($this->returnValue('bar'));
173
174 $mockArguments = new Tx_Extbase_MVC_Controller_Arguments();
175 $mockArguments->addArgument($mockArgumentFoo);
176 $mockArguments->addArgument($mockArgumentBar);
177
178 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
179 $mockRequest->expects($this->once())->method('getArguments')->will($this->returnValue(array('requestFoo', 'requestBar')));
180
181 $mockMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults');
182
183 $mockPropertyMapper = $this->getMock('Tx_Extbase_Property_Mapper', array(), array(), '', FALSE);
184 $mockPropertyMapper->expects($this->once())->method('mapAndValidate')
185 ->with(array('foo', 'bar'), array('requestFoo', 'requestBar'), $mockArguments, array(), $mockValidator)
186 ->will($this->returnValue(TRUE));
187 $mockPropertyMapper->expects($this->once())->method('getMappingResults')->will($this->returnValue($mockMappingResults));
188
189 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
190
191 $controller->_set('arguments', $mockArguments);
192 $controller->_set('request', $mockRequest);
193 $controller->_set('propertyMapper', $mockPropertyMapper);
194 $controller->_set('objectManager', $mockObjectManager);
195
196 $controller->_call('mapRequestArgumentsToControllerArguments');
197
198 $this->assertSame($mockMappingResults, $controller->_get('argumentsMappingResults'));
199 // $this->assertTrue(in_array('Tx_Extbase_Validation_Validator_ObjectValidatorInterface', class_implements($controller->_get('argumentsMappingResults'))));
200 }
201
202 }
203 ?>