TestShell.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://book.cakephp.org/2.0/en/development/testing.html
  18. * @since CakePHP(tm) v 1.2.0.4433
  19. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  20. */
  21. App::uses('Shell', 'Console');
  22. App::uses('CakeTestSuiteDispatcher', 'TestSuite');
  23. App::uses('CakeTestSuiteCommand', 'TestSuite');
  24. App::uses('CakeTestLoader', 'TestSuite');
  25. /**
  26. * Provides a CakePHP wrapper around PHPUnit.
  27. * Adds in CakePHP's fixtures and gives access to plugin, app and core test cases
  28. *
  29. * @package Cake.Console.Command
  30. */
  31. class TestShell extends Shell {
  32. /**
  33. * Dispatcher object for the run.
  34. *
  35. * @var CakeTestDispatcher
  36. */
  37. protected $_dispatcher = null;
  38. /**
  39. * get the option parser for the test suite.
  40. *
  41. * @return void
  42. */
  43. public function getOptionParser() {
  44. $parser = new ConsoleOptionParser($this->name);
  45. $parser->description(array(
  46. __d('cake_console', 'The CakePHP Testsuite allows you to run test cases from the command line'),
  47. ))->addArgument('category', array(
  48. 'help' => __d('cake_console', 'The category for the test, or test file, to test.'),
  49. 'required' => false,
  50. ))->addArgument('file', array(
  51. 'help' => __d('cake_console', 'The path to the file, or test file, to test.'),
  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 JSON 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. * @throws Exception
  165. */
  166. public function initialize() {
  167. $this->_dispatcher = new CakeTestSuiteDispatcher();
  168. $success = $this->_dispatcher->loadTestFramework();
  169. if (!$success) {
  170. throw new Exception(__d('cake_dev', 'Please install PHPUnit framework <info>(http://www.phpunit.de)</info>'));
  171. }
  172. }
  173. /**
  174. * Parse the CLI options into an array CakeTestDispatcher can use.
  175. *
  176. * @return array Array of params for CakeTestDispatcher
  177. */
  178. protected function _parseArgs() {
  179. if (empty($this->args)) {
  180. return;
  181. }
  182. $params = array(
  183. 'core' => false,
  184. 'app' => false,
  185. 'plugin' => null,
  186. 'output' => 'text',
  187. );
  188. if (strpos($this->args[0], '.php')) {
  189. $category = $this->_mapFileToCategory($this->args[0]);
  190. $params['case'] = $this->_mapFileToCase($this->args[0], $category);
  191. } else {
  192. $category = $this->args[0];
  193. if (isset($this->args[1])) {
  194. $params['case'] = $this->args[1];
  195. }
  196. }
  197. if ($category === 'core') {
  198. $params['core'] = true;
  199. } elseif ($category === 'app') {
  200. $params['app'] = true;
  201. } else {
  202. $params['plugin'] = $category;
  203. }
  204. return $params;
  205. }
  206. /**
  207. * Converts the options passed to the shell as options for the PHPUnit cli runner
  208. *
  209. * @return array Array of params for CakeTestDispatcher
  210. */
  211. protected function _runnerOptions() {
  212. $options = array();
  213. $params = $this->params;
  214. unset($params['help']);
  215. if (!empty($params['no-colors'])) {
  216. unset($params['no-colors'], $params['colors']);
  217. } else {
  218. $params['colors'] = true;
  219. }
  220. foreach ($params as $param => $value) {
  221. if ($value === false) {
  222. continue;
  223. }
  224. $options[] = '--' . $param;
  225. if (is_string($value)) {
  226. $options[] = $value;
  227. }
  228. }
  229. return $options;
  230. }
  231. /**
  232. * Main entry point to this shell
  233. *
  234. * @return void
  235. */
  236. public function main() {
  237. $this->out(__d('cake_console', 'CakePHP Test Shell'));
  238. $this->hr();
  239. $args = $this->_parseArgs();
  240. if (empty($args['case'])) {
  241. return $this->available();
  242. }
  243. $this->_run($args, $this->_runnerOptions());
  244. }
  245. /**
  246. * Runs the test case from $runnerArgs
  247. *
  248. * @param array $runnerArgs list of arguments as obtained from _parseArgs()
  249. * @param array $options list of options as constructed by _runnerOptions()
  250. * @return void
  251. */
  252. protected function _run($runnerArgs, $options = array()) {
  253. restore_error_handler();
  254. restore_error_handler();
  255. $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
  256. $testCli->run($options);
  257. }
  258. /**
  259. * Shows a list of available test cases and gives the option to run one of them
  260. *
  261. * @return void
  262. */
  263. public function available() {
  264. $params = $this->_parseArgs();
  265. $testCases = CakeTestLoader::generateTestList($params);
  266. $app = $params['app'];
  267. $plugin = $params['plugin'];
  268. $title = "Core Test Cases:";
  269. $category = 'core';
  270. if ($app) {
  271. $title = "App Test Cases:";
  272. $category = 'app';
  273. } elseif ($plugin) {
  274. $title = Inflector::humanize($plugin) . " Test Cases:";
  275. $category = $plugin;
  276. }
  277. if (empty($testCases)) {
  278. $this->out(__d('cake_console', "No test cases available \n\n"));
  279. return $this->out($this->OptionParser->help());
  280. }
  281. $this->out($title);
  282. $i = 1;
  283. $cases = array();
  284. foreach ($testCases as $testCase) {
  285. $case = str_replace('Test.php', '', $testCase);
  286. $this->out("[$i] $case");
  287. $cases[$i] = $case;
  288. $i++;
  289. }
  290. while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
  291. if (is_numeric($choice) && isset($cases[$choice])) {
  292. $this->args[0] = $category;
  293. $this->args[1] = $cases[$choice];
  294. $this->_run($this->_parseArgs(), $this->_runnerOptions());
  295. break;
  296. }
  297. if (is_string($choice) && in_array($choice, $cases)) {
  298. $this->args[0] = $category;
  299. $this->args[1] = $choice;
  300. $this->_run($this->_parseArgs(), $this->_runnerOptions());
  301. break;
  302. }
  303. if ($choice === 'q') {
  304. break;
  305. }
  306. }
  307. }
  308. /**
  309. * Find the test case for the passed file. The file could itself be a test.
  310. *
  311. * @param string $file
  312. * @param string $category
  313. * @param boolean $throwOnMissingFile
  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. * @return string
  372. */
  373. protected function _mapFileToCategory($file) {
  374. $_file = realpath($file);
  375. if ($_file) {
  376. $file = $_file;
  377. }
  378. $file = str_replace(DS, '/', $file);
  379. if (strpos($file, 'lib/Cake/') !== false) {
  380. return 'core';
  381. } elseif (preg_match('@(?:plugins|Plugin)/([^/]*)@', $file, $match)) {
  382. return $match[1];
  383. }
  384. return 'app';
  385. }
  386. }