2010-12-15 03:58:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ControllerTestCase file
|
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-12-15 03:58:27 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-12-15 03:58:27 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite
|
2010-12-15 03:58:27 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-14 04:26:41 +00:00
|
|
|
App::uses('Dispatcher', 'Routing');
|
|
|
|
App::uses('CakeTestCase', 'TestSuite');
|
|
|
|
App::uses('Router', 'Routing');
|
|
|
|
App::uses('CakeRequest', 'Network');
|
|
|
|
App::uses('CakeResponse', 'Network');
|
|
|
|
App::uses('Helper', 'View');
|
2012-04-15 17:20:27 +00:00
|
|
|
App::uses('CakeEvent', 'Event');
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ControllerTestDispatcher class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
class ControllerTestDispatcher extends Dispatcher {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The controller to use in the dispatch process
|
|
|
|
*
|
|
|
|
* @var Controller
|
|
|
|
*/
|
|
|
|
public $testController = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use custom routes during tests
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
public $loadRoutes = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the test controller
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param CakeRequest $request The request instance.
|
|
|
|
* @param CakeResponse $response The response instance.
|
2010-12-15 03:58:27 +00:00
|
|
|
* @return Controller
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
protected function _getController($request, $response) {
|
2010-12-15 03:58:27 +00:00
|
|
|
if ($this->testController === null) {
|
2011-07-04 00:19:08 +00:00
|
|
|
$this->testController = parent::_getController($request, $response);
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
$this->testController->helpers = array_merge(array('InterceptContent'), $this->testController->helpers);
|
|
|
|
$this->testController->setRequest($request);
|
|
|
|
$this->testController->response = $this->response;
|
2012-11-16 11:14:28 +00:00
|
|
|
foreach ($this->testController->Components->loaded() as $component) {
|
2011-11-01 18:19:40 +00:00
|
|
|
$object = $this->testController->Components->{$component};
|
|
|
|
if (isset($object->response)) {
|
|
|
|
$object->response = $response;
|
|
|
|
}
|
2012-03-24 19:04:29 +00:00
|
|
|
if (isset($object->request)) {
|
|
|
|
$object->request = $request;
|
|
|
|
}
|
2011-11-01 18:19:40 +00:00
|
|
|
}
|
2010-12-15 03:58:27 +00:00
|
|
|
return $this->testController;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads routes and resets if the test case dictates it should
|
2011-10-28 05:01:17 +00:00
|
|
|
*
|
2010-12-15 03:58:27 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-12-24 17:54:04 +00:00
|
|
|
protected function _loadRoutes() {
|
|
|
|
parent::_loadRoutes();
|
2010-12-15 03:58:27 +00:00
|
|
|
if (!$this->loadRoutes) {
|
|
|
|
Router::reload();
|
|
|
|
}
|
|
|
|
}
|
2012-03-05 02:51:44 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* InterceptContentHelper class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
class InterceptContentHelper extends Helper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Intercepts and stores the contents of the view before the layout is rendered
|
|
|
|
*
|
|
|
|
* @param string $viewFile The view file
|
2014-04-02 01:09:42 +00:00
|
|
|
* @return void
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function afterRender($viewFile) {
|
2011-12-18 04:50:41 +00:00
|
|
|
$this->_View->assign('__view_no_layout__', $this->_View->fetch('content'));
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->_View->Helpers->unload('InterceptContent');
|
|
|
|
}
|
2012-03-05 02:51:44 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ControllerTestCase class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite
|
2016-01-05 16:39:07 +00:00
|
|
|
* @method mixed testAction() testAction($url, $options = array()) Lets you do functional tests of a controller action.
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-07-31 02:35:17 +00:00
|
|
|
abstract class ControllerTestCase extends CakeTestCase {
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The controller to test in testAction
|
|
|
|
*
|
|
|
|
* @var Controller
|
|
|
|
*/
|
|
|
|
public $controller = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatically mock controllers that aren't mocked
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-11-06 14:36:27 +00:00
|
|
|
public $autoMock = true;
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use custom routes during tests
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
public $loadRoutes = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The resulting view vars of the last testAction call
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $vars = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The resulting rendered view of the last testAction call
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $view = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The resulting rendered layout+view of the last testAction call
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $contents = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The returned result of the dispatch (requestAction), if any
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $result = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The headers that would have been sent by the action
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $headers = null;
|
|
|
|
|
2011-10-22 14:58:21 +00:00
|
|
|
/**
|
|
|
|
* Flag for checking if the controller instance is dirty.
|
|
|
|
* Once a test has been run on a controller it should be rebuilt
|
|
|
|
* to clean up properties.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2011-10-22 14:58:21 +00:00
|
|
|
*/
|
2012-11-14 09:00:15 +00:00
|
|
|
protected $_dirtyController = false;
|
2016-04-19 19:27:30 +00:00
|
|
|
|
2016-04-19 07:50:40 +00:00
|
|
|
/**
|
|
|
|
* The class name to use for mocking the response object.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_responseClass = 'CakeResponse';
|
2011-10-22 14:58:21 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* Used to enable calling ControllerTestCase::testAction() without the testing
|
|
|
|
* framework thinking that it's a test case
|
|
|
|
*
|
|
|
|
* @param string $name The name of the function
|
|
|
|
* @param array $arguments Array of arguments
|
2014-12-19 17:20:48 +00:00
|
|
|
* @return mixed The return of _testAction.
|
2012-08-20 20:19:34 +00:00
|
|
|
* @throws BadMethodCallException when you call methods that don't exist.
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
public function __call($name, $arguments) {
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($name === 'testAction') {
|
2010-12-15 03:58:27 +00:00
|
|
|
return call_user_func_array(array($this, '_testAction'), $arguments);
|
|
|
|
}
|
2012-08-20 20:19:34 +00:00
|
|
|
throw new BadMethodCallException("Method '{$name}' does not exist.");
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-12 02:26:46 +00:00
|
|
|
* Lets you do functional tests of a controller action.
|
2010-12-15 03:58:27 +00:00
|
|
|
*
|
|
|
|
* ### Options:
|
2011-03-09 12:31:11 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `data` Will be used as the request data. If the `method` is GET,
|
|
|
|
* data will be used a GET params. If the `method` is POST, it will be used
|
2011-11-12 02:26:46 +00:00
|
|
|
* as POST data. By setting `$options['data']` to a string, you can simulate XML or JSON
|
|
|
|
* payloads to your controllers allowing you to test REST webservices.
|
2011-09-13 02:00:23 +00:00
|
|
|
* - `method` POST or GET. Defaults to POST.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `return` Specify the return type you want. Choose from:
|
2011-09-13 02:00:23 +00:00
|
|
|
* - `vars` Get the set view variables.
|
|
|
|
* - `view` Get the rendered view, without a layout.
|
|
|
|
* - `contents` Get the rendered view including the layout.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `result` Get the return value of the controller action. Useful
|
2011-09-13 02:00:23 +00:00
|
|
|
* for testing requestAction methods.
|
2010-12-15 03:58:27 +00:00
|
|
|
*
|
2014-12-23 12:46:55 +00:00
|
|
|
* @param string|array $url The URL to test.
|
2010-12-15 03:58:27 +00:00
|
|
|
* @param array $options See options
|
2014-12-19 17:20:48 +00:00
|
|
|
* @return mixed The specified return type.
|
2014-11-30 21:50:11 +00:00
|
|
|
* @triggers ControllerTestCase $Dispatch, array('request' => $request)
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2014-12-23 12:42:21 +00:00
|
|
|
protected function _testAction($url, $options = array()) {
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
|
|
|
|
|
2014-04-07 23:25:14 +00:00
|
|
|
$options += array(
|
2010-12-15 03:58:27 +00:00
|
|
|
'data' => array(),
|
|
|
|
'method' => 'POST',
|
|
|
|
'return' => 'result'
|
2014-04-07 23:25:14 +00:00
|
|
|
);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
2014-12-23 12:42:21 +00:00
|
|
|
if (is_array($url)) {
|
|
|
|
$url = Router::url($url);
|
|
|
|
}
|
|
|
|
|
2012-05-01 00:36:03 +00:00
|
|
|
$restore = array('get' => $_GET, 'post' => $_POST);
|
|
|
|
|
2011-08-29 21:24:22 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
|
2011-11-12 02:26:46 +00:00
|
|
|
if (is_array($options['data'])) {
|
2013-02-12 02:38:08 +00:00
|
|
|
if (strtoupper($options['method']) === 'GET') {
|
2011-11-12 02:26:46 +00:00
|
|
|
$_GET = $options['data'];
|
|
|
|
$_POST = array();
|
|
|
|
} else {
|
|
|
|
$_POST = $options['data'];
|
|
|
|
$_GET = array();
|
|
|
|
}
|
|
|
|
}
|
2017-04-18 18:54:34 +00:00
|
|
|
|
|
|
|
if (strpos($url, '?') !== false) {
|
|
|
|
list($url, $query) = explode('?', $url, 2);
|
|
|
|
parse_str($query, $queryArgs);
|
|
|
|
$_GET += $queryArgs;
|
|
|
|
}
|
|
|
|
|
2017-04-13 11:39:17 +00:00
|
|
|
$_SERVER['REQUEST_URI'] = $url;
|
2017-05-16 11:24:25 +00:00
|
|
|
/** @var CakeRequest|PHPUnit_Framework_MockObject_MockObject $request */
|
2017-04-13 11:39:17 +00:00
|
|
|
$request = $this->getMock('CakeRequest', array('_readInput'));
|
2011-11-12 02:26:46 +00:00
|
|
|
|
|
|
|
if (is_string($options['data'])) {
|
|
|
|
$request->expects($this->any())
|
|
|
|
->method('_readInput')
|
|
|
|
->will($this->returnValue($options['data']));
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
2011-11-12 02:26:46 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
$Dispatch = new ControllerTestDispatcher();
|
|
|
|
foreach (Router::$routes as $route) {
|
2011-08-20 13:58:35 +00:00
|
|
|
if ($route instanceof RedirectRoute) {
|
2010-12-15 03:58:27 +00:00
|
|
|
$route->response = $this->getMock('CakeResponse', array('send'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$Dispatch->loadRoutes = $this->loadRoutes;
|
2012-04-15 17:20:27 +00:00
|
|
|
$Dispatch->parseParams(new CakeEvent('ControllerTestCase', $Dispatch, array('request' => $request)));
|
2012-07-30 02:37:41 +00:00
|
|
|
if (!isset($request->params['controller']) && Router::currentRoute()) {
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->headers = Router::currentRoute()->response->header();
|
2015-09-25 15:22:00 +00:00
|
|
|
return null;
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
2012-11-14 09:00:15 +00:00
|
|
|
if ($this->_dirtyController) {
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->controller = null;
|
|
|
|
}
|
2011-10-22 14:58:21 +00:00
|
|
|
|
2011-08-20 13:58:35 +00:00
|
|
|
$plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
|
2010-12-15 03:58:27 +00:00
|
|
|
if ($this->controller === null && $this->autoMock) {
|
2011-12-12 18:29:33 +00:00
|
|
|
$this->generate($plugin . Inflector::camelize($request->params['controller']));
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
$params = array();
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($options['return'] === 'result') {
|
2010-12-15 03:58:27 +00:00
|
|
|
$params['return'] = 1;
|
|
|
|
$params['bare'] = 1;
|
|
|
|
$params['requested'] = 1;
|
|
|
|
}
|
|
|
|
$Dispatch->testController = $this->controller;
|
2016-04-19 07:50:40 +00:00
|
|
|
$Dispatch->response = $this->getMock($this->_responseClass, array('send', '_clearBuffer'));
|
2011-07-04 00:19:08 +00:00
|
|
|
$this->result = $Dispatch->dispatch($request, $Dispatch->response, $params);
|
2016-03-19 16:16:21 +00:00
|
|
|
|
|
|
|
// Clear out any stored requests.
|
2016-03-20 15:10:34 +00:00
|
|
|
while (Router::getRequest()) {
|
|
|
|
Router::popRequest();
|
|
|
|
}
|
2016-03-19 16:16:21 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->controller = $Dispatch->testController;
|
2011-12-02 14:19:15 +00:00
|
|
|
$this->vars = $this->controller->viewVars;
|
|
|
|
$this->contents = $this->controller->response->body();
|
|
|
|
if (isset($this->controller->View)) {
|
2011-12-18 04:50:41 +00:00
|
|
|
$this->view = $this->controller->View->fetch('__view_no_layout__');
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
2012-11-14 09:00:15 +00:00
|
|
|
$this->_dirtyController = true;
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->headers = $Dispatch->response->header();
|
2012-05-01 00:36:03 +00:00
|
|
|
|
|
|
|
$_GET = $restore['get'];
|
|
|
|
$_POST = $restore['post'];
|
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
return $this->{$options['return']};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a mocked controller and mocks any classes passed to `$mocks`. By
|
|
|
|
* default, `_stop()` is stubbed as is sending the response headers, so to not
|
|
|
|
* interfere with testing.
|
2011-10-28 05:01:17 +00:00
|
|
|
*
|
2010-12-15 03:58:27 +00:00
|
|
|
* ### Mocks:
|
2010-12-16 04:33:00 +00:00
|
|
|
*
|
2010-12-15 03:58:27 +00:00
|
|
|
* - `methods` Methods to mock on the controller. `_stop()` is mocked by default
|
2014-03-19 11:47:31 +00:00
|
|
|
* - `models` Models to mock. Models are added to the ClassRegistry so any
|
2011-12-02 05:58:09 +00:00
|
|
|
* time they are instantiated the mock will be created. Pass as key value pairs
|
2010-12-15 03:58:27 +00:00
|
|
|
* with the value being specific methods on the model to mock. If `true` or
|
|
|
|
* no value is passed, the entire model will be mocked.
|
|
|
|
* - `components` Components to mock. Components are only mocked on this controller
|
|
|
|
* and not within each other (i.e., components on components)
|
|
|
|
*
|
|
|
|
* @param string $controller Controller name
|
|
|
|
* @param array $mocks List of classes and methods to mock
|
|
|
|
* @return Controller Mocked controller
|
2012-03-05 02:51:44 +00:00
|
|
|
* @throws MissingControllerException When controllers could not be created.
|
|
|
|
* @throws MissingComponentException When components could not be created.
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
public function generate($controller, $mocks = array()) {
|
2011-03-09 05:35:58 +00:00
|
|
|
list($plugin, $controller) = pluginSplit($controller);
|
|
|
|
if ($plugin) {
|
|
|
|
App::uses($plugin . 'AppController', $plugin . '.Controller');
|
|
|
|
$plugin .= '.';
|
|
|
|
}
|
|
|
|
App::uses($controller . 'Controller', $plugin . 'Controller');
|
2012-03-05 02:51:44 +00:00
|
|
|
if (!class_exists($controller . 'Controller')) {
|
2011-10-16 11:40:36 +00:00
|
|
|
throw new MissingControllerException(array(
|
|
|
|
'class' => $controller . 'Controller',
|
|
|
|
'plugin' => substr($plugin, 0, -1)
|
|
|
|
));
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
ClassRegistry::flush();
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
$mocks = array_merge_recursive(array(
|
|
|
|
'methods' => array('_stop'),
|
2011-10-28 05:01:17 +00:00
|
|
|
'models' => array(),
|
2010-12-15 03:58:27 +00:00
|
|
|
'components' => array()
|
|
|
|
), (array)$mocks);
|
|
|
|
|
2011-01-19 01:50:34 +00:00
|
|
|
list($plugin, $name) = pluginSplit($controller);
|
2017-05-16 11:24:25 +00:00
|
|
|
/** @var Controller|PHPUnit_Framework_MockObject_MockObject $controllerObj */
|
2013-05-21 01:50:36 +00:00
|
|
|
$controllerObj = $this->getMock($name . 'Controller', $mocks['methods'], array(), '', false);
|
|
|
|
$controllerObj->name = $name;
|
2017-05-16 11:24:25 +00:00
|
|
|
/** @var CakeRequest|PHPUnit_Framework_MockObject_MockObject $request */
|
2011-07-04 00:19:08 +00:00
|
|
|
$request = $this->getMock('CakeRequest');
|
2017-05-16 11:24:25 +00:00
|
|
|
/** @var CakeResponse|PHPUnit_Framework_MockObject_MockObject $response */
|
2016-04-19 07:50:40 +00:00
|
|
|
$response = $this->getMock($this->_responseClass, array('_sendHeader'));
|
2013-05-21 01:50:36 +00:00
|
|
|
$controllerObj->__construct($request, $response);
|
2013-07-30 13:08:24 +00:00
|
|
|
$controllerObj->Components->setController($controllerObj);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
2010-12-16 04:33:00 +00:00
|
|
|
$config = ClassRegistry::config('Model');
|
2010-12-15 03:58:27 +00:00
|
|
|
foreach ($mocks['models'] as $model => $methods) {
|
|
|
|
if (is_string($methods)) {
|
|
|
|
$model = $methods;
|
|
|
|
$methods = true;
|
|
|
|
}
|
|
|
|
if ($methods === true) {
|
|
|
|
$methods = array();
|
|
|
|
}
|
2012-11-13 01:10:53 +00:00
|
|
|
$this->getMockForModel($model, $methods, $config);
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($mocks['components'] as $component => $methods) {
|
|
|
|
if (is_string($methods)) {
|
|
|
|
$component = $methods;
|
|
|
|
$methods = true;
|
|
|
|
}
|
|
|
|
if ($methods === true) {
|
|
|
|
$methods = array();
|
|
|
|
}
|
2017-12-31 05:25:00 +00:00
|
|
|
$config = isset($controllerObj->components[$component]) ? $controllerObj->components[$component] : array();
|
|
|
|
if (isset($config['className'])) {
|
|
|
|
$alias = $component;
|
|
|
|
$component = $config['className'];
|
|
|
|
}
|
2011-03-09 05:35:58 +00:00
|
|
|
list($plugin, $name) = pluginSplit($component, true);
|
2017-12-31 05:25:00 +00:00
|
|
|
if (!isset($alias)) {
|
|
|
|
$alias = $name;
|
|
|
|
}
|
2011-10-15 18:06:31 +00:00
|
|
|
$componentClass = $name . 'Component';
|
|
|
|
App::uses($componentClass, $plugin . 'Controller/Component');
|
|
|
|
if (!class_exists($componentClass)) {
|
|
|
|
throw new MissingComponentException(array(
|
|
|
|
'class' => $componentClass
|
2010-12-15 03:58:27 +00:00
|
|
|
));
|
2011-10-22 14:58:21 +00:00
|
|
|
}
|
2017-05-16 11:24:25 +00:00
|
|
|
/** @var Component|PHPUnit_Framework_MockObject_MockObject $componentObj */
|
2013-08-19 09:46:07 +00:00
|
|
|
$componentObj = $this->getMock($componentClass, $methods, array($controllerObj->Components, $config));
|
2017-12-31 05:25:00 +00:00
|
|
|
$controllerObj->Components->set($alias, $componentObj);
|
|
|
|
$controllerObj->Components->enable($alias);
|
|
|
|
unset($alias);
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 01:50:36 +00:00
|
|
|
$controllerObj->constructClasses();
|
2012-11-14 09:00:15 +00:00
|
|
|
$this->_dirtyController = false;
|
2010-12-15 03:58:27 +00:00
|
|
|
|
2013-05-21 01:50:36 +00:00
|
|
|
$this->controller = $controllerObj;
|
2010-12-15 03:58:27 +00:00
|
|
|
return $this->controller;
|
|
|
|
}
|
2012-03-05 02:51:44 +00:00
|
|
|
|
2018-08-13 10:26:26 +00:00
|
|
|
/**
|
|
|
|
* Unsets some properties to free memory.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
unset($this->contents);
|
|
|
|
unset($this->controller);
|
|
|
|
unset($this->headers);
|
|
|
|
unset($this->result);
|
|
|
|
unset($this->view);
|
|
|
|
unset($this->vars);
|
|
|
|
}
|
2011-07-04 00:19:08 +00:00
|
|
|
}
|