TestShell.php 13 KB

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