CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

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

Generated using CakePHP API Docs