/lib/Cake/Console/Command/TestsuiteShell.php

https://bitbucket.org/udeshika/fake_twitter · PHP · 329 lines · 236 code · 26 blank · 67 comment · 21 complexity · 6968f3cd887dcfe8d8cc76d7d6d7a633 MD5 · raw file

  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('AppShell', 'Console/Command');
  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 AppShell {
  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', 'Enable full output of testsuite. (supported in PHPUnit 3.6.0 and greater)'),
  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. $sucess = $this->_dispatcher->loadTestFramework();
  169. if (!$sucess) {
  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. $category = $this->args[0];
  189. if ($category == 'core') {
  190. $params['core'] = true;
  191. } elseif ($category == 'app') {
  192. $params['app'] = true;
  193. } elseif ($category != 'core') {
  194. $params['plugin'] = $category;
  195. }
  196. if (isset($this->args[1])) {
  197. $params['case'] = $this->args[1];
  198. }
  199. return $params;
  200. }
  201. /**
  202. * Converts the options passed to the shell as options for the PHPUnit cli runner
  203. *
  204. * @return array Array of params for CakeTestDispatcher
  205. */
  206. protected function _runnerOptions() {
  207. $options = array();
  208. $params = $this->params;
  209. unset($params['help']);
  210. if (!empty($params['no-colors'])) {
  211. unset($params['no-colors'], $params['colors']);
  212. } else {
  213. $params['colors'] = true;
  214. }
  215. foreach ($params as $param => $value) {
  216. if ($value === false) {
  217. continue;
  218. }
  219. $options[] = '--' . $param;
  220. if (is_string($value)) {
  221. $options[] = $value;
  222. }
  223. }
  224. return $options;
  225. }
  226. /**
  227. * Main entry point to this shell
  228. *
  229. * @return void
  230. */
  231. public function main() {
  232. $this->out(__d('cake_console', 'CakePHP Test Shell'));
  233. $this->hr();
  234. $args = $this->_parseArgs();
  235. if (empty($args['case'])) {
  236. return $this->available();
  237. }
  238. $this->_run($args, $this->_runnerOptions());
  239. }
  240. /**
  241. * Runs the test case from $runnerArgs
  242. *
  243. * @param array $runnerArgs list of arguments as obtained from _parseArgs()
  244. * @param array $options list of options as constructed by _runnerOptions()
  245. * @return void
  246. */
  247. protected function _run($runnerArgs, $options = array()) {
  248. restore_error_handler();
  249. restore_error_handler();
  250. $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs);
  251. $testCli->run($options);
  252. }
  253. /**
  254. * Shows a list of available test cases and gives the option to run one of them
  255. *
  256. * @return void
  257. */
  258. public function available() {
  259. $params = $this->_parseArgs();
  260. $testCases = CakeTestLoader::generateTestList($params);
  261. $app = $params['app'];
  262. $plugin = $params['plugin'];
  263. $title = "Core Test Cases:";
  264. $category = 'core';
  265. if ($app) {
  266. $title = "App Test Cases:";
  267. $category = 'app';
  268. } elseif ($plugin) {
  269. $title = Inflector::humanize($plugin) . " Test Cases:";
  270. $category = $plugin;
  271. }
  272. if (empty($testCases)) {
  273. $this->out(__d('cake_console', "No test cases available \n\n"));
  274. return $this->out($this->OptionParser->help());
  275. }
  276. $this->out($title);
  277. $i = 1;
  278. $cases = array();
  279. foreach ($testCases as $testCaseFile => $testCase) {
  280. $case = str_replace('Test.php', '', $testCase);
  281. $this->out("[$i] $case");
  282. $cases[$i] = $case;
  283. $i++;
  284. }
  285. while ($choice = $this->in(__d('cake_console', 'What test case would you like to run?'), null, 'q')) {
  286. if (is_numeric($choice) && isset($cases[$choice])) {
  287. $this->args[0] = $category;
  288. $this->args[1] = $cases[$choice];
  289. $this->_run($this->_parseArgs(), $this->_runnerOptions());
  290. break;
  291. }
  292. if (is_string($choice) && in_array($choice, $cases)) {
  293. $this->args[0] = $category;
  294. $this->args[1] = $choice;
  295. $this->_run($this->_parseArgs(), $this->_runnerOptions());
  296. break;
  297. }
  298. if ($choice == 'q') {
  299. break;
  300. }
  301. }
  302. }
  303. }