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: 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: 76: 77: 78:
79: protected function _loadRoutes() {
80: parent::_loadRoutes();
81: if (!$this->loadRoutes) {
82: Router::reload();
83: }
84: }
85:
86: }
87:
88: 89: 90: 91: 92:
93: class InterceptContentHelper extends Helper {
94:
95: 96: 97: 98: 99: 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: 110: 111: 112:
113: abstract class ControllerTestCase extends CakeTestCase {
114:
115: 116: 117: 118: 119:
120: public $controller = null;
121:
122: 123: 124: 125: 126:
127: public $autoMock = true;
128:
129: 130: 131: 132: 133:
134: public $loadRoutes = true;
135:
136: 137: 138: 139: 140:
141: public $vars = null;
142:
143: 144: 145: 146: 147:
148: public $view = null;
149:
150: 151: 152: 153: 154:
155: public $contents = null;
156:
157: 158: 159: 160: 161:
162: public $result = null;
163:
164: 165: 166: 167: 168:
169: public $headers = null;
170:
171: 172: 173: 174: 175: 176: 177:
178: protected $_dirtyController = false;
179:
180: 181: 182: 183: 184: 185: 186: 187: 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: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217:
218: protected function _testAction($url, $options = array()) {
219: $this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
220:
221: $options += array(
222: 'data' => array(),
223: 'method' => 'POST',
224: 'return' => 'result'
225: );
226:
227: $restore = array('get' => $_GET, 'post' => $_POST);
228:
229: $_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
230: if (is_array($options['data'])) {
231: if (strtoupper($options['method']) === 'GET') {
232: $_GET = $options['data'];
233: $_POST = array();
234: } else {
235: $_POST = $options['data'];
236: $_GET = array();
237: }
238: }
239: $request = $this->getMock('CakeRequest', array('_readInput'), array($url));
240:
241: if (is_string($options['data'])) {
242: $request->expects($this->any())
243: ->method('_readInput')
244: ->will($this->returnValue($options['data']));
245: }
246:
247: $Dispatch = new ControllerTestDispatcher();
248: foreach (Router::$routes as $route) {
249: if ($route instanceof RedirectRoute) {
250: $route->response = $this->getMock('CakeResponse', array('send'));
251: }
252: }
253: $Dispatch->loadRoutes = $this->loadRoutes;
254: $Dispatch->parseParams(new CakeEvent('ControllerTestCase', $Dispatch, array('request' => $request)));
255: if (!isset($request->params['controller']) && Router::currentRoute()) {
256: $this->headers = Router::currentRoute()->response->header();
257: return;
258: }
259: if ($this->_dirtyController) {
260: $this->controller = null;
261: }
262:
263: $plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
264: if ($this->controller === null && $this->autoMock) {
265: $this->generate($plugin . Inflector::camelize($request->params['controller']));
266: }
267: $params = array();
268: if ($options['return'] === 'result') {
269: $params['return'] = 1;
270: $params['bare'] = 1;
271: $params['requested'] = 1;
272: }
273: $Dispatch->testController = $this->controller;
274: $Dispatch->response = $this->getMock('CakeResponse', array('send'));
275: $this->result = $Dispatch->dispatch($request, $Dispatch->response, $params);
276: $this->controller = $Dispatch->testController;
277: $this->vars = $this->controller->viewVars;
278: $this->contents = $this->controller->response->body();
279: if (isset($this->controller->View)) {
280: $this->view = $this->controller->View->fetch('__view_no_layout__');
281: }
282: $this->_dirtyController = true;
283: $this->headers = $Dispatch->response->header();
284:
285: $_GET = $restore['get'];
286: $_POST = $restore['post'];
287:
288: return $this->{$options['return']};
289: }
290:
291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311:
312: public function generate($controller, $mocks = array()) {
313: list($plugin, $controller) = pluginSplit($controller);
314: if ($plugin) {
315: App::uses($plugin . 'AppController', $plugin . '.Controller');
316: $plugin .= '.';
317: }
318: App::uses($controller . 'Controller', $plugin . 'Controller');
319: if (!class_exists($controller . 'Controller')) {
320: throw new MissingControllerException(array(
321: 'class' => $controller . 'Controller',
322: 'plugin' => substr($plugin, 0, -1)
323: ));
324: }
325: ClassRegistry::flush();
326:
327: $mocks = array_merge_recursive(array(
328: 'methods' => array('_stop'),
329: 'models' => array(),
330: 'components' => array()
331: ), (array)$mocks);
332:
333: list($plugin, $name) = pluginSplit($controller);
334: $controllerObj = $this->getMock($name . 'Controller', $mocks['methods'], array(), '', false);
335: $controllerObj->name = $name;
336: $request = $this->getMock('CakeRequest');
337: $response = $this->getMock('CakeResponse', array('_sendHeader'));
338: $controllerObj->__construct($request, $response);
339: $controllerObj->Components->setController($controllerObj);
340:
341: $config = ClassRegistry::config('Model');
342: foreach ($mocks['models'] as $model => $methods) {
343: if (is_string($methods)) {
344: $model = $methods;
345: $methods = true;
346: }
347: if ($methods === true) {
348: $methods = array();
349: }
350: $this->getMockForModel($model, $methods, $config);
351: }
352:
353: foreach ($mocks['components'] as $component => $methods) {
354: if (is_string($methods)) {
355: $component = $methods;
356: $methods = true;
357: }
358: if ($methods === true) {
359: $methods = array();
360: }
361: list($plugin, $name) = pluginSplit($component, true);
362: $componentClass = $name . 'Component';
363: App::uses($componentClass, $plugin . 'Controller/Component');
364: if (!class_exists($componentClass)) {
365: throw new MissingComponentException(array(
366: 'class' => $componentClass
367: ));
368: }
369: $config = isset($controllerObj->components[$component]) ? $controllerObj->components[$component] : array();
370: $componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components, $config));
371: $controllerObj->Components->set($name, $componentObj);
372: $controllerObj->Components->enable($name);
373: }
374:
375: $controllerObj->constructClasses();
376: $this->_dirtyController = false;
377:
378: $this->controller = $controllerObj;
379: return $this->controller;
380: }
381:
382: }
383: