TestsuiteShell.php 9.4 KB

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