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.5 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.5
      • 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:  * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7:  *
  8:  * Licensed under The MIT License
  9:  * For full copyright and license information, please see the LICENSE.txt
 10:  * Redistributions of files must retain the above copyright notice
 11:  *
 12:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 13:  * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
 14:  * @package       Cake.TestSuite
 15:  * @since         CakePHP(tm) v 2.0
 16:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 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:  * ControllerTestDispatcher class
 29:  *
 30:  * @package       Cake.TestSuite
 31:  */
 32: class ControllerTestDispatcher extends Dispatcher {
 33: 
 34: /**
 35:  * The controller to use in the dispatch process
 36:  *
 37:  * @var Controller
 38:  */
 39:     public $testController = null;
 40: 
 41: /**
 42:  * Use custom routes during tests
 43:  *
 44:  * @var bool
 45:  */
 46:     public $loadRoutes = true;
 47: 
 48: /**
 49:  * Returns the test controller
 50:  *
 51:  * @param CakeRequest $request The request instance.
 52:  * @param CakeResponse $response The response instance.
 53:  * @return Controller
 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:  * Loads routes and resets if the test case dictates it should
 76:  *
 77:  * @return void
 78:  */
 79:     protected function _loadRoutes() {
 80:         parent::_loadRoutes();
 81:         if (!$this->loadRoutes) {
 82:             Router::reload();
 83:         }
 84:     }
 85: 
 86: }
 87: 
 88: /**
 89:  * InterceptContentHelper class
 90:  *
 91:  * @package       Cake.TestSuite
 92:  */
 93: class InterceptContentHelper extends Helper {
 94: 
 95: /**
 96:  * Intercepts and stores the contents of the view before the layout is rendered
 97:  *
 98:  * @param string $viewFile The view file
 99:  * @return void
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:  * ControllerTestCase class
110:  *
111:  * @package       Cake.TestSuite
112:  */
113: abstract class ControllerTestCase extends CakeTestCase {
114: 
115: /**
116:  * The controller to test in testAction
117:  *
118:  * @var Controller
119:  */
120:     public $controller = null;
121: 
122: /**
123:  * Automatically mock controllers that aren't mocked
124:  *
125:  * @var bool
126:  */
127:     public $autoMock = true;
128: 
129: /**
130:  * Use custom routes during tests
131:  *
132:  * @var bool
133:  */
134:     public $loadRoutes = true;
135: 
136: /**
137:  * The resulting view vars of the last testAction call
138:  *
139:  * @var array
140:  */
141:     public $vars = null;
142: 
143: /**
144:  * The resulting rendered view of the last testAction call
145:  *
146:  * @var string
147:  */
148:     public $view = null;
149: 
150: /**
151:  * The resulting rendered layout+view of the last testAction call
152:  *
153:  * @var string
154:  */
155:     public $contents = null;
156: 
157: /**
158:  * The returned result of the dispatch (requestAction), if any
159:  *
160:  * @var string
161:  */
162:     public $result = null;
163: 
164: /**
165:  * The headers that would have been sent by the action
166:  *
167:  * @var string
168:  */
169:     public $headers = null;
170: 
171: /**
172:  * Flag for checking if the controller instance is dirty.
173:  * Once a test has been run on a controller it should be rebuilt
174:  * to clean up properties.
175:  *
176:  * @var bool
177:  */
178:     protected $_dirtyController = false;
179: 
180: /**
181:  * Used to enable calling ControllerTestCase::testAction() without the testing
182:  * framework thinking that it's a test case
183:  *
184:  * @param string $name The name of the function
185:  * @param array $arguments Array of arguments
186:  * @return mixed The return of _testAction.
187:  * @throws BadMethodCallException when you call methods that don't exist.
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:  * Lets you do functional tests of a controller action.
198:  *
199:  * ### Options:
200:  *
201:  * - `data` Will be used as the request data. If the `method` is GET,
202:  *   data will be used a GET params. If the `method` is POST, it will be used
203:  *   as POST data. By setting `$options['data']` to a string, you can simulate XML or JSON
204:  *   payloads to your controllers allowing you to test REST webservices.
205:  * - `method` POST or GET. Defaults to POST.
206:  * - `return` Specify the return type you want. Choose from:
207:  *     - `vars` Get the set view variables.
208:  *     - `view` Get the rendered view, without a layout.
209:  *     - `contents` Get the rendered view including the layout.
210:  *     - `result` Get the return value of the controller action. Useful
211:  *       for testing requestAction methods.
212:  *
213:  * @param string $url The URL to test
214:  * @param array $options See options
215:  * @return mixed The specified return type.
216:  * @triggers ControllerTestCase $Dispatch, array('request' => $request)
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:  * Generates a mocked controller and mocks any classes passed to `$mocks`. By
293:  * default, `_stop()` is stubbed as is sending the response headers, so to not
294:  * interfere with testing.
295:  *
296:  * ### Mocks:
297:  *
298:  * - `methods` Methods to mock on the controller. `_stop()` is mocked by default
299:  * - `models` Models to mock. Models are added to the ClassRegistry so any
300:  *   time they are instantiated the mock will be created. Pass as key value pairs
301:  *   with the value being specific methods on the model to mock. If `true` or
302:  *   no value is passed, the entire model will be mocked.
303:  * - `components` Components to mock. Components are only mocked on this controller
304:  *   and not within each other (i.e., components on components)
305:  *
306:  * @param string $controller Controller name
307:  * @param array $mocks List of classes and methods to mock
308:  * @return Controller Mocked controller
309:  * @throws MissingControllerException When controllers could not be created.
310:  * @throws MissingComponentException When components could not be created.
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: 
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