1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('Dispatcher', 'Routing');
21: App::uses('CakeTestCase', 'TestSuite');
22: App::uses('Router', 'Routing');
23: App::uses('CakeRequest', 'Network');
24: App::uses('CakeResponse', 'Network');
25: App::uses('Helper', 'View');
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->attached() 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: public function afterRender($viewFile) {
99: $this->_View->assign('__view_no_layout__', $this->_View->fetch('content'));
100: $this->_View->Helpers->unload('InterceptContent');
101: }
102:
103: }
104:
105: 106: 107: 108: 109:
110: abstract class ControllerTestCase extends CakeTestCase {
111:
112: 113: 114: 115: 116:
117: public $controller = null;
118:
119: 120: 121: 122: 123:
124: public $autoMock = true;
125:
126: 127: 128: 129: 130:
131: public $loadRoutes = true;
132:
133: 134: 135: 136: 137:
138: public $vars = null;
139:
140: 141: 142: 143: 144:
145: public $view = null;
146:
147: 148: 149: 150: 151:
152: public $contents = null;
153:
154: 155: 156: 157: 158:
159: public $result = null;
160:
161: 162: 163: 164: 165:
166: public $headers = null;
167:
168: 169: 170: 171: 172: 173: 174:
175: private $__dirtyController = false;
176:
177: 178: 179: 180: 181: 182: 183: 184:
185: public function __call($name, $arguments) {
186: if ($name == 'testAction') {
187: return call_user_func_array(array($this, '_testAction'), $arguments);
188: }
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210:
211: protected function _testAction($url = '', $options = array()) {
212: $this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
213:
214: $options = array_merge(array(
215: 'data' => array(),
216: 'method' => 'POST',
217: 'return' => 'result'
218: ), $options);
219:
220: $restore = array('get' => $_GET, 'post' => $_POST);
221:
222: $_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
223: if (is_array($options['data'])) {
224: if (strtoupper($options['method']) == 'GET') {
225: $_GET = $options['data'];
226: $_POST = array();
227: } else {
228: $_POST = $options['data'];
229: $_GET = array();
230: }
231: }
232: $request = $this->getMock('CakeRequest', array('_readInput'), array($url));
233:
234: if (is_string($options['data'])) {
235: $request->expects($this->any())
236: ->method('_readInput')
237: ->will($this->returnValue($options['data']));
238: }
239:
240: $Dispatch = new ControllerTestDispatcher();
241: foreach (Router::$routes as $route) {
242: if ($route instanceof RedirectRoute) {
243: $route->response = $this->getMock('CakeResponse', array('send'));
244: }
245: }
246: $Dispatch->loadRoutes = $this->loadRoutes;
247: $request = $Dispatch->parseParams($request);
248: if (!isset($request->params['controller'])) {
249: $this->headers = Router::currentRoute()->response->header();
250: return;
251: }
252: if ($this->__dirtyController) {
253: $this->controller = null;
254: }
255:
256: $plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
257: if ($this->controller === null && $this->autoMock) {
258: $this->generate($plugin . Inflector::camelize($request->params['controller']));
259: }
260: $params = array();
261: if ($options['return'] == 'result') {
262: $params['return'] = 1;
263: $params['bare'] = 1;
264: $params['requested'] = 1;
265: }
266: $Dispatch->testController = $this->controller;
267: $Dispatch->response = $this->getMock('CakeResponse', array('send'));
268: $this->result = $Dispatch->dispatch($request, $Dispatch->response, $params);
269: $this->controller = $Dispatch->testController;
270: $this->vars = $this->controller->viewVars;
271: $this->contents = $this->controller->response->body();
272: if (isset($this->controller->View)) {
273: $this->view = $this->controller->View->fetch('__view_no_layout__');
274: }
275: $this->__dirtyController = true;
276: $this->headers = $Dispatch->response->header();
277:
278: $_GET = $restore['get'];
279: $_POST = $restore['post'];
280:
281: return $this->{$options['return']};
282: }
283:
284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304:
305: public function generate($controller, $mocks = array()) {
306: list($plugin, $controller) = pluginSplit($controller);
307: if ($plugin) {
308: App::uses($plugin . 'AppController', $plugin . '.Controller');
309: $plugin .= '.';
310: }
311: App::uses($controller . 'Controller', $plugin . 'Controller');
312: if (!class_exists($controller . 'Controller')) {
313: throw new MissingControllerException(array(
314: 'class' => $controller . 'Controller',
315: 'plugin' => substr($plugin, 0, -1)
316: ));
317: }
318: ClassRegistry::flush();
319:
320: $mocks = array_merge_recursive(array(
321: 'methods' => array('_stop'),
322: 'models' => array(),
323: 'components' => array()
324: ), (array)$mocks);
325:
326: list($plugin, $name) = pluginSplit($controller);
327: $_controller = $this->getMock($name . 'Controller', $mocks['methods'], array(), '', false);
328: $_controller->name = $name;
329: $request = $this->getMock('CakeRequest');
330: $response = $this->getMock('CakeResponse', array('_sendHeader'));
331: $_controller->__construct($request, $response);
332:
333: $config = ClassRegistry::config('Model');
334: foreach ($mocks['models'] as $model => $methods) {
335: if (is_string($methods)) {
336: $model = $methods;
337: $methods = true;
338: }
339: if ($methods === true) {
340: $methods = array();
341: }
342: ClassRegistry::init($model);
343: list($plugin, $name) = pluginSplit($model);
344: $config = array_merge((array)$config, array('name' => $model));
345: $_model = $this->getMock($name, $methods, array($config));
346: ClassRegistry::removeObject($name);
347: ClassRegistry::addObject($name, $_model);
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: $_component = $this->getMock($componentClass, $methods, array(), '', false);
367: $_controller->Components->set($name, $_component);
368: }
369:
370: $_controller->constructClasses();
371: $this->__dirtyController = false;
372:
373: $this->controller = $_controller;
374: return $this->controller;
375: }
376:
377: }
378: