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

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.0
      • 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
        • Auth
    • Core
    • Error
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • 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/view/1196/Testing>
  8:  * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://book.cakephp.org/view/1196/Testing 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: 
 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 boolean
 45:  */
 46:     public $loadRoutes = true;
 47: 
 48: /**
 49:  * Returns the test controller
 50:  *
 51:  * @return Controller
 52:  */
 53:     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:         }
 66:         if (isset($object->request)) {
 67:             $object->request = $request;
 68:         }
 69:         return $this->testController;
 70:     }
 71: 
 72: /**
 73:  * Loads routes and resets if the test case dictates it should
 74:  *
 75:  * @return void
 76:  */
 77:     protected function _loadRoutes() {
 78:         parent::_loadRoutes();
 79:         if (!$this->loadRoutes) {
 80:             Router::reload();
 81:         }
 82:     }
 83: }
 84: 
 85: /**
 86:  * InterceptContentHelper class
 87:  *
 88:  * @package       Cake.TestSuite
 89:  */
 90: class InterceptContentHelper extends Helper {
 91: 
 92: /**
 93:  * Intercepts and stores the contents of the view before the layout is rendered
 94:  *
 95:  * @param string $viewFile The view file
 96:  */
 97:     public function afterRender($viewFile) {
 98:         $this->_View->_viewNoLayout = $this->_View->output;
 99:         $this->_View->Helpers->unload('InterceptContent');
100:     }
101: }
102: 
103: /**
104:  * ControllerTestCase class
105:  *
106:  * @package       Cake.TestSuite
107:  */
108: abstract class ControllerTestCase extends CakeTestCase {
109: 
110: /**
111:  * The controller to test in testAction
112:  *
113:  * @var Controller
114:  */
115:     public $controller = null;
116: 
117: /**
118:  * Automatically mock controllers that aren't mocked
119:  *
120:  * @var boolean
121:  */
122:     public $autoMock = true;
123: 
124: /**
125:  * Use custom routes during tests
126:  *
127:  * @var boolean
128:  */
129:     public $loadRoutes = true;
130: 
131: /**
132:  * The resulting view vars of the last testAction call
133:  *
134:  * @var array
135:  */
136:     public $vars = null;
137: 
138: /**
139:  * The resulting rendered view of the last testAction call
140:  *
141:  * @var string
142:  */
143:     public $view = null;
144: 
145: /**
146:  * The resulting rendered layout+view of the last testAction call
147:  *
148:  * @var string
149:  */
150:     public $contents = null;
151: 
152: /**
153:  * The returned result of the dispatch (requestAction), if any
154:  *
155:  * @var string
156:  */
157:     public $result = null;
158: 
159: /**
160:  * The headers that would have been sent by the action
161:  *
162:  * @var string
163:  */
164:     public $headers = null;
165: 
166: /**
167:  * Flag for checking if the controller instance is dirty.
168:  * Once a test has been run on a controller it should be rebuilt
169:  * to clean up properties.
170:  *
171:  * @var boolean
172:  */
173:     private $__dirtyController = false;
174: 
175: /**
176:  * Used to enable calling ControllerTestCase::testAction() without the testing
177:  * framework thinking that it's a test case
178:  *
179:  * @param string $name The name of the function
180:  * @param array $arguments Array of arguments
181:  * @return Function
182:  */
183:     public function __call($name, $arguments) {
184:         if ($name == 'testAction') {
185:             return call_user_func_array(array($this, '_testAction'), $arguments);
186:         }
187:     }
188: 
189: /**
190:  * Lets you do functional tests of a controller action.
191:  *
192:  * ### Options:
193:  *
194:  * - `data` Will be used as the request data.  If the `method` is GET,
195:  *   data will be used a GET params.  If the `method` is POST, it will be used
196:  *   as POST data. By setting `$options['data']` to a string, you can simulate XML or JSON
197:  *   payloads to your controllers allowing you to test REST webservices.
198:  * - `method` POST or GET. Defaults to POST.
199:  * - `return` Specify the return type you want.  Choose from:
200:  *     - `vars` Get the set view variables.
201:  *     - `view` Get the rendered view, without a layout.
202:  *     - `contents` Get the rendered view including the layout.
203:  *     - `result` Get the return value of the controller action.  Useful
204:  *       for testing requestAction methods.
205:  *
206:  * @param string $url The url to test
207:  * @param array $options See options
208:  */
209:     protected function _testAction($url = '', $options = array()) {
210:         $this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
211: 
212:         $options = array_merge(array(
213:             'data' => array(),
214:             'method' => 'POST',
215:             'return' => 'result'
216:         ), $options);
217: 
218:         $_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
219:         if (is_array($options['data'])) {
220:             if (strtoupper($options['method']) == 'GET') {
221:                 $_GET = $options['data'];
222:                 $_POST = array();
223:             } else {
224:                 $_POST = $options['data'];
225:                 $_GET = array();
226:             }
227:         }
228:         $request = $this->getMock('CakeRequest', array('_readInput'), array($url));
229: 
230:         if (is_string($options['data'])) {
231:             $request->expects($this->any())
232:                 ->method('_readInput')
233:                 ->will($this->returnValue($options['data']));
234:         }
235: 
236:         $Dispatch = new ControllerTestDispatcher();
237:         foreach (Router::$routes as $route) {
238:             if ($route instanceof RedirectRoute) {
239:                 $route->response = $this->getMock('CakeResponse', array('send'));
240:             }
241:         }
242:         $Dispatch->loadRoutes = $this->loadRoutes;
243:         $request = $Dispatch->parseParams($request);
244:         if (!isset($request->params['controller'])) {
245:             $this->headers = Router::currentRoute()->response->header();
246:             return;
247:         }
248:         if ($this->__dirtyController) {
249:             $this->controller = null;
250:         }
251: 
252:         $plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
253:         if ($this->controller === null && $this->autoMock) {
254:             $this->generate($plugin . Inflector::camelize($request->params['controller']));
255:         }
256:         $params = array();
257:         if ($options['return'] == 'result') {
258:             $params['return'] = 1;
259:             $params['bare'] = 1;
260:             $params['requested'] = 1;
261:         }
262:         $Dispatch->testController = $this->controller;
263:         $Dispatch->response = $this->getMock('CakeResponse', array('send'));
264:         $this->result = $Dispatch->dispatch($request, $Dispatch->response, $params);
265:         $this->controller = $Dispatch->testController;
266:         $this->vars = $this->controller->viewVars;
267:         $this->contents = $this->controller->response->body();
268:         if (isset($this->controller->View)) {
269:             $this->view = $this->controller->View->_viewNoLayout;
270:         }
271:         $this->__dirtyController = true;
272:         $this->headers = $Dispatch->response->header();
273:         return $this->{$options['return']};
274:     }
275: 
276: /**
277:  * Generates a mocked controller and mocks any classes passed to `$mocks`. By
278:  * default, `_stop()` is stubbed as is sending the response headers, so to not
279:  * interfere with testing.
280:  *
281:  * ### Mocks:
282:  *
283:  * - `methods` Methods to mock on the controller. `_stop()` is mocked by default
284:  * - `models` Models to mock. Models are added to the ClassRegistry so they any
285:  *   time they are instantiated the mock will be created. Pass as key value pairs
286:  *   with the value being specific methods on the model to mock. If `true` or
287:  *   no value is passed, the entire model will be mocked.
288:  * - `components` Components to mock. Components are only mocked on this controller
289:  *   and not within each other (i.e., components on components)
290:  *
291:  * @param string $controller Controller name
292:  * @param array $mocks List of classes and methods to mock
293:  * @return Controller Mocked controller
294:  */
295:     public function generate($controller, $mocks = array()) {
296:         list($plugin, $controller) = pluginSplit($controller);
297:         if ($plugin) {
298:             App::uses($plugin . 'AppController', $plugin . '.Controller');
299:             $plugin .= '.';
300:         }
301:         App::uses($controller . 'Controller', $plugin . 'Controller');
302:         if (!class_exists($controller.'Controller')) {
303:             throw new MissingControllerException(array(
304:                 'class' => $controller . 'Controller',
305:                 'plugin' => substr($plugin, 0, -1)
306:             ));
307:         }
308:         ClassRegistry::flush();
309: 
310:         $mocks = array_merge_recursive(array(
311:             'methods' => array('_stop'),
312:             'models' => array(),
313:             'components' => array()
314:         ), (array)$mocks);
315: 
316:         list($plugin, $name) = pluginSplit($controller);
317:         $_controller = $this->getMock($name.'Controller', $mocks['methods'], array(), '', false);
318:         $_controller->name = $name;
319:         $request = $this->getMock('CakeRequest');
320:         $response = $this->getMock('CakeResponse', array('_sendHeader'));
321:         $_controller->__construct($request, $response);
322: 
323:         $config = ClassRegistry::config('Model');
324:         foreach ($mocks['models'] as $model => $methods) {
325:             if (is_string($methods)) {
326:                 $model = $methods;
327:                 $methods = true;
328:             }
329:             if ($methods === true) {
330:                 $methods = array();
331:             }
332:             ClassRegistry::init($model);
333:             list($plugin, $name) = pluginSplit($model);
334:             $config = array_merge((array)$config, array('name' => $model));
335:             $_model = $this->getMock($name, $methods, array($config));
336:             ClassRegistry::removeObject($name);
337:             ClassRegistry::addObject($name, $_model);
338:         }
339: 
340:         foreach ($mocks['components'] as $component => $methods) {
341:             if (is_string($methods)) {
342:                 $component = $methods;
343:                 $methods = true;
344:             }
345:             if ($methods === true) {
346:                 $methods = array();
347:             }
348:             list($plugin, $name) = pluginSplit($component, true);
349:             $componentClass = $name . 'Component';
350:             App::uses($componentClass, $plugin . 'Controller/Component');
351:             if (!class_exists($componentClass)) {
352:                 throw new MissingComponentException(array(
353:                     'class' => $componentClass
354:                 ));
355:             }
356:             $_component = $this->getMock($componentClass, $methods, array(), '', false);
357:             $_controller->Components->set($name, $_component);
358:         }
359: 
360:         $_controller->constructClasses();
361:         $this->__dirtyController = false;
362: 
363:         $this->controller = $_controller;
364:         return $this->controller;
365:     }
366: }
367: 
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