Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * ControllerTestCase file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.TestSuite
15
 * @since         CakePHP(tm) v 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Dispatcher', 'Routing');
20
App::uses('CakeTestCase', 'TestSuite');
21
App::uses('Router', 'Routing');
22
App::uses('CakeRequest', 'Network');
23
App::uses('CakeResponse', 'Network');
24
App::uses('Helper', 'View');
25
App::uses('CakeEvent', 'Event');
26
 
27
/**
28
 * ControllerTestDispatcher class
29
 *
30
 * @package       Cake.TestSuite
31
 */
32
class ControllerTestDispatcher extends Dispatcher {
33
 
34
/**
35
 * The controller to use in the dispatch process
36
 *
37
 * @var Controller
38
 */
39
	public $testController = null;
40
 
41
/**
42
 * Use custom routes during tests
43
 *
44
 * @var bool
45
 */
46
	public $loadRoutes = true;
47
 
48
/**
49
 * Returns the test controller
50
 *
51
 * @param CakeRequest $request The request instance.
52
 * @param CakeResponse $response The response instance.
53
 * @return Controller
54
 */
55
	protected function _getController($request, $response) {
56
		if ($this->testController === null) {
57
			$this->testController = parent::_getController($request, $response);
58
		}
59
		$this->testController->helpers = array_merge(array('InterceptContent'), $this->testController->helpers);
60
		$this->testController->setRequest($request);
61
		$this->testController->response = $this->response;
62
		foreach ($this->testController->Components->loaded() as $component) {
63
			$object = $this->testController->Components->{$component};
64
			if (isset($object->response)) {
65
				$object->response = $response;
66
			}
67
			if (isset($object->request)) {
68
				$object->request = $request;
69
			}
70
		}
71
		return $this->testController;
72
	}
73
 
74
/**
75
 * Loads routes and resets if the test case dictates it should
76
 *
77
 * @return void
78
 */
79
	protected function _loadRoutes() {
80
		parent::_loadRoutes();
81
		if (!$this->loadRoutes) {
82
			Router::reload();
83
		}
84
	}
85
 
86
}
87
 
88
/**
89
 * InterceptContentHelper class
90
 *
91
 * @package       Cake.TestSuite
92
 */
93
class InterceptContentHelper extends Helper {
94
 
95
/**
96
 * Intercepts and stores the contents of the view before the layout is rendered
97
 *
98
 * @param string $viewFile The view file
99
 * @return void
100
 */
101
	public function afterRender($viewFile) {
102
		$this->_View->assign('__view_no_layout__', $this->_View->fetch('content'));
103
		$this->_View->Helpers->unload('InterceptContent');
104
	}
105
 
106
}
107
 
108
/**
109
 * ControllerTestCase class
110
 *
111
 * @package       Cake.TestSuite
112
 */
113
abstract class ControllerTestCase extends CakeTestCase {
114
 
115
/**
116
 * The controller to test in testAction
117
 *
118
 * @var Controller
119
 */
120
	public $controller = null;
121
 
122
/**
123
 * Automatically mock controllers that aren't mocked
124
 *
125
 * @var bool
126
 */
127
	public $autoMock = true;
128
 
129
/**
130
 * Use custom routes during tests
131
 *
132
 * @var bool
133
 */
134
	public $loadRoutes = true;
135
 
136
/**
137
 * The resulting view vars of the last testAction call
138
 *
139
 * @var array
140
 */
141
	public $vars = null;
142
 
143
/**
144
 * The resulting rendered view of the last testAction call
145
 *
146
 * @var string
147
 */
148
	public $view = null;
149
 
150
/**
151
 * The resulting rendered layout+view of the last testAction call
152
 *
153
 * @var string
154
 */
155
	public $contents = null;
156
 
157
/**
158
 * The returned result of the dispatch (requestAction), if any
159
 *
160
 * @var string
161
 */
162
	public $result = null;
163
 
164
/**
165
 * The headers that would have been sent by the action
166
 *
167
 * @var string
168
 */
169
	public $headers = null;
170
 
171
/**
172
 * Flag for checking if the controller instance is dirty.
173
 * Once a test has been run on a controller it should be rebuilt
174
 * to clean up properties.
175
 *
176
 * @var bool
177
 */
178
	protected $_dirtyController = false;
179
 
180
/**
181
 * Used to enable calling ControllerTestCase::testAction() without the testing
182
 * framework thinking that it's a test case
183
 *
184
 * @param string $name The name of the function
185
 * @param array $arguments Array of arguments
186
 * @return the return of _testAction
187
 * @throws BadMethodCallException when you call methods that don't exist.
188
 */
189
	public function __call($name, $arguments) {
190
		if ($name === 'testAction') {
191
			return call_user_func_array(array($this, '_testAction'), $arguments);
192
		}
193
		throw new BadMethodCallException("Method '{$name}' does not exist.");
194
	}
195
 
196
/**
197
 * Lets you do functional tests of a controller action.
198
 *
199
 * ### Options:
200
 *
201
 * - `data` Will be used as the request data. If the `method` is GET,
202
 *   data will be used a GET params. If the `method` is POST, it will be used
203
 *   as POST data. By setting `$options['data']` to a string, you can simulate XML or JSON
204
 *   payloads to your controllers allowing you to test REST webservices.
205
 * - `method` POST or GET. Defaults to POST.
206
 * - `return` Specify the return type you want. Choose from:
207
 *     - `vars` Get the set view variables.
208
 *     - `view` Get the rendered view, without a layout.
209
 *     - `contents` Get the rendered view including the layout.
210
 *     - `result` Get the return value of the controller action. Useful
211
 *       for testing requestAction methods.
212
 *
213
 * @param string $url The url to test
214
 * @param array $options See options
215
 * @return mixed
216
 */
217
	protected function _testAction($url = '', $options = array()) {
218
		$this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
219
 
220
		$options += array(
221
			'data' => array(),
222
			'method' => 'POST',
223
			'return' => 'result'
224
		);
225
 
226
		$restore = array('get' => $_GET, 'post' => $_POST);
227
 
228
		$_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
229
		if (is_array($options['data'])) {
230
			if (strtoupper($options['method']) === 'GET') {
231
				$_GET = $options['data'];
232
				$_POST = array();
233
			} else {
234
				$_POST = $options['data'];
235
				$_GET = array();
236
			}
237
		}
238
		$request = $this->getMock('CakeRequest', array('_readInput'), array($url));
239
 
240
		if (is_string($options['data'])) {
241
			$request->expects($this->any())
242
				->method('_readInput')
243
				->will($this->returnValue($options['data']));
244
		}
245
 
246
		$Dispatch = new ControllerTestDispatcher();
247
		foreach (Router::$routes as $route) {
248
			if ($route instanceof RedirectRoute) {
249
				$route->response = $this->getMock('CakeResponse', array('send'));
250
			}
251
		}
252
		$Dispatch->loadRoutes = $this->loadRoutes;
253
		$Dispatch->parseParams(new CakeEvent('ControllerTestCase', $Dispatch, array('request' => $request)));
254
		if (!isset($request->params['controller']) && Router::currentRoute()) {
255
			$this->headers = Router::currentRoute()->response->header();
256
			return;
257
		}
258
		if ($this->_dirtyController) {
259
			$this->controller = null;
260
		}
261
 
262
		$plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
263
		if ($this->controller === null && $this->autoMock) {
264
			$this->generate($plugin . Inflector::camelize($request->params['controller']));
265
		}
266
		$params = array();
267
		if ($options['return'] === 'result') {
268
			$params['return'] = 1;
269
			$params['bare'] = 1;
270
			$params['requested'] = 1;
271
		}
272
		$Dispatch->testController = $this->controller;
273
		$Dispatch->response = $this->getMock('CakeResponse', array('send'));
274
		$this->result = $Dispatch->dispatch($request, $Dispatch->response, $params);
275
		$this->controller = $Dispatch->testController;
276
		$this->vars = $this->controller->viewVars;
277
		$this->contents = $this->controller->response->body();
278
		if (isset($this->controller->View)) {
279
			$this->view = $this->controller->View->fetch('__view_no_layout__');
280
		}
281
		$this->_dirtyController = true;
282
		$this->headers = $Dispatch->response->header();
283
 
284
		$_GET = $restore['get'];
285
		$_POST = $restore['post'];
286
 
287
		return $this->{$options['return']};
288
	}
289
 
290
/**
291
 * Generates a mocked controller and mocks any classes passed to `$mocks`. By
292
 * default, `_stop()` is stubbed as is sending the response headers, so to not
293
 * interfere with testing.
294
 *
295
 * ### Mocks:
296
 *
297
 * - `methods` Methods to mock on the controller. `_stop()` is mocked by default
298
 * - `models` Models to mock. Models are added to the ClassRegistry so any
299
 *   time they are instantiated the mock will be created. Pass as key value pairs
300
 *   with the value being specific methods on the model to mock. If `true` or
301
 *   no value is passed, the entire model will be mocked.
302
 * - `components` Components to mock. Components are only mocked on this controller
303
 *   and not within each other (i.e., components on components)
304
 *
305
 * @param string $controller Controller name
306
 * @param array $mocks List of classes and methods to mock
307
 * @return Controller Mocked controller
308
 * @throws MissingControllerException When controllers could not be created.
309
 * @throws MissingComponentException When components could not be created.
310
 */
311
	public function generate($controller, $mocks = array()) {
312
		list($plugin, $controller) = pluginSplit($controller);
313
		if ($plugin) {
314
			App::uses($plugin . 'AppController', $plugin . '.Controller');
315
			$plugin .= '.';
316
		}
317
		App::uses($controller . 'Controller', $plugin . 'Controller');
318
		if (!class_exists($controller . 'Controller')) {
319
			throw new MissingControllerException(array(
320
				'class' => $controller . 'Controller',
321
				'plugin' => substr($plugin, 0, -1)
322
			));
323
		}
324
		ClassRegistry::flush();
325
 
326
		$mocks = array_merge_recursive(array(
327
			'methods' => array('_stop'),
328
			'models' => array(),
329
			'components' => array()
330
		), (array)$mocks);
331
 
332
		list($plugin, $name) = pluginSplit($controller);
333
		$controllerObj = $this->getMock($name . 'Controller', $mocks['methods'], array(), '', false);
334
		$controllerObj->name = $name;
335
		$request = $this->getMock('CakeRequest');
336
		$response = $this->getMock('CakeResponse', array('_sendHeader'));
337
		$controllerObj->__construct($request, $response);
338
		$controllerObj->Components->setController($controllerObj);
339
 
340
		$config = ClassRegistry::config('Model');
341
		foreach ($mocks['models'] as $model => $methods) {
342
			if (is_string($methods)) {
343
				$model = $methods;
344
				$methods = true;
345
			}
346
			if ($methods === true) {
347
				$methods = array();
348
			}
349
			$this->getMockForModel($model, $methods, $config);
350
		}
351
 
352
		foreach ($mocks['components'] as $component => $methods) {
353
			if (is_string($methods)) {
354
				$component = $methods;
355
				$methods = true;
356
			}
357
			if ($methods === true) {
358
				$methods = array();
359
			}
360
			list($plugin, $name) = pluginSplit($component, true);
361
			$componentClass = $name . 'Component';
362
			App::uses($componentClass, $plugin . 'Controller/Component');
363
			if (!class_exists($componentClass)) {
364
				throw new MissingComponentException(array(
365
					'class' => $componentClass
366
				));
367
			}
368
			$config = isset($controllerObj->components[$component]) ? $controllerObj->components[$component] : array();
369
			$componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components, $config));
370
			$controllerObj->Components->set($name, $componentObj);
371
			$controllerObj->Components->enable($name);
372
		}
373
 
374
		$controllerObj->constructClasses();
375
		$this->_dirtyController = false;
376
 
377
		$this->controller = $controllerObj;
378
		return $this->controller;
379
	}
380
 
381
}