cake_test_case.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2007, Cake Software Foundation, Inc.
  12. * 1785 E. Sahara Avenue, Suite 490-204
  13. * Las Vegas, Nevada 89104
  14. *
  15. * Licensed under The Open Group Test Suite License
  16. * Redistributions of files must retain the above copyright notice.
  17. *
  18. * @filesource
  19. * @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
  20. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  21. * @package cake
  22. * @subpackage cake.cake.tests.libs
  23. * @since CakePHP(tm) v 1.2.0.4667
  24. * @version $Revision$
  25. * @modifiedby $LastChangedBy$
  26. * @lastmodified $Date$
  27. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  28. */
  29. require_once CAKE . 'tests' . DS . 'lib' . DS . 'cake_test_model.php';
  30. require_once CAKE . 'tests' . DS . 'lib' . DS . 'cake_test_fixture.php';
  31. vendor('simpletest'.DS.'unit_tester');
  32. /**
  33. * Short description for class.
  34. *
  35. * @package cake
  36. * @subpackage cake.cake.tests.lib
  37. */
  38. class CakeTestDispatcher extends Dispatcher {
  39. var $controller;
  40. var $testCase;
  41. function testCase(&$testCase) {
  42. $this->testCase =& $testCase;
  43. }
  44. function _invoke (&$controller, $params, $missingAction = false) {
  45. $this->controller =& $controller;
  46. if (isset($this->testCase) && method_exists($this->testCase, 'startController')) {
  47. $this->testCase->startController($this->controller, $params);
  48. }
  49. $result = parent::_invoke($this->controller, $params, $missingAction);
  50. if (isset($this->testCase) && method_exists($this->testCase, 'endController')) {
  51. $this->testCase->endController($this->controller, $params);
  52. }
  53. return $result;
  54. }
  55. }
  56. /**
  57. * Short description for class.
  58. *
  59. * @package cake
  60. * @subpackage cake.cake.tests.lib
  61. */
  62. class CakeTestCase extends UnitTestCase {
  63. /**
  64. * Methods used internally.
  65. *
  66. * @var array
  67. * @access private
  68. */
  69. var $methods = array('start', 'end', 'startcase', 'endcase', 'starttest', 'endtest');
  70. /**
  71. * Called when a test case (group of methods) is about to start (to be overriden when needed.)
  72. *
  73. * @param string $method Test method about to get executed.
  74. *
  75. * @access protected
  76. */
  77. function startCase() {
  78. }
  79. /**
  80. * Called when a test case (group of methods) has been executed (to be overriden when needed.)
  81. *
  82. * @param string $method Test method about that was executed.
  83. *
  84. * @access protected
  85. */
  86. function endCase() {
  87. }
  88. /**
  89. * Called when a test case method is about to start (to be overriden when needed.)
  90. *
  91. * @param string $method Test method about to get executed.
  92. *
  93. * @access protected
  94. */
  95. function startTest($method) {
  96. }
  97. /**
  98. * Called when a test case method has been executed (to be overriden when needed.)
  99. *
  100. * @param string $method Test method about that was executed.
  101. *
  102. * @access protected
  103. */
  104. function endTest($method) {
  105. }
  106. /**
  107. * Callback issued when a controller's action is about to be invoked through testAction().
  108. *
  109. * @param Controller $controller Controller that's about to be invoked.
  110. * @param array $params Additional parameters as sent by testAction().
  111. */
  112. function startController(&$controller, $params = array()) {
  113. if (isset($params['fixturize']) && ((is_array($params['fixturize']) && !empty($params['fixturize'])) || $params['fixturize'] === true)) {
  114. if (!isset($this->db)) {
  115. $this->_initDb();
  116. }
  117. $classRegistry =& ClassRegistry::getInstance();
  118. $models = array();
  119. foreach($classRegistry->__map as $key => $name) {
  120. $object =& $classRegistry->getObject(Inflector::camelize($key));
  121. if (is_subclass_of($object, 'Model') && ((is_array($params['fixturize']) && in_array($object->name, $params['fixturize'])) || $params['fixturize'] === true)) {
  122. $models[$object->name] = array (
  123. 'table' => $object->table,
  124. 'model' => $object->name,
  125. 'key' => Inflector::camelize($key)
  126. );
  127. }
  128. }
  129. if (!empty($models) && isset($this->db)) {
  130. $this->_queries = array(
  131. 'create' => array(),
  132. 'insert' => array(),
  133. 'drop' => array()
  134. );
  135. foreach($models as $model) {
  136. $fixture =& new CakeTestFixture($this->db);
  137. $fixture->name = $model['model'] . 'Test';
  138. $fixture->table = $model['table'];
  139. $fixture->import = array('model' => $model['model'], 'records' => true);
  140. $fixture->init();
  141. $createFixture = $fixture->create();
  142. $insertsFixture = $fixture->insert();
  143. $dropFixture = $fixture->drop();
  144. if (!empty($createFixture)) {
  145. $this->_queries['create'] = am($this->_queries['create'], array($createFixture));
  146. }
  147. if (!empty($insertsFixture)) {
  148. $this->_queries['insert'] = am($this->_queries['insert'], $insertsFixture);
  149. }
  150. if (!empty($dropFixture)) {
  151. $this->_queries['drop'] = am($this->_queries['drop'], array($dropFixture));
  152. }
  153. }
  154. foreach($this->_queries['create'] as $query) {
  155. if (isset($query) && $query !== false) {
  156. $this->db->_execute($query);
  157. }
  158. }
  159. foreach($this->_queries['insert'] as $query) {
  160. if (isset($query) && $query !== false) {
  161. $this->db->_execute($query);
  162. }
  163. }
  164. foreach($models as $model) {
  165. $object =& $classRegistry->getObject($model['key']);
  166. if ($object !== false) {
  167. $object->useDbConfig = 'test_suite';
  168. $object->setDataSource();
  169. }
  170. }
  171. }
  172. }
  173. }
  174. /**
  175. * Callback issued when a controller's action has been invoked through testAction().
  176. *
  177. * @param Controller $controller Controller that has been invoked.
  178. * * @param array $params Additional parameters as sent by testAction().
  179. */
  180. function endController(&$controller, $params = array()) {
  181. if (isset($this->db) && isset($this->_queries) && !empty($this->_queries) && !empty($this->_queries['drop'])) {
  182. foreach($this->_queries['drop'] as $query) {
  183. if (isset($query) && $query !== false) {
  184. $this->db->_execute($query);
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Executes a Cake URL, optionally getting the view rendering or what is returned
  191. * when params['requested'] is set.
  192. *
  193. * @param string $url Cake URL to execute (e.g: /articles/view/455)
  194. * @param array $params Parameters
  195. *
  196. * @return mixed What is returned from action (if $requested is true), or view rendered html
  197. *
  198. * @access protected
  199. */
  200. function testAction($url, $params = array()) {
  201. $default = array(
  202. 'return' => 'result',
  203. 'fixturize' => false,
  204. 'data' => array(),
  205. 'method' => 'post'
  206. );
  207. $params = am($default, $params);
  208. if (!empty($params['data'])) {
  209. $data = array('data' => $params['data']);
  210. if (low($params['method']) == 'get') {
  211. $_GET = $data;
  212. } else {
  213. $_POST = $data;
  214. }
  215. }
  216. $return = $params['return'];
  217. unset($params['data']);
  218. unset($params['method']);
  219. unset($params['return']);
  220. $dispatcher =& new CakeTestDispatcher();
  221. $dispatcher->testCase($this);
  222. if (low($return) != 'result') {
  223. $params['return'] = 0;
  224. ob_start();
  225. @$dispatcher->dispatch($url, $params);
  226. $result = ob_get_clean();
  227. if (low($return) == 'vars') {
  228. $view =& ClassRegistry::getObject('view');
  229. $viewVars = $view->getVars();
  230. $result = array();
  231. foreach($viewVars as $var) {
  232. $result[$var] = $view->getVar($var);
  233. }
  234. if (!empty($view->pageTitle)) {
  235. $result = am($result, array('title' => $view->pageTitle));
  236. }
  237. }
  238. } else {
  239. $params['return'] = 1;
  240. $params['bare'] = 1;
  241. $params['requested'] = 1;
  242. $result = @$dispatcher->dispatch($url, $params);
  243. }
  244. $classRegistry =& ClassRegistry::getInstance();
  245. $keys = array_keys($classRegistry->__objects);
  246. foreach($keys as $key) {
  247. $key = Inflector::camelize($key);
  248. $classRegistry->removeObject($key);
  249. }
  250. $classRegistry->__map = array();
  251. if (isset($this->_queries)) {
  252. unset($this->_queries);
  253. }
  254. return $result;
  255. }
  256. /**
  257. * Announces the start of a test.
  258. *
  259. * @param string $method Test method just started.
  260. *
  261. * @access public
  262. */
  263. function before($method) {
  264. parent::before($method);
  265. if (isset($this->fixtures) && (!is_array($this->fixtures) || empty($this->fixtures))) {
  266. unset($this->fixtures);
  267. }
  268. // Set up DB connection
  269. if (isset($this->fixtures) && low($method) == 'start') {
  270. $this->_initDb();
  271. $this->_loadFixtures();
  272. }
  273. // Create records
  274. if (isset($this->_fixtures) && isset($this->db) && !in_array(low($method), array('start', 'end'))) {
  275. foreach($this->_fixtures as $fixture) {
  276. $inserts = $fixture->insert();
  277. if (isset($inserts) && !empty($inserts)) {
  278. foreach($inserts as $query) {
  279. if (isset($query) && $query !== false) {
  280. $this->db->_execute($query);
  281. }
  282. }
  283. }
  284. }
  285. }
  286. if (!in_array(low($method), $this->methods)) {
  287. $this->startTest($method);
  288. }
  289. }
  290. /**
  291. * Runs as first test to create tables.
  292. *
  293. * @access public
  294. */
  295. function start() {
  296. if (isset($this->_fixtures) && isset($this->db)) {
  297. foreach($this->_fixtures as $fixture) {
  298. $query = $fixture->create();
  299. if (isset($query) && $query !== false) {
  300. $this->db->_execute($query);
  301. }
  302. }
  303. }
  304. }
  305. /**
  306. * Runs as last test to drop tables.
  307. *
  308. * @access public
  309. */
  310. function end() {
  311. if (isset($this->_fixtures) && isset($this->db)) {
  312. foreach(array_reverse($this->_fixtures) as $fixture) {
  313. $query = $fixture->drop();
  314. if (isset($query) && $query !== false) {
  315. $this->db->_execute($query);
  316. }
  317. }
  318. }
  319. }
  320. /**
  321. * Announces the end of a test.
  322. *
  323. * @param string $method Test method just finished.
  324. *
  325. * @access public
  326. */
  327. function after($method) {
  328. if (isset($this->_fixtures) && isset($this->db) && !in_array(low($method), array('start', 'end'))) {
  329. foreach($this->_fixtures as $fixture) {
  330. $query = $fixture->truncate();
  331. if (isset($query) && $query !== false) {
  332. $this->db->_execute($query);
  333. }
  334. }
  335. }
  336. if (!in_array(low($method), $this->methods)) {
  337. $this->endTest($method);
  338. }
  339. parent::after($method);
  340. }
  341. /**
  342. * Gets a list of test names. Normally that will be all internal methods that start with the
  343. * name "test". This method should be overridden if you want a different rule.
  344. *
  345. * @return array List of test names.
  346. *
  347. * @access public
  348. */
  349. function getTests() {
  350. $methods = array_diff(parent::getTests(), array('testAction', 'testaction'));
  351. $methods = am(am(array('start', 'startCase'), $methods), array('endCase', 'end'));
  352. return $methods;
  353. }
  354. /**
  355. * Initialize DB connection.
  356. *
  357. */
  358. function _initDb() {
  359. $testDbAvailable = false;
  360. if (class_exists('DATABASE_CONFIG')) {
  361. $dbConfig =& new DATABASE_CONFIG();
  362. $testDbAvailable = isset($dbConfig->test);
  363. }
  364. if ($testDbAvailable) {
  365. // Try for test DB
  366. restore_error_handler();
  367. @$db =& ConnectionManager::getDataSource('test');
  368. set_error_handler('simpleTestErrorHandler');
  369. $testDbAvailable = $db->isConnected();
  370. }
  371. // Try for default DB
  372. if (!$testDbAvailable) {
  373. $db =& ConnectionManager::getDataSource('default');
  374. }
  375. // Add test prefix
  376. $config = $db->config;
  377. $config['prefix'] .= 'test_suite_';
  378. // Set up db connection
  379. ConnectionManager::create('test_suite', $config);
  380. // Get db connection
  381. $this->db =& ConnectionManager::getDataSource('test_suite');
  382. $this->db->fullDebug = false;
  383. }
  384. /**
  385. * Load fixtures specified in var $fixtures.
  386. *
  387. * @access private
  388. */
  389. function _loadFixtures() {
  390. if (!isset($this->fixtures) || empty($this->fixtures)) {
  391. return;
  392. }
  393. if (!is_array($this->fixtures)) {
  394. $this->fixtures = array( $this->fixtures );
  395. }
  396. $this->_fixtures = array();
  397. foreach($this->fixtures as $index => $fixture) {
  398. $fixtureFile = null;
  399. if (strpos($fixture, 'core.') === 0) {
  400. $fixture = substr($fixture, strlen('core.'));
  401. $fixturePaths = array(
  402. CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures'
  403. );
  404. } else if (strpos($fixture, 'app.') === 0) {
  405. $fixture = substr($fixture, strlen('app.'));
  406. $fixturePaths = array(
  407. APP . 'tests' . DS . 'fixtures'
  408. );
  409. } else {
  410. $fixturePaths = array(
  411. TESTS . 'fixtures',
  412. CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures'
  413. );
  414. }
  415. foreach($fixturePaths as $path) {
  416. if (is_readable($path . DS . $fixture . '_fixture.php')) {
  417. $fixtureFile = $path . DS . $fixture . '_fixture.php';
  418. break;
  419. }
  420. }
  421. if (isset($fixtureFile)) {
  422. require_once($fixtureFile);
  423. $fixtureClass = Inflector::camelize($fixture) . 'Fixture';
  424. $this->_fixtures[$this->fixtures[$index]] =& new $fixtureClass($this->db);
  425. }
  426. }
  427. if (empty($this->_fixtures)) {
  428. unset($this->_fixtures);
  429. }
  430. }
  431. }
  432. ?>