TestsuiteShell.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. ))->addOption('debug', array(
  156. 'help' => __d('cake_console', 'More verbose output.'),
  157. ));
  158. return $parser;
  159. }
  160. /**
  161. * Initialization method installs PHPUnit and loads all plugins
  162. *
  163. * @return void
  164. */
  165. public function initialize() {
  166. $this->_dispatcher = new CakeTestSuiteDispatcher();
  167. $sucess = $this->_dispatcher->loadTestFramework();
  168. if (!$sucess) {
  169. throw new Exception(__d('cake_dev', 'Please install PHPUnit framework <info>(http://www.phpunit.de)</info>'));
  170. }
  171. }
  172. /**
  173. * Parse the CLI options into an array CakeTestDispatcher can use.
  174. *
  175. * @return array Array of params for CakeTestDispatcher
  176. */
  177. protected function parseArgs() {
  178. if (empty($this->args)) {
  179. return;
  180. }
  181. $params = array(
  182. 'core' => false,
  183. 'app' => false,
  184. 'plugin' => null,
  185. 'output' => 'text',
  186. );
  187. $category = $this->args[0];
  188. if ($category == 'core') {
  189. $params['core'] = true;
  190. } elseif ($category == 'app') {
  191. $params['app'] = true;
  192. } elseif ($category != 'core') {
  193. $params['plugin'] = $category;
  194. }
  195. if (isset($this->args[1])) {
  196. $params['case'] = $this->args[1];
  197. }
  198. return $params;
  199. }
  200. /**
  201. * Converts the options passed to the shell as options for the PHPUnit cli runner
  202. *
  203. * @return array Array of params for CakeTestDispatcher
  204. */
  205. protected function runnerOptions() {
  206. $options = array();
  207. $params = $this->params;
  208. unset($params['help']);
  209. if (!empty($params['no-colors'])) {
  210. unset($params['no-colors'], $params['colors']);
  211. } else {
  212. $params['colors'] = true;
  213. }
  214. foreach ($params as $param => $value) {
  215. if ($value === false) {
  216. continue;
  217. }
  218. $options[] = '--' . $param;
  219. if (is_string($value)) {
  220. $options[] = $value;
  221. }
  222. }
  223. return $options;
  224. }
  225. /**
  226. * Main entry point to this shell
  227. *
  228. * @return void
  229. */
  230. public function main() {
  231. $this->out(__d('cake_console', 'CakePHP Test Shell'));
  232. $this->hr();
  233. $args = $this->parseArgs();
  234. if (empty($args['case'])) {
  235. return $this->available();
  236. }
  237. $this->run($args, $this->runnerOptions());
  238. }
  239. /**
  240. * Runs the test case from $runnerArgs
  241. *
  242. * @param array $runnerArgs list of arguments as obtained from parseArgs()
  243. * @param array $options list of options as constructed by runnerOptions()
  244. * @return void
  245. */
  246. protected function run($runnerArgs, $options = array()) {
  247. restore_error_handler();
  248. restore_error_handler();
  249. $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
  250. $testCli->run($options);
  251. }
  252. /**
  253. * Shows a list of available test cases and gives the option to run one of them
  254. *
  255. * @return void
  256. */
  257. public function available() {
  258. $params = $this->parseArgs();
  259. $testCases = CakeTestLoader::generateTestList($params);
  260. $app = $params['app'];
  261. $plugin = $params['plugin'];
  262. $title = "Core Test Cases:";
  263. $category = 'core';
  264. if ($app) {
  265. $title = "App Test Cases:";
  266. $category = 'app';
  267. } elseif ($plugin) {
  268. $title = Inflector::humanize($plugin) . " Test Cases:";
  269. $category = $plugin;
  270. }
  271. if (empty($testCases)) {
  272. $this->out(__d('cake_console', "No test cases available \n\n"));
  273. return $this->out($this->OptionParser->help());
  274. }
  275. $this->out($title);
  276. $i = 1;
  277. $cases = array();
  278. foreach ($testCases as $testCaseFile => $testCase) {
  279. $case = str_replace('Test.php', '', $testCase);
  280. $this->out("[$i] $case");
  281. $cases[$i] = $case;
  282. $i++;
  283. }
  284. while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
  285. if (is_numeric($choice) && isset($cases[$choice])) {
  286. $this->args[0] = $category;
  287. $this->args[1] = $cases[$choice];
  288. $this->run($this->parseArgs(), $this->runnerOptions());
  289. break;
  290. }
  291. if (is_string($choice) && in_array($choice, $cases)) {
  292. $this->args[0] = $category;
  293. $this->args[1] = $choice;
  294. $this->run($this->parseArgs(), $this->runnerOptions());
  295. break;
  296. }
  297. if ($choice == 'q') {
  298. break;
  299. }
  300. }
  301. }
  302. }