ControllerTestCase.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.libs
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT');
  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. * ControllerTestDispatcher class
  28. *
  29. * @package cake.tests.lib
  30. */
  31. class ControllerTestDispatcher extends Dispatcher {
  32. /**
  33. * The controller to use in the dispatch process
  34. *
  35. * @var Controller
  36. */
  37. public $testController = null;
  38. /**
  39. * Use custom routes during tests
  40. *
  41. * @var boolean
  42. */
  43. public $loadRoutes = true;
  44. /**
  45. * Returns the test controller
  46. *
  47. * @return Controller
  48. */
  49. function _getController($request) {
  50. if ($this->testController === null) {
  51. $this->testController = parent::_getController($request);
  52. }
  53. $this->testController->helpers = array_merge(array('InterceptContent'), $this->testController->helpers);
  54. $this->testController->setRequest($request);
  55. $this->testController->response = $this->response;
  56. return $this->testController;
  57. }
  58. /**
  59. * Loads routes and resets if the test case dictates it should
  60. *
  61. * @return void
  62. */
  63. protected function _loadRoutes() {
  64. parent::_loadRoutes();
  65. if (!$this->loadRoutes) {
  66. Router::reload();
  67. }
  68. }
  69. }
  70. /**
  71. * InterceptContentHelper class
  72. *
  73. * @package cake.tests.lib
  74. */
  75. class InterceptContentHelper extends Helper {
  76. /**
  77. * Intercepts and stores the contents of the view before the layout is rendered
  78. *
  79. * @param string $viewFile The view file
  80. */
  81. function afterRender($viewFile) {
  82. $this->_View->_viewNoLayout = $this->_View->output;
  83. $this->_View->Helpers->unload('InterceptContent');
  84. }
  85. }
  86. /**
  87. * ControllerTestCase class
  88. *
  89. * @package cake.tests.lib
  90. */
  91. class ControllerTestCase extends CakeTestCase {
  92. /**
  93. * The controller to test in testAction
  94. *
  95. * @var Controller
  96. */
  97. public $controller = null;
  98. /**
  99. * Automatically mock controllers that aren't mocked
  100. *
  101. * @var boolean
  102. */
  103. public $autoMock = false;
  104. /**
  105. * Use custom routes during tests
  106. *
  107. * @var boolean
  108. */
  109. public $loadRoutes = true;
  110. /**
  111. * The resulting view vars of the last testAction call
  112. *
  113. * @var array
  114. */
  115. public $vars = null;
  116. /**
  117. * The resulting rendered view of the last testAction call
  118. *
  119. * @var string
  120. */
  121. public $view = null;
  122. /**
  123. * The resulting rendered layout+view of the last testAction call
  124. *
  125. * @var string
  126. */
  127. public $contents = null;
  128. /**
  129. * The returned result of the dispatch (requestAction), if any
  130. *
  131. * @var string
  132. */
  133. public $result = null;
  134. /**
  135. * The headers that would have been sent by the action
  136. *
  137. * @var string
  138. */
  139. public $headers = null;
  140. /**
  141. * Used to enable calling ControllerTestCase::testAction() without the testing
  142. * framework thinking that it's a test case
  143. *
  144. * @param string $name The name of the function
  145. * @param array $arguments Array of arguments
  146. * @return Function
  147. */
  148. public function __call($name, $arguments) {
  149. if ($name == 'testAction') {
  150. return call_user_func_array(array($this, '_testAction'), $arguments);
  151. }
  152. }
  153. /**
  154. * Tests a controller action.
  155. *
  156. * ### Options:
  157. * - `data` POST or GET data to pass
  158. * - `method` POST or GET
  159. *
  160. * @param string $url The url to test
  161. * @param array $options See options
  162. */
  163. private function _testAction($url = '', $options = array()) {
  164. $this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
  165. $options = array_merge(array(
  166. 'data' => array(),
  167. 'method' => 'POST',
  168. 'return' => 'result'
  169. ), $options);
  170. if (strtoupper($options['method']) == 'GET') {
  171. $_GET = $options['data'];
  172. $_POST = array();
  173. } else {
  174. $_POST = array('data' => $options['data']);
  175. $_GET = array();
  176. }
  177. $request = new CakeRequest($url);
  178. $Dispatch = new ControllerTestDispatcher();
  179. foreach (Router::$routes as $route) {
  180. if (is_a($route, 'RedirectRoute')) {
  181. $route->response = $this->getMock('CakeResponse', array('send'));
  182. }
  183. }
  184. $Dispatch->loadRoutes = $this->loadRoutes;
  185. $request = $Dispatch->parseParams($request);
  186. if (!isset($request->params['controller'])) {
  187. $this->headers = Router::currentRoute()->response->header();
  188. return;
  189. }
  190. if ($this->controller !== null && Inflector::camelize($request->params['controller']) !== $this->controller->name) {
  191. $this->controller = null;
  192. }
  193. if ($this->controller === null && $this->autoMock) {
  194. $this->generate(Inflector::camelize($request->params['controller']));
  195. }
  196. $params = array();
  197. if ($options['return'] == 'result') {
  198. $params['return'] = 1;
  199. $params['bare'] = 1;
  200. $params['requested'] = 1;
  201. }
  202. $Dispatch->testController = $this->controller;
  203. $Dispatch->response = $this->getMock('CakeResponse', array('send'));
  204. $this->result = $Dispatch->dispatch($request, $params);
  205. $this->controller = $Dispatch->testController;
  206. if ($options['return'] != 'result') {
  207. $this->vars = $this->controller->View->viewVars;
  208. $this->view = $this->controller->View->_viewNoLayout;
  209. $this->contents = $this->controller->response->body();
  210. }
  211. $this->headers = $Dispatch->response->header();
  212. return $this->{$options['return']};
  213. }
  214. /**
  215. * Generates a mocked controller and mocks any classes passed to `$mocks`. By
  216. * default, `_stop()` is stubbed as is sending the response headers, so to not
  217. * interfere with testing.
  218. *
  219. * ### Mocks:
  220. *
  221. * - `methods` Methods to mock on the controller. `_stop()` is mocked by default
  222. * - `models` Models to mock. Models are added to the ClassRegistry so they any
  223. * time they are instatiated the mock will be created. Pass as key value pairs
  224. * with the value being specific methods on the model to mock. If `true` or
  225. * no value is passed, the entire model will be mocked.
  226. * - `components` Components to mock. Components are only mocked on this controller
  227. * and not within each other (i.e., components on components)
  228. *
  229. * @param string $controller Controller name
  230. * @param array $mocks List of classes and methods to mock
  231. * @return Controller Mocked controller
  232. */
  233. public function generate($controller, $mocks = array()) {
  234. list($plugin, $controller) = pluginSplit($controller);
  235. if ($plugin) {
  236. App::uses($plugin . 'AppController', $plugin . '.Controller');
  237. $plugin .= '.';
  238. }
  239. App::uses($controller . 'Controller', $plugin . 'Controller');
  240. if (!class_exists($controller.'Controller')) {
  241. throw new MissingControllerException(array('controller' => $controller.'Controller'));
  242. }
  243. ClassRegistry::flush();
  244. $mocks = array_merge_recursive(array(
  245. 'methods' => array('_stop'),
  246. 'models' => array(),
  247. 'components' => array()
  248. ), (array)$mocks);
  249. list($plugin, $name) = pluginSplit($controller);
  250. $_controller = $this->getMock($name.'Controller', $mocks['methods'], array(), '', false);
  251. $_controller->name = $name;
  252. $_controller->__construct();
  253. $config = ClassRegistry::config('Model');
  254. foreach ($mocks['models'] as $model => $methods) {
  255. if (is_string($methods)) {
  256. $model = $methods;
  257. $methods = true;
  258. }
  259. if ($methods === true) {
  260. $methods = array();
  261. }
  262. ClassRegistry::init($model);
  263. list($plugin, $name) = pluginSplit($model);
  264. $config = array_merge((array)$config, array('name' => $model));
  265. $_model = $this->getMock($name, $methods, array($config));
  266. ClassRegistry::removeObject($name);
  267. ClassRegistry::addObject($name, $_model);
  268. }
  269. foreach ($mocks['components'] as $component => $methods) {
  270. if (is_string($methods)) {
  271. $component = $methods;
  272. $methods = true;
  273. }
  274. if ($methods === true) {
  275. $methods = array();
  276. }
  277. list($plugin, $name) = pluginSplit($component, true);
  278. App::uses($name . 'Component', $plugin . 'Controller/Component');
  279. if (!class_exists($name . 'Component')) {
  280. throw new MissingComponentFileException(array(
  281. 'file' => $name . 'Component.php',
  282. 'class' => $name.'Component'
  283. ));
  284. }
  285. $_component = $this->getMock($name.'Component', $methods, array(), '', false);
  286. $_controller->Components->set($name, $_component);
  287. }
  288. $_controller->constructClasses();
  289. $this->controller = $_controller;
  290. return $this->controller;
  291. }
  292. }