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: if (is_array($url)) {
228: $url = Router::url($url);
229: }
230:
231: $restore = array('get' => $_GET, 'post' => $_POST);
232:
233: $_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
234: if (is_array($options['data'])) {
235: if (strtoupper($options['method']) === 'GET') {
236: $_GET = $options['data'];
237: $_POST = array();
238: } else {
239: $_POST = $options['data'];
240: $_GET = array();
241: }
242: }
243: $request = $this->getMock('CakeRequest', array('_readInput'), array($url));
244:
245: if (is_string($options['data'])) {
246: $request->expects($this->any())
247: ->method('_readInput')
248: ->will($this->returnValue($options['data']));
249: }
250:
251: $Dispatch = new ControllerTestDispatcher();
252: foreach (Router::$routes as $route) {
253: if ($route instanceof RedirectRoute) {
254: $route->response = $this->getMock('CakeResponse', array('send'));
255: }
256: }
257: $Dispatch->loadRoutes = $this->loadRoutes;
258: $Dispatch->parseParams(new CakeEvent('ControllerTestCase', $Dispatch, array('request' => $request)));
259: if (!isset($request->params['controller']) && Router::currentRoute()) {
260: $this->headers = Router::currentRoute()->response->header();
261: return null;
262: }
263: if ($this->_dirtyController) {
264: $this->controller = null;
265: }
266:
267: $plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
268: if ($this->controller === null && $this->autoMock) {
269: $this->generate($plugin . Inflector::camelize($request->params['controller']));
270: }
271: $params = array();
272: if ($options['return'] === 'result') {
273: $params['return'] = 1;
274: $params['bare'] = 1;
275: $params['requested'] = 1;
276: }
277: $Dispatch->testController = $this->controller;
278: $Dispatch->response = $this->getMock('CakeResponse', array('send', '_clearBuffer'));
279: $this->result = $Dispatch->dispatch($request, $Dispatch->response, $params);
280: $this->controller = $Dispatch->testController;
281: $this->vars = $this->controller->viewVars;
282: $this->contents = $this->controller->response->body();
283: if (isset($this->controller->View)) {
284: $this->view = $this->controller->View->fetch('__view_no_layout__');
285: }
286: $this->_dirtyController = true;
287: $this->headers = $Dispatch->response->header();
288:
289: $_GET = $restore['get'];
290: $_POST = $restore['post'];
291:
292: return $this->{$options['return']};
293: }
294:
295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315:
316: public function generate($controller, $mocks = array()) {
317: list($plugin, $controller) = pluginSplit($controller);
318: if ($plugin) {
319: App::uses($plugin . 'AppController', $plugin . '.Controller');
320: $plugin .= '.';
321: }
322: App::uses($controller . 'Controller', $plugin . 'Controller');
323: if (!class_exists($controller . 'Controller')) {
324: throw new MissingControllerException(array(
325: 'class' => $controller . 'Controller',
326: 'plugin' => substr($plugin, 0, -1)
327: ));
328: }
329: ClassRegistry::flush();
330:
331: $mocks = array_merge_recursive(array(
332: 'methods' => array('_stop'),
333: 'models' => array(),
334: 'components' => array()
335: ), (array)$mocks);
336:
337: list($plugin, $name) = pluginSplit($controller);
338: $controllerObj = $this->getMock($name . 'Controller', $mocks['methods'], array(), '', false);
339: $controllerObj->name = $name;
340: $request = $this->getMock('CakeRequest');
341: $response = $this->getMock('CakeResponse', array('_sendHeader'));
342: $controllerObj->__construct($request, $response);
343: $controllerObj->Components->setController($controllerObj);
344:
345: $config = ClassRegistry::config('Model');
346: foreach ($mocks['models'] as $model => $methods) {
347: if (is_string($methods)) {
348: $model = $methods;
349: $methods = true;
350: }
351: if ($methods === true) {
352: $methods = array();
353: }
354: $this->getMockForModel($model, $methods, $config);
355: }
356:
357: foreach ($mocks['components'] as $component => $methods) {
358: if (is_string($methods)) {
359: $component = $methods;
360: $methods = true;
361: }
362: if ($methods === true) {
363: $methods = array();
364: }
365: list($plugin, $name) = pluginSplit($component, true);
366: $componentClass = $name . 'Component';
367: App::uses($componentClass, $plugin . 'Controller/Component');
368: if (!class_exists($componentClass)) {
369: throw new MissingComponentException(array(
370: 'class' => $componentClass
371: ));
372: }
373: $config = isset($controllerObj->components[$component]) ? $controllerObj->components[$component] : array();
374: $componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components, $config));
375: $controllerObj->Components->set($name, $componentObj);
376: $controllerObj->Components->enable($name);
377: }
378:
379: $controllerObj->constructClasses();
380: $this->_dirtyController = false;
381:
382: $this->controller = $controllerObj;
383: return $this->controller;
384: }
385:
386: }
387: