TestShell.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /**
  3. * Test Suite Shell
  4. *
  5. * This Shell allows the running of test suites via the cake command line
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  10. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice
  14. *
  15. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  17. * @package cake.console.shells
  18. * @since CakePHP(tm) v 1.2.0.4433
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Shell', 'Console');
  22. App::uses('CakeTestSuiteDispatcher', 'TestSuite');
  23. App::uses('CakeTestSuiteCommand', 'TestSuite');
  24. App::uses('CakeTestLoader', 'TestSuite');
  25. class TestShell extends Shell {
  26. /**
  27. * Dispatcher object for the run.
  28. *
  29. * @var CakeTestDispatcher
  30. */
  31. protected $_dispatcher = null;
  32. /**
  33. * get the option parser for the test suite.
  34. *
  35. * @return void
  36. */
  37. public function getOptionParser() {
  38. $parser = new ConsoleOptionParser($this->name);
  39. $parser->description(array(
  40. __d('cake_console', 'The CakePHP Testsuite allows you to run test cases from the command line'),
  41. __d('cake_console', 'If run with no command line arguments, a list of available core test cases will be shown')
  42. ))->addArgument('category', array(
  43. 'help' => __d('cake_console', 'app, core or name of a plugin.'),
  44. 'required' => true
  45. ))->addArgument('file', array(
  46. 'help' => __d('cake_console', 'file name with folder prefix and without the test.php suffix.'),
  47. 'required' => false,
  48. ))->addOption('log-junit', array(
  49. 'help' => __d('cake_console', '<file> Log test execution in JUnit XML format to file.'),
  50. 'default' => false
  51. ))->addOption('log-json', array(
  52. 'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
  53. 'default' => false
  54. ))->addOption('log-tap', array(
  55. 'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
  56. 'default' => false
  57. ))->addOption('log-dbus', array(
  58. 'help' => __d('cake_console', 'Log test execution to DBUS.'),
  59. 'default' => false
  60. ))->addOption('coverage-html', array(
  61. 'help' => __d('cake_console', '<dir> Generate code coverage report in HTML format.'),
  62. 'default' => false
  63. ))->addOption('coverage-clover', array(
  64. 'help' => __d('cake_console', '<file> Write code coverage data in Clover XML format.'),
  65. 'default' => false
  66. ))->addOption('testdox-html', array(
  67. 'help' => __d('cake_console', '<file> Write agile documentation in HTML format to file.'),
  68. 'default' => false
  69. ))->addOption('testdox-text', array(
  70. 'help' => __d('cake_console', '<file> Write agile documentation in Text format to file.'),
  71. 'default' => false
  72. ))->addOption('filter', array(
  73. 'help' => __d('cake_console', '<pattern> Filter which tests to run.'),
  74. 'default' => false
  75. ))->addOption('group', array(
  76. 'help' => __d('cake_console', '<name> Only runs tests from the specified group(s).'),
  77. 'default' => false
  78. ))->addOption('exclude-group', array(
  79. 'help' => __d('cake_console', '<name> Exclude tests from the specified group(s).'),
  80. 'default' => false
  81. ))->addOption('list-groups', array(
  82. 'help' => __d('cake_console', 'List available test groups.'),
  83. 'boolean' => true
  84. ))->addOption('loader', array(
  85. 'help' => __d('cake_console', 'TestSuiteLoader implementation to use.'),
  86. 'default' => false
  87. ))->addOption('repeat', array(
  88. 'help' => __d('cake_console', '<times> Runs the test(s) repeatedly.'),
  89. 'default' => false
  90. ))->addOption('tap', array(
  91. 'help' => __d('cake_console', 'Report test execution progress in TAP format.'),
  92. 'boolean' => true
  93. ))->addOption('testdox', array(
  94. 'help' => __d('cake_console', 'Report test execution progress in TestDox format.'),
  95. 'default' => false,
  96. 'boolean' => true
  97. ))->addOption('no-colors', array(
  98. 'help' => __d('cake_console', 'Do not use colors in output.'),
  99. 'boolean' => true
  100. ))->addOption('stderr', array(
  101. 'help' => __d('cake_console', 'Write to STDERR instead of STDOUT.'),
  102. 'boolean' => true
  103. ))->addOption('stop-on-error', array(
  104. 'help' => __d('cake_console', 'Stop execution upon first error or failure.'),
  105. 'boolean' => true
  106. ))->addOption('stop-on-failure', array(
  107. 'help' => __d('cake_console', 'Stop execution upon first failure.'),
  108. 'boolean' => true
  109. ))->addOption('stop-on-skipped ', array(
  110. 'help' => __d('cake_console', 'Stop execution upon first skipped test.'),
  111. 'boolean' => true
  112. ))->addOption('stop-on-incomplete', array(
  113. 'help' => __d('cake_console', 'Stop execution upon first incomplete test.'),
  114. 'boolean' => true
  115. ))->addOption('strict', array(
  116. 'help' => __d('cake_console', 'Mark a test as incomplete if no assertions are made.'),
  117. 'boolean' => true
  118. ))->addOption('wait', array(
  119. 'help' => __d('cake_console', 'Waits for a keystroke after each test.'),
  120. 'boolean' => true
  121. ))->addOption('process-isolation', array(
  122. 'help' => __d('cake_console', 'Run each test in a separate PHP process.'),
  123. 'boolean' => true
  124. ))->addOption('no-globals-backup', array(
  125. 'help' => __d('cake_console', 'Do not backup and restore $GLOBALS for each test.'),
  126. 'boolean' => true
  127. ))->addOption('static-backup ', array(
  128. 'help' => __d('cake_console', 'Backup and restore static attributes for each test.'),
  129. 'boolean' => true
  130. ))->addOption('syntax-check', array(
  131. 'help' => __d('cake_console', 'Try to check source files for syntax errors.'),
  132. 'boolean' => true
  133. ))->addOption('bootstrap', array(
  134. 'help' => __d('cake_console', '<file> A "bootstrap" PHP file that is run before the tests.'),
  135. 'default' => false
  136. ))->addOption('configuration', array(
  137. 'help' => __d('cake_console', '<file> Read configuration from XML file.'),
  138. 'default' => false
  139. ))->addOption('no-configuration', array(
  140. 'help' => __d('cake_console', 'Ignore default configuration file (phpunit.xml).'),
  141. 'boolean' => true
  142. ))->addOption('include-path', array(
  143. 'help' => __d('cake_console', '<path(s)> Prepend PHP include_path with given path(s).'),
  144. 'default' => false
  145. ))->addOption('directive', array(
  146. 'help' => __d('cake_console', 'key[=value] Sets a php.ini value.'),
  147. 'default' => false
  148. ))->addOption('fixture', array(
  149. 'help' => __d('cake_console', 'Choose a custom fixture manager.'),
  150. ));
  151. return $parser;
  152. }
  153. /**
  154. * Initialization method installs PHPUnit and loads all plugins
  155. *
  156. * @return void
  157. */
  158. public function initialize() {
  159. $this->_dispatcher = new CakeTestSuiteDispatcher();
  160. $sucess = $this->_dispatcher->loadTestFramework();
  161. if (!$sucess) {
  162. throw new Exception(__d('cake_dev', 'Please install PHPUnit framework <info>(http://www.phpunit.de)</info>'));
  163. }
  164. }
  165. /**
  166. * Parse the CLI options into an array CakeTestDispatcher can use.
  167. *
  168. * @return array Array of params for CakeTestDispatcher
  169. */
  170. protected function parseArgs() {
  171. if (empty($this->args)) {
  172. return;
  173. }
  174. $params = array(
  175. 'core' => false,
  176. 'app' => false,
  177. 'plugin' => null,
  178. 'output' => 'text',
  179. );
  180. if (strpos($this->args[0], '.php')) {
  181. $category = $this->_mapFileToCategory($this->args[0]);
  182. $params['case'] = $this->_mapFileToCase($this->args[0], $category);
  183. } else {
  184. $category = $this->args[0];
  185. if (isset($this->args[1])) {
  186. $params['case'] = $this->args[1];
  187. }
  188. }
  189. if ($category === 'core') {
  190. $params['core'] = true;
  191. } elseif ($category === 'app') {
  192. $params['app'] = true;
  193. } else {
  194. $params['plugin'] = $category;
  195. }
  196. return $params;
  197. }
  198. /**
  199. * Converts the options passed to the shell as options for the PHPUnit cli runner
  200. *
  201. * @return array Array of params for CakeTestDispatcher
  202. */
  203. protected function runnerOptions() {
  204. $options = array();
  205. $params = $this->params;
  206. unset($params['help']);
  207. if (!empty($params['no-colors'])) {
  208. unset($params['no-colors'], $params['colors']);
  209. } else {
  210. $params['colors'] = true;
  211. }
  212. foreach ($params as $param => $value) {
  213. if ($value === false) {
  214. continue;
  215. }
  216. $options[] = '--' . $param;
  217. if (is_string($value)) {
  218. $options[] = $value;
  219. }
  220. }
  221. return $options;
  222. }
  223. /**
  224. * Main entry point to this shell
  225. *
  226. * @return void
  227. */
  228. public function main() {
  229. $this->out(__d('cake_console', 'CakePHP Test Shell'));
  230. $this->hr();
  231. $args = $this->parseArgs();
  232. if (empty($args['case'])) {
  233. return $this->available();
  234. }
  235. $this->run($args, $this->runnerOptions());
  236. }
  237. /**
  238. * Runs the test case from $runnerArgs
  239. *
  240. * @param array $runnerArgs list of arguments as obtained from parseArgs()
  241. * @param array $options list of options as constructed by runnerOptions()
  242. * @return void
  243. */
  244. protected function run($runnerArgs, $options = array()) {
  245. restore_error_handler();
  246. restore_error_handler();
  247. $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
  248. $testCli->run($options);
  249. }
  250. /**
  251. * Shows a list of available test cases and gives the option to run one of them
  252. *
  253. * @return void
  254. */
  255. public function available() {
  256. $params = $this->parseArgs();
  257. $testCases = CakeTestLoader::generateTestList($params);
  258. $app = $params['app'];
  259. $plugin = $params['plugin'];
  260. $title = "Core Test Cases:";
  261. $category = 'core';
  262. if ($app) {
  263. $title = "App Test Cases:";
  264. $category = 'app';
  265. } elseif ($plugin) {
  266. $title = Inflector::humanize($plugin) . " Test Cases:";
  267. $category = $plugin;
  268. }
  269. if (empty($testCases)) {
  270. $this->out(__d('cake_console', "No test cases available \n\n"));
  271. return $this->out($this->OptionParser->help());
  272. }
  273. $this->out($title);
  274. $i = 1;
  275. $cases = array();
  276. foreach ($testCases as $testCaseFile => $testCase) {
  277. $case = str_replace('Test.php', '', $testCase);
  278. $this->out("[$i] $case");
  279. $cases[$i] = $case;
  280. $i++;
  281. }
  282. while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
  283. if (is_numeric($choice) && isset($cases[$choice])) {
  284. $this->args[0] = $category;
  285. $this->args[1] = $cases[$choice];
  286. $this->run($this->parseArgs(), $this->runnerOptions());
  287. break;
  288. }
  289. if (is_string($choice) && in_array($choice, $cases)) {
  290. $this->args[0] = $category;
  291. $this->args[1] = $choice;
  292. $this->run($this->parseArgs(), $this->runnerOptions());
  293. break;
  294. }
  295. if ($choice == 'q') {
  296. break;
  297. }
  298. }
  299. }
  300. /**
  301. * Find the test case for the passed file. The file could itself be a test.
  302. *
  303. * @param mixed $file
  304. * @access protected
  305. * @return array(type, case)
  306. */
  307. protected function _mapFileToCase($file, $category) {
  308. if (!$category || (substr($file, -4) !== '.php')) {
  309. return false;
  310. }
  311. $testFile = $testCase = null;
  312. if (preg_match('@Test[\\\/]@', $file)) {
  313. if (substr($file, -8) === 'Test.php') {
  314. $testCase = substr($file, 0, -8);
  315. $testCase = str_replace(DS, '/', $testCase);
  316. if ($testCase = preg_replace('@.*Test\/cases\/@', '', $testCase)) {
  317. if ($category === 'core') {
  318. $testCase = str_replace('lib/Cake/Test/Case/', '', $testCase);
  319. }
  320. return $testCase;
  321. }
  322. throw new Exception(__d('cake_dev', 'Test case %s cannot be run via this shell', $testFile));
  323. return false;
  324. }
  325. }
  326. $file = substr($file, 0, -4);
  327. if ($category === 'core') {
  328. $testCase = str_replace(DS, '/', $file);
  329. $testCase = preg_replace('@.*lib/Cake/@', '', $file);
  330. $testCase[0] = strtoupper($testCase[0]);
  331. $testFile = CAKE . 'Test/Case/' . $testCase . 'Test.php';
  332. if (file_exists($testFile)) {
  333. return $testCase;
  334. }
  335. throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
  336. return false;
  337. }
  338. $testFile = preg_replace(
  339. '@(.*)((?:(?:Config|Console|Controller|Lib|Locale|Model|plugins|Plugin|Test|Vendor|View|webroot)[\\\/]).*$|App[-a-z]*$)@',
  340. '\1Test/Case/\2Test.php',
  341. $file
  342. );
  343. if (!file_exists($testFile)) {
  344. throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
  345. return false;
  346. }
  347. $testCase = substr($file, 0, -8);
  348. $testCase = str_replace(DS, '/', $testCase);
  349. $testCase = preg_replace('@.*Test/Case/@', '', $testCase);
  350. return $testCase;
  351. }
  352. /**
  353. * For the given file, what category of test is it? returns app, core or the name of the plugin
  354. *
  355. * @param mixed $file
  356. * @access protected
  357. * @return string
  358. */
  359. protected function _mapFileToCategory($file) {
  360. $_file = realpath($file);
  361. if ($_file) {
  362. $file = $_file;
  363. }
  364. $file = str_replace(DS, '/', $file);
  365. if (strpos($file, 'lib/Cake/') !== false) {
  366. return 'core';
  367. } elseif (preg_match('@(?:plugins|Plugin)/([^/]*)@', $file, $match)) {
  368. return $match[1];
  369. }
  370. return 'app';
  371. }
  372. }