TestsuiteShell.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  17. * @since CakePHP(tm) v 1.2.0.4433
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('Shell', 'Console');
  21. App::uses('CakeTestSuiteDispatcher', 'TestSuite');
  22. App::uses('CakeTestSuiteCommand', 'TestSuite');
  23. App::uses('CakeTestLoader', 'TestSuite');
  24. /**
  25. * Provides a CakePHP wrapper around PHPUnit.
  26. * Adds in CakePHP's fixtures and gives access to plugin, app and core test cases
  27. *
  28. * @package Cake.Console.Command
  29. */
  30. class TestsuiteShell extends Shell {
  31. /**
  32. * Dispatcher object for the run.
  33. *
  34. * @var CakeTestDispatcher
  35. */
  36. protected $_dispatcher = null;
  37. /**
  38. * get the option parser for the test suite.
  39. *
  40. * @return void
  41. */
  42. public function getOptionParser() {
  43. $parser = new ConsoleOptionParser($this->name);
  44. $parser->description(array(
  45. __d('cake_console', 'The CakePHP Testsuite allows you to run test cases from the command line'),
  46. __d('cake_console', 'If run with no command line arguments, a list of available core test cases will be shown')
  47. ))->addArgument('category', array(
  48. 'help' => __d('cake_console', 'app, core or name of a plugin.'),
  49. 'required' => true
  50. ))->addArgument('file', array(
  51. 'help' => __d('cake_console', 'file name with folder prefix and without the test.php suffix.'),
  52. 'required' => false,
  53. ))->addOption('log-junit', array(
  54. 'help' => __d('cake_console', '<file> Log test execution in JUnit XML format to file.'),
  55. 'default' => false
  56. ))->addOption('log-json', array(
  57. 'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
  58. 'default' => false
  59. ))->addOption('log-tap', array(
  60. 'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
  61. 'default' => false
  62. ))->addOption('log-dbus', array(
  63. 'help' => __d('cake_console', 'Log test execution to DBUS.'),
  64. 'default' => false
  65. ))->addOption('coverage-html', array(
  66. 'help' => __d('cake_console', '<dir> Generate code coverage report in HTML format.'),
  67. 'default' => false
  68. ))->addOption('coverage-clover', array(
  69. 'help' => __d('cake_console', '<file> Write code coverage data in Clover XML format.'),
  70. 'default' => false
  71. ))->addOption('testdox-html', array(
  72. 'help' => __d('cake_console', '<file> Write agile documentation in HTML format to file.'),
  73. 'default' => false
  74. ))->addOption('testdox-text', array(
  75. 'help' => __d('cake_console', '<file> Write agile documentation in Text format to file.'),
  76. 'default' => false
  77. ))->addOption('filter', array(
  78. 'help' => __d('cake_console', '<pattern> Filter which tests to run.'),
  79. 'default' => false
  80. ))->addOption('group', array(
  81. 'help' => __d('cake_console', '<name> Only runs tests from the specified group(s).'),
  82. 'default' => false
  83. ))->addOption('exclude-group', array(
  84. 'help' => __d('cake_console', '<name> Exclude tests from the specified group(s).'),
  85. 'default' => false
  86. ))->addOption('list-groups', array(
  87. 'help' => __d('cake_console', 'List available test groups.'),
  88. 'boolean' => true
  89. ))->addOption('loader', array(
  90. 'help' => __d('cake_console', 'TestSuiteLoader implementation to use.'),
  91. 'default' => false
  92. ))->addOption('repeat', array(
  93. 'help' => __d('cake_console', '<times> Runs the test(s) repeatedly.'),
  94. 'default' => false
  95. ))->addOption('tap', array(
  96. 'help' => __d('cake_console', 'Report test execution progress in TAP format.'),
  97. 'boolean' => true
  98. ))->addOption('testdox', array(
  99. 'help' => __d('cake_console', 'Report test execution progress in TestDox format.'),
  100. 'default' => false,
  101. 'boolean' => true
  102. ))->addOption('no-colors', array(
  103. 'help' => __d('cake_console', 'Do not use colors in output.'),
  104. 'boolean' => true
  105. ))->addOption('stderr', array(
  106. 'help' => __d('cake_console', 'Write to STDERR instead of STDOUT.'),
  107. 'boolean' => true
  108. ))->addOption('stop-on-error', array(
  109. 'help' => __d('cake_console', 'Stop execution upon first error or failure.'),
  110. 'boolean' => true
  111. ))->addOption('stop-on-failure', array(
  112. 'help' => __d('cake_console', 'Stop execution upon first failure.'),
  113. 'boolean' => true
  114. ))->addOption('stop-on-skipped ', array(
  115. 'help' => __d('cake_console', 'Stop execution upon first skipped test.'),
  116. 'boolean' => true
  117. ))->addOption('stop-on-incomplete', array(
  118. 'help' => __d('cake_console', 'Stop execution upon first incomplete test.'),
  119. 'boolean' => true
  120. ))->addOption('strict', array(
  121. 'help' => __d('cake_console', 'Mark a test as incomplete if no assertions are made.'),
  122. 'boolean' => true
  123. ))->addOption('wait', array(
  124. 'help' => __d('cake_console', 'Waits for a keystroke after each test.'),
  125. 'boolean' => true
  126. ))->addOption('process-isolation', array(
  127. 'help' => __d('cake_console', 'Run each test in a separate PHP process.'),
  128. 'boolean' => true
  129. ))->addOption('no-globals-backup', array(
  130. 'help' => __d('cake_console', 'Do not backup and restore $GLOBALS for each test.'),
  131. 'boolean' => true
  132. ))->addOption('static-backup ', array(
  133. 'help' => __d('cake_console', 'Backup and restore static attributes for each test.'),
  134. 'boolean' => true
  135. ))->addOption('syntax-check', array(
  136. 'help' => __d('cake_console', 'Try to check source files for syntax errors.'),
  137. 'boolean' => true
  138. ))->addOption('bootstrap', array(
  139. 'help' => __d('cake_console', '<file> A "bootstrap" PHP file that is run before the tests.'),
  140. 'default' => false
  141. ))->addOption('configuration', array(
  142. 'help' => __d('cake_console', '<file> Read configuration from XML file.'),
  143. 'default' => false
  144. ))->addOption('no-configuration', array(
  145. 'help' => __d('cake_console', 'Ignore default configuration file (phpunit.xml).'),
  146. 'boolean' => true
  147. ))->addOption('include-path', array(
  148. 'help' => __d('cake_console', '<path(s)> Prepend PHP include_path with given path(s).'),
  149. 'default' => false
  150. ))->addOption('directive', array(
  151. 'help' => __d('cake_console', 'key[=value] Sets a php.ini value.'),
  152. 'default' => false
  153. ))->addOption('fixture', array(
  154. 'help' => __d('cake_console', 'Choose a custom fixture manager.'),
  155. ));
  156. return $parser;
  157. }
  158. /**
  159. * Initialization method installs PHPUnit and loads all plugins
  160. *
  161. * @return void
  162. */
  163. public function initialize() {
  164. $this->_dispatcher = new CakeTestSuiteDispatcher();
  165. $sucess = $this->_dispatcher->loadTestFramework();
  166. if (!$sucess) {
  167. throw new Exception(__d('cake_dev', 'Please install PHPUnit framework <info>(http://www.phpunit.de)</info>'));
  168. }
  169. }
  170. /**
  171. * Parse the CLI options into an array CakeTestDispatcher can use.
  172. *
  173. * @return array Array of params for CakeTestDispatcher
  174. */
  175. protected function parseArgs() {
  176. if (empty($this->args)) {
  177. return;
  178. }
  179. $params = array(
  180. 'core' => false,
  181. 'app' => false,
  182. 'plugin' => null,
  183. 'output' => 'text',
  184. );
  185. $category = $this->args[0];
  186. if ($category == 'core') {
  187. $params['core'] = true;
  188. } elseif ($category == 'app') {
  189. $params['app'] = true;
  190. } elseif ($category != 'core') {
  191. $params['plugin'] = $category;
  192. }
  193. if (isset($this->args[1])) {
  194. $params['case'] = $this->args[1];
  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. }