TestShell.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /**
  3. * Test 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/2.0/en/development/testing.html>
  10. * Copyright 2005-2012, 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-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/2.0/en/development/testing.html
  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 TestShell 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. ))->addArgument('category', array(
  47. 'help' => __d('cake_console', 'The category for the test, or test file, to test.'),
  48. 'required' => false,
  49. ))->addArgument('file', array(
  50. 'help' => __d('cake_console', 'The path to the file, or test file, to test.'),
  51. 'required' => false,
  52. ))->addOption('log-junit', array(
  53. 'help' => __d('cake_console', '<file> Log test execution in JUnit XML format to file.'),
  54. 'default' => false
  55. ))->addOption('log-json', array(
  56. 'help' => __d('cake_console', '<file> Log test execution in JSON format to file.'),
  57. 'default' => false
  58. ))->addOption('log-tap', array(
  59. 'help' => __d('cake_console', '<file> Log test execution in TAP format to file.'),
  60. 'default' => false
  61. ))->addOption('log-dbus', array(
  62. 'help' => __d('cake_console', 'Log test execution to DBUS.'),
  63. 'default' => false
  64. ))->addOption('coverage-html', array(
  65. 'help' => __d('cake_console', '<dir> Generate code coverage report in HTML format.'),
  66. 'default' => false
  67. ))->addOption('coverage-clover', array(
  68. 'help' => __d('cake_console', '<file> Write code coverage data in Clover XML format.'),
  69. 'default' => false
  70. ))->addOption('testdox-html', array(
  71. 'help' => __d('cake_console', '<file> Write agile documentation in HTML format to file.'),
  72. 'default' => false
  73. ))->addOption('testdox-text', array(
  74. 'help' => __d('cake_console', '<file> Write agile documentation in Text format to file.'),
  75. 'default' => false
  76. ))->addOption('filter', array(
  77. 'help' => __d('cake_console', '<pattern> Filter which tests to run.'),
  78. 'default' => false
  79. ))->addOption('group', array(
  80. 'help' => __d('cake_console', '<name> Only runs tests from the specified group(s).'),
  81. 'default' => false
  82. ))->addOption('exclude-group', array(
  83. 'help' => __d('cake_console', '<name> Exclude tests from the specified group(s).'),
  84. 'default' => false
  85. ))->addOption('list-groups', array(
  86. 'help' => __d('cake_console', 'List available test groups.'),
  87. 'boolean' => true
  88. ))->addOption('loader', array(
  89. 'help' => __d('cake_console', 'TestSuiteLoader implementation to use.'),
  90. 'default' => false
  91. ))->addOption('repeat', array(
  92. 'help' => __d('cake_console', '<times> Runs the test(s) repeatedly.'),
  93. 'default' => false
  94. ))->addOption('tap', array(
  95. 'help' => __d('cake_console', 'Report test execution progress in TAP format.'),
  96. 'boolean' => true
  97. ))->addOption('testdox', array(
  98. 'help' => __d('cake_console', 'Report test execution progress in TestDox format.'),
  99. 'default' => false,
  100. 'boolean' => true
  101. ))->addOption('no-colors', array(
  102. 'help' => __d('cake_console', 'Do not use colors in output.'),
  103. 'boolean' => true
  104. ))->addOption('stderr', array(
  105. 'help' => __d('cake_console', 'Write to STDERR instead of STDOUT.'),
  106. 'boolean' => true
  107. ))->addOption('stop-on-error', array(
  108. 'help' => __d('cake_console', 'Stop execution upon first error or failure.'),
  109. 'boolean' => true
  110. ))->addOption('stop-on-failure', array(
  111. 'help' => __d('cake_console', 'Stop execution upon first failure.'),
  112. 'boolean' => true
  113. ))->addOption('stop-on-skipped', array(
  114. 'help' => __d('cake_console', 'Stop execution upon first skipped test.'),
  115. 'boolean' => true
  116. ))->addOption('stop-on-incomplete', array(
  117. 'help' => __d('cake_console', 'Stop execution upon first incomplete test.'),
  118. 'boolean' => true
  119. ))->addOption('strict', array(
  120. 'help' => __d('cake_console', 'Mark a test as incomplete if no assertions are made.'),
  121. 'boolean' => true
  122. ))->addOption('wait', array(
  123. 'help' => __d('cake_console', 'Waits for a keystroke after each test.'),
  124. 'boolean' => true
  125. ))->addOption('process-isolation', array(
  126. 'help' => __d('cake_console', 'Run each test in a separate PHP process.'),
  127. 'boolean' => true
  128. ))->addOption('no-globals-backup', array(
  129. 'help' => __d('cake_console', 'Do not backup and restore $GLOBALS for each test.'),
  130. 'boolean' => true
  131. ))->addOption('static-backup', array(
  132. 'help' => __d('cake_console', 'Backup and restore static attributes for each test.'),
  133. 'boolean' => true
  134. ))->addOption('syntax-check', array(
  135. 'help' => __d('cake_console', 'Try to check source files for syntax errors.'),
  136. 'boolean' => true
  137. ))->addOption('bootstrap', array(
  138. 'help' => __d('cake_console', '<file> A "bootstrap" PHP file that is run before the tests.'),
  139. 'default' => false
  140. ))->addOption('configuration', array(
  141. 'help' => __d('cake_console', '<file> Read configuration from XML file.'),
  142. 'default' => false
  143. ))->addOption('no-configuration', array(
  144. 'help' => __d('cake_console', 'Ignore default configuration file (phpunit.xml).'),
  145. 'boolean' => true
  146. ))->addOption('include-path', array(
  147. 'help' => __d('cake_console', '<path(s)> Prepend PHP include_path with given path(s).'),
  148. 'default' => false
  149. ))->addOption('directive', array(
  150. 'help' => __d('cake_console', 'key[=value] Sets a php.ini value.'),
  151. 'default' => false
  152. ))->addOption('fixture', array(
  153. 'help' => __d('cake_console', 'Choose a custom fixture manager.'),
  154. ))->addOption('debug', array(
  155. 'help' => __d('cake_console', 'More verbose output.'),
  156. ));
  157. return $parser;
  158. }
  159. /**
  160. * Initialization method installs PHPUnit and loads all plugins
  161. *
  162. * @return void
  163. * @throws Exception
  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. if (strpos($this->args[0], '.php')) {
  188. $category = $this->_mapFileToCategory($this->args[0]);
  189. $params['case'] = $this->_mapFileToCase($this->args[0], $category);
  190. } else {
  191. $category = $this->args[0];
  192. if (isset($this->args[1])) {
  193. $params['case'] = $this->args[1];
  194. }
  195. }
  196. if ($category === 'core') {
  197. $params['core'] = true;
  198. } elseif ($category === 'app') {
  199. $params['app'] = true;
  200. } else {
  201. $params['plugin'] = $category;
  202. }
  203. return $params;
  204. }
  205. /**
  206. * Converts the options passed to the shell as options for the PHPUnit cli runner
  207. *
  208. * @return array Array of params for CakeTestDispatcher
  209. */
  210. protected function _runnerOptions() {
  211. $options = array();
  212. $params = $this->params;
  213. unset($params['help']);
  214. if (!empty($params['no-colors'])) {
  215. unset($params['no-colors'], $params['colors']);
  216. } else {
  217. $params['colors'] = true;
  218. }
  219. foreach ($params as $param => $value) {
  220. if ($value === false) {
  221. continue;
  222. }
  223. $options[] = '--' . $param;
  224. if (is_string($value)) {
  225. $options[] = $value;
  226. }
  227. }
  228. return $options;
  229. }
  230. /**
  231. * Main entry point to this shell
  232. *
  233. * @return void
  234. */
  235. public function main() {
  236. $this->out(__d('cake_console', 'CakePHP Test Shell'));
  237. $this->hr();
  238. $args = $this->_parseArgs();
  239. if (empty($args['case'])) {
  240. return $this->available();
  241. }
  242. $this->_run($args, $this->_runnerOptions());
  243. }
  244. /**
  245. * Runs the test case from $runnerArgs
  246. *
  247. * @param array $runnerArgs list of arguments as obtained from _parseArgs()
  248. * @param array $options list of options as constructed by _runnerOptions()
  249. * @return void
  250. */
  251. protected function _run($runnerArgs, $options = array()) {
  252. restore_error_handler();
  253. restore_error_handler();
  254. $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
  255. $testCli->run($options);
  256. }
  257. /**
  258. * Shows a list of available test cases and gives the option to run one of them
  259. *
  260. * @return void
  261. */
  262. public function available() {
  263. $params = $this->_parseArgs();
  264. $testCases = CakeTestLoader::generateTestList($params);
  265. $app = $params['app'];
  266. $plugin = $params['plugin'];
  267. $title = "Core Test Cases:";
  268. $category = 'core';
  269. if ($app) {
  270. $title = "App Test Cases:";
  271. $category = 'app';
  272. } elseif ($plugin) {
  273. $title = Inflector::humanize($plugin) . " Test Cases:";
  274. $category = $plugin;
  275. }
  276. if (empty($testCases)) {
  277. $this->out(__d('cake_console', "No test cases available \n\n"));
  278. return $this->out($this->OptionParser->help());
  279. }
  280. $this->out($title);
  281. $i = 1;
  282. $cases = array();
  283. foreach ($testCases as $testCase) {
  284. $case = str_replace('Test.php', '', $testCase);
  285. $this->out("[$i] $case");
  286. $cases[$i] = $case;
  287. $i++;
  288. }
  289. while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
  290. if (is_numeric($choice) && isset($cases[$choice])) {
  291. $this->args[0] = $category;
  292. $this->args[1] = $cases[$choice];
  293. $this->_run($this->_parseArgs(), $this->_runnerOptions());
  294. break;
  295. }
  296. if (is_string($choice) && in_array($choice, $cases)) {
  297. $this->args[0] = $category;
  298. $this->args[1] = $choice;
  299. $this->_run($this->_parseArgs(), $this->_runnerOptions());
  300. break;
  301. }
  302. if ($choice == 'q') {
  303. break;
  304. }
  305. }
  306. }
  307. /**
  308. * Find the test case for the passed file. The file could itself be a test.
  309. *
  310. * @param string $file
  311. * @param string $category
  312. * @param boolean $throwOnMissingFile
  313. * @access protected
  314. * @return array array(type, case)
  315. * @throws Exception
  316. */
  317. protected function _mapFileToCase($file, $category, $throwOnMissingFile = true) {
  318. if (!$category || (substr($file, -4) !== '.php')) {
  319. return false;
  320. }
  321. $_file = realpath($file);
  322. if ($_file) {
  323. $file = $_file;
  324. }
  325. $testFile = $testCase = null;
  326. if (preg_match('@Test[\\\/]@', $file)) {
  327. if (substr($file, -8) === 'Test.php') {
  328. $testCase = substr($file, 0, -8);
  329. $testCase = str_replace(DS, '/', $testCase);
  330. if ($testCase = preg_replace('@.*Test\/Case\/@', '', $testCase)) {
  331. if ($category === 'core') {
  332. $testCase = str_replace('lib/Cake', '', $testCase);
  333. }
  334. return $testCase;
  335. }
  336. throw new Exception(__d('cake_dev', 'Test case %s cannot be run via this shell', $testFile));
  337. }
  338. }
  339. $file = substr($file, 0, -4);
  340. if ($category === 'core') {
  341. $testCase = str_replace(DS, '/', $file);
  342. $testCase = preg_replace('@.*lib/Cake/@', '', $file);
  343. $testCase[0] = strtoupper($testCase[0]);
  344. $testFile = CAKE . 'Test/Case/' . $testCase . 'Test.php';
  345. if (!file_exists($testFile) && $throwOnMissingFile) {
  346. throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
  347. }
  348. return $testCase;
  349. }
  350. if ($category === 'app') {
  351. $testFile = str_replace(APP, APP . 'Test/Case/', $file) . 'Test.php';
  352. } else {
  353. $testFile = preg_replace(
  354. "@((?:plugins|Plugin)[\\/]{$category}[\\/])(.*)$@",
  355. '\1Test/Case/\2Test.php',
  356. $file
  357. );
  358. }
  359. if (!file_exists($testFile) && $throwOnMissingFile) {
  360. throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
  361. }
  362. $testCase = substr($testFile, 0, -8);
  363. $testCase = str_replace(DS, '/', $testCase);
  364. $testCase = preg_replace('@.*Test/Case/@', '', $testCase);
  365. return $testCase;
  366. }
  367. /**
  368. * For the given file, what category of test is it? returns app, core or the name of the plugin
  369. *
  370. * @param string $file
  371. * @access protected
  372. * @return string
  373. */
  374. protected function _mapFileToCategory($file) {
  375. $_file = realpath($file);
  376. if ($_file) {
  377. $file = $_file;
  378. }
  379. $file = str_replace(DS, '/', $file);
  380. if (strpos($file, 'lib/Cake/') !== false) {
  381. return 'core';
  382. } elseif (preg_match('@(?:plugins|Plugin)/([^/]*)@', $file, $match)) {
  383. return $match[1];
  384. }
  385. return 'app';
  386. }
  387. }