TestsuiteShell.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 TestsuiteShell 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. 'The CakePHP Testsuite allows you to run test cases from the command line',
  41. 'If run with no command line arguments, a list of available core test cases will be shown'
  42. ))->addArgument('category', array(
  43. 'help' => __('app, core or name of a plugin.'),
  44. 'required' => true
  45. ))->addArgument('file', array(
  46. 'help' => __('file name with folder prefix and without the test.php suffix.'),
  47. 'required' => false,
  48. ))->addOption('log-junit', array(
  49. 'help' => __('<file> Log test execution in JUnit XML format to file.'),
  50. 'default' => false
  51. ))->addOption('log-json', array(
  52. 'help' => __('<file> Log test execution in TAP format to file.'),
  53. 'default' => false
  54. ))->addOption('log-tap', array(
  55. 'help' => __('<file> Log test execution in TAP format to file.'),
  56. 'default' => false
  57. ))->addOption('log-dbus', array(
  58. 'help' => __('Log test execution to DBUS.'),
  59. 'default' => false
  60. ))->addOption('coverage-html', array(
  61. 'help' => __('<dir> Generate code coverage report in HTML format.'),
  62. 'default' => false
  63. ))->addOption('coverage-clover', array(
  64. 'help' => __('<file> Write code coverage data in Clover XML format.'),
  65. 'default' => false
  66. ))->addOption('testdox-html', array(
  67. 'help' => __('<file> Write agile documentation in HTML format to file.'),
  68. 'default' => false
  69. ))->addOption('testdox-text', array(
  70. 'help' => __('<file> Write agile documentation in Text format to file.'),
  71. 'default' => false
  72. ))->addOption('filter', array(
  73. 'help' => __('<pattern> Filter which tests to run.'),
  74. 'default' => false
  75. ))->addOption('group', array(
  76. 'help' => __('<name> Only runs tests from the specified group(s).'),
  77. 'default' => false
  78. ))->addOption('exclude-group', array(
  79. 'help' => __('<name> Exclude tests from the specified group(s).'),
  80. 'default' => false
  81. ))->addOption('list-groups', array(
  82. 'help' => __('List available test groups.'),
  83. 'boolean' => true
  84. ))->addOption('loader', array(
  85. 'help' => __('TestSuiteLoader implementation to use.'),
  86. 'default' => false
  87. ))->addOption('repeat', array(
  88. 'help' => __('<times> Runs the test(s) repeatedly.'),
  89. 'default' => false
  90. ))->addOption('tap', array(
  91. 'help' => __('Report test execution progress in TAP format.'),
  92. 'boolean' => true
  93. ))->addOption('testdox', array(
  94. 'help' => __('Report test execution progress in TestDox format.'),
  95. 'default' => false,
  96. 'boolean' => true
  97. ))->addOption('no-colors', array(
  98. 'help' => __('Do not use colors in output.'),
  99. 'boolean' => true
  100. ))->addOption('stderr', array(
  101. 'help' => __('Write to STDERR instead of STDOUT.'),
  102. 'boolean' => true
  103. ))->addOption('stop-on-error', array(
  104. 'help' => __('Stop execution upon first error or failure.'),
  105. 'boolean' => true
  106. ))->addOption('stop-on-failure', array(
  107. 'help' => __('Stop execution upon first failure.'),
  108. 'boolean' => true
  109. ))->addOption('stop-on-skipped ', array(
  110. 'help' => __('Stop execution upon first skipped test.'),
  111. 'boolean' => true
  112. ))->addOption('stop-on-incomplete', array(
  113. 'help' => __('Stop execution upon first incomplete test.'),
  114. 'boolean' => true
  115. ))->addOption('strict', array(
  116. 'help' => __('Mark a test as incomplete if no assertions are made.'),
  117. 'boolean' => true
  118. ))->addOption('wait', array(
  119. 'help' => __('Waits for a keystroke after each test.'),
  120. 'boolean' => true
  121. ))->addOption('process-isolation', array(
  122. 'help' => __('Run each test in a separate PHP process.'),
  123. 'boolean' => true
  124. ))->addOption('no-globals-backup', array(
  125. 'help' => __('Do not backup and restore $GLOBALS for each test.'),
  126. 'boolean' => true
  127. ))->addOption('static-backup ', array(
  128. 'help' => __('Backup and restore static attributes for each test.'),
  129. 'boolean' => true
  130. ))->addOption('syntax-check', array(
  131. 'help' => __('Try to check source files for syntax errors.'),
  132. 'boolean' => true
  133. ))->addOption('bootstrap', array(
  134. 'help' => __('<file> A "bootstrap" PHP file that is run before the tests.'),
  135. 'default' => false
  136. ))->addOption('configuration', array(
  137. 'help' => __('<file> Read configuration from XML file.'),
  138. 'default' => false
  139. ))->addOption('no-configuration', array(
  140. 'help' => __('Ignore default configuration file (phpunit.xml).'),
  141. 'boolean' => true
  142. ))->addOption('include-path', array(
  143. 'help' => __('<path(s)> Prepend PHP include_path with given path(s).'),
  144. 'default' => false
  145. ))->addOption('directive', array(
  146. 'help' => __('key[=value] Sets a php.ini value.'),
  147. 'default' => false
  148. ))->addOption('fixture', array(
  149. 'help' => __('Choose a custom fixture manager.'),
  150. ));
  151. return $parser;
  152. }
  153. /**
  154. * Initialization method installs Simpletest and loads all plugins
  155. *
  156. * @return void
  157. */
  158. public function initialize() {
  159. $this->_dispatcher = new CakeTestSuiteDispatcher();
  160. $this->_dispatcher->loadTestFramework();
  161. }
  162. /**
  163. * Parse the CLI options into an array CakeTestDispatcher can use.
  164. *
  165. * @return array Array of params for CakeTestDispatcher
  166. */
  167. protected function parseArgs() {
  168. if (empty($this->args)) {
  169. return;
  170. }
  171. $params = array(
  172. 'core' => false,
  173. 'app' => false,
  174. 'plugin' => null,
  175. 'output' => 'text',
  176. );
  177. $category = $this->args[0];
  178. if ($category == 'core') {
  179. $params['core'] = true;
  180. } elseif ($category == 'app') {
  181. $params['app'] = true;
  182. } elseif ($category != 'core') {
  183. $params['plugin'] = $category;
  184. }
  185. if (isset($this->args[1])) {
  186. $params['case'] = Inflector::underscore($this->args[1]);
  187. }
  188. return $params;
  189. }
  190. /**
  191. * Converts the options passed to the shell as options for the PHPUnit cli runner
  192. *
  193. * @return array Array of params for CakeTestDispatcher
  194. */
  195. protected function runnerOptions() {
  196. $options = array();
  197. $params = $this->params;
  198. unset($params['help']);
  199. if (!empty($params['no-colors'])) {
  200. unset($params['no-colors'], $params['colors']);
  201. } else {
  202. $params['colors'] = true;
  203. }
  204. foreach ($params as $param => $value) {
  205. if ($value === false) {
  206. continue;
  207. }
  208. $options[] = '--' . $param;
  209. if (is_string($value)) {
  210. $options[] = $value;
  211. }
  212. }
  213. return $options;
  214. }
  215. /**
  216. * Main entry point to this shell
  217. *
  218. * @return void
  219. */
  220. public function main() {
  221. $this->out(__('CakePHP Test Shell'));
  222. $this->hr();
  223. $args = $this->parseArgs();
  224. if (empty($args['case'])) {
  225. return $this->available();
  226. }
  227. $this->run($args, $this->runnerOptions());
  228. }
  229. /**
  230. * Runs the test case from $runnerArgs
  231. *
  232. * @param array $runnerArgs list of arguments as obtained from parseArgs()
  233. * @param array $options list of options as constructed by runnerOptions()
  234. * @return void
  235. */
  236. protected function run($runnerArgs, $options = array()) {
  237. restore_error_handler();
  238. restore_error_handler();
  239. $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
  240. $testCli->run($options);
  241. }
  242. /**
  243. * Shows a list of available test cases and gives the option to run one of them
  244. *
  245. * @return void
  246. */
  247. public function available() {
  248. $params = $this->parseArgs();
  249. $testCases = CakeTestLoader::generateTestList($params);
  250. $app = $params['app'];
  251. $plugin = $params['plugin'];
  252. $title = "Core Test Cases:";
  253. $category = 'core';
  254. if ($app) {
  255. $title = "App Test Cases:";
  256. $category = 'app';
  257. } elseif ($plugin) {
  258. $title = Inflector::humanize($plugin) . " Test Cases:";
  259. $category = $plugin;
  260. }
  261. if (empty($testCases)) {
  262. $this->out(__("No test cases available \n\n"));
  263. return $this->out($this->OptionParser->help());
  264. }
  265. $this->out($title);
  266. $i = 1;
  267. $cases = array();
  268. foreach ($testCases as $testCaseFile => $testCase) {
  269. $case = explode(DS, str_replace('.test.php', '', $testCase));
  270. $case[count($case) - 1] = Inflector::camelize($case[count($case) - 1]);
  271. $case = implode('/', $case);
  272. $this->out("[$i] $case");
  273. $cases[$i] = $case;
  274. $i++;
  275. }
  276. while ($choice = $this->in(__('What test case would you like to run?'), null, 'q')) {
  277. if (is_numeric($choice) && isset($cases[$choice])) {
  278. $this->args[0] = $category;
  279. $this->args[1] = $cases[$choice];
  280. $this->run($this->parseArgs(), $this->runnerOptions());
  281. break;
  282. }
  283. if (is_string($choice) && in_array($choice, $cases)) {
  284. $this->args[0] = $category;
  285. $this->args[1] = $choice;
  286. $this->run($this->parseArgs(), $this->runnerOptions());
  287. break;
  288. }
  289. if ($choice == 'q') {
  290. break;
  291. }
  292. }
  293. }
  294. }