pictcode / lib / Cake / TestSuite / ControllerTestCase.php @ master
履歴 | 表示 | アノテート | ダウンロード (11.161 KB)
| 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|array $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 | 
      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 | 
       * Generates a mocked controller and mocks any classes passed to `$mocks`. By
     | 
  
| 297 | 
       * default, `_stop()` is stubbed as is sending the response headers, so to not
     | 
  
| 298 | 
       * interfere with testing.
     | 
  
| 299 | 
       *
     | 
  
| 300 | 
       * ### Mocks:
     | 
  
| 301 | 
       *
     | 
  
| 302 | 
       * - `methods` Methods to mock on the controller. `_stop()` is mocked by default
     | 
  
| 303 | 
       * - `models` Models to mock. Models are added to the ClassRegistry so any
     | 
  
| 304 | 
       *   time they are instantiated the mock will be created. Pass as key value pairs
     | 
  
| 305 | 
       *   with the value being specific methods on the model to mock. If `true` or
     | 
  
| 306 | 
       *   no value is passed, the entire model will be mocked.
     | 
  
| 307 | 
       * - `components` Components to mock. Components are only mocked on this controller
     | 
  
| 308 | 
       *   and not within each other (i.e., components on components)
     | 
  
| 309 | 
       *
     | 
  
| 310 | 
       * @param string $controller Controller name
     | 
  
| 311 | 
       * @param array $mocks List of classes and methods to mock
     | 
  
| 312 | 
       * @return Controller Mocked controller
     | 
  
| 313 | 
       * @throws MissingControllerException When controllers could not be created.
     | 
  
| 314 | 
       * @throws MissingComponentException When components could not be created.
     | 
  
| 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 | 
      }  |