1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 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: 29: 30: 31:
32: class ControllerTestDispatcher extends Dispatcher {
33:
34: 35: 36: 37: 38:
39: public $testController = null;
40:
41: 42: 43: 44: 45:
46: public $loadRoutes = true;
47:
48: 49: 50: 51: 52:
53: protected function _getController($request, $response) {
54: if ($this->testController === null) {
55: $this->testController = parent::_getController($request, $response);
56: }
57: $this->testController->helpers = array_merge(array('InterceptContent'), $this->testController->helpers);
58: $this->testController->setRequest($request);
59: $this->testController->response = $this->response;
60: foreach ($this->testController->Components->loaded() as $component) {
61: $object = $this->testController->Components->{$component};
62: if (isset($object->response)) {
63: $object->response = $response;
64: }
65: if (isset($object->request)) {
66: $object->request = $request;
67: }
68: }
69: return $this->testController;
70: }
71:
72: 73: 74: 75: 76:
77: protected function _loadRoutes() {
78: parent::_loadRoutes();
79: if (!$this->loadRoutes) {
80: Router::reload();
81: }
82: }
83:
84: }
85:
86: 87: 88: 89: 90:
91: class InterceptContentHelper extends Helper {
92:
93: 94: 95: 96: 97: 98:
99: public function afterRender($viewFile) {
100: $this->_View->assign('__view_no_layout__', $this->_View->fetch('content'));
101: $this->_View->Helpers->unload('InterceptContent');
102: }
103:
104: }
105:
106: 107: 108: 109: 110:
111: abstract class ControllerTestCase extends CakeTestCase {
112:
113: 114: 115: 116: 117:
118: public $controller = null;
119:
120: 121: 122: 123: 124:
125: public $autoMock = true;
126:
127: 128: 129: 130: 131:
132: public $loadRoutes = true;
133:
134: 135: 136: 137: 138:
139: public $vars = null;
140:
141: 142: 143: 144: 145:
146: public $view = null;
147:
148: 149: 150: 151: 152:
153: public $contents = null;
154:
155: 156: 157: 158: 159:
160: public $result = null;
161:
162: 163: 164: 165: 166:
167: public $headers = null;
168:
169: 170: 171: 172: 173: 174: 175:
176: protected $_dirtyController = false;
177:
178: 179: 180: 181: 182: 183: 184: 185: 186:
187: public function __call($name, $arguments) {
188: if ($name === 'testAction') {
189: return call_user_func_array(array($this, '_testAction'), $arguments);
190: }
191: throw new BadMethodCallException("Method '{$name}' does not exist.");
192: }
193:
194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214:
215: protected function _testAction($url = '', $options = array()) {
216: $this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
217:
218: $options = array_merge(array(
219: 'data' => array(),
220: 'method' => 'POST',
221: 'return' => 'result'
222: ), $options);
223:
224: $restore = array('get' => $_GET, 'post' => $_POST);
225:
226: $_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
227: if (is_array($options['data'])) {
228: if (strtoupper($options['method']) === 'GET') {
229: $_GET = $options['data'];
230: $_POST = array();
231: } else {
232: $_POST = $options['data'];
233: $_GET = array();
234: }
235: }
236: $request = $this->getMock('CakeRequest', array('_readInput'), array($url));
237:
238: if (is_string($options['data'])) {
239: $request->expects($this->any())
240: ->method('_readInput')
241: ->will($this->returnValue($options['data']));
242: }
243:
244: $Dispatch = new ControllerTestDispatcher();
245: foreach (Router::$routes as $route) {
246: if ($route instanceof RedirectRoute) {
247: $route->response = $this->getMock('CakeResponse', array('send'));
248: }
249: }
250: $Dispatch->loadRoutes = $this->loadRoutes;
251: $Dispatch->parseParams(new CakeEvent('ControllerTestCase', $Dispatch, array('request' => $request)));
252: if (!isset($request->params['controller']) && Router::currentRoute()) {
253: $this->headers = Router::currentRoute()->response->header();
254: return;
255: }
256: if ($this->_dirtyController) {
257: $this->controller = null;
258: }
259:
260: $plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
261: if ($this->controller === null && $this->autoMock) {
262: $this->generate($plugin . Inflector::camelize($request->params['controller']));
263: }
264: $params = array();
265: if ($options['return'] === 'result') {
266: $params['return'] = 1;
267: $params['bare'] = 1;
268: $params['requested'] = 1;
269: }
270: $Dispatch->testController = $this->controller;
271: $Dispatch->response = $this->getMock('CakeResponse', array('send'));
272: $this->result = $Dispatch->dispatch($request, $Dispatch->response, $params);
273: $this->controller = $Dispatch->testController;
274: $this->vars = $this->controller->viewVars;
275: $this->contents = $this->controller->response->body();
276: if (isset($this->controller->View)) {
277: $this->view = $this->controller->View->fetch('__view_no_layout__');
278: }
279: $this->_dirtyController = true;
280: $this->headers = $Dispatch->response->header();
281:
282: $_GET = $restore['get'];
283: $_POST = $restore['post'];
284:
285: return $this->{$options['return']};
286: }
287:
288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308:
309: public function generate($controller, $mocks = array()) {
310: list($plugin, $controller) = pluginSplit($controller);
311: if ($plugin) {
312: App::uses($plugin . 'AppController', $plugin . '.Controller');
313: $plugin .= '.';
314: }
315: App::uses($controller . 'Controller', $plugin . 'Controller');
316: if (!class_exists($controller . 'Controller')) {
317: throw new MissingControllerException(array(
318: 'class' => $controller . 'Controller',
319: 'plugin' => substr($plugin, 0, -1)
320: ));
321: }
322: ClassRegistry::flush();
323:
324: $mocks = array_merge_recursive(array(
325: 'methods' => array('_stop'),
326: 'models' => array(),
327: 'components' => array()
328: ), (array)$mocks);
329:
330: list($plugin, $name) = pluginSplit($controller);
331: $controllerObj = $this->getMock($name . 'Controller', $mocks['methods'], array(), '', false);
332: $controllerObj->name = $name;
333: $request = $this->getMock('CakeRequest');
334: $response = $this->getMock('CakeResponse', array('_sendHeader'));
335: $controllerObj->__construct($request, $response);
336: $controllerObj->Components->setController($controllerObj);
337:
338: $config = ClassRegistry::config('Model');
339: foreach ($mocks['models'] as $model => $methods) {
340: if (is_string($methods)) {
341: $model = $methods;
342: $methods = true;
343: }
344: if ($methods === true) {
345: $methods = array();
346: }
347: $this->getMockForModel($model, $methods, $config);
348: }
349:
350: foreach ($mocks['components'] as $component => $methods) {
351: if (is_string($methods)) {
352: $component = $methods;
353: $methods = true;
354: }
355: if ($methods === true) {
356: $methods = array();
357: }
358: list($plugin, $name) = pluginSplit($component, true);
359: $componentClass = $name . 'Component';
360: App::uses($componentClass, $plugin . 'Controller/Component');
361: if (!class_exists($componentClass)) {
362: throw new MissingComponentException(array(
363: 'class' => $componentClass
364: ));
365: }
366: $config = isset($controllerObj->components[$component]) ? $controllerObj->components[$component] : array();
367: $componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components, $config));
368: $controllerObj->Components->set($name, $componentObj);
369: $controllerObj->Components->enable($name);
370: }
371:
372: $controllerObj->constructClasses();
373: $this->_dirtyController = false;
374:
375: $this->controller = $controllerObj;
376: return $this->controller;
377: }
378:
379: }
380: