PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/php/PEAR/Command/Test.php

https://bitbucket.org/adarshj/convenient_website
PHP | 337 lines | 262 code | 35 blank | 40 comment | 55 complexity | 8794af46ada0364362db0e131607e6d1 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PEAR_Command_Test (run-tests)
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category pear
  8. * @package PEAR
  9. * @author Stig Bakken <ssb@php.net>
  10. * @author Martin Jansen <mj@php.net>
  11. * @author Greg Beaver <cellog@php.net>
  12. * @copyright 1997-2009 The Authors
  13. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  14. * @version CVS: $Id: Test.php 313023 2011-07-06 19:17:11Z dufuz $
  15. * @link http://pear.php.net/package/PEAR
  16. * @since File available since Release 0.1
  17. */
  18. /**
  19. * base class
  20. */
  21. require_once 'PEAR/Command/Common.php';
  22. /**
  23. * PEAR commands for login/logout
  24. *
  25. * @category pear
  26. * @package PEAR
  27. * @author Stig Bakken <ssb@php.net>
  28. * @author Martin Jansen <mj@php.net>
  29. * @author Greg Beaver <cellog@php.net>
  30. * @copyright 1997-2009 The Authors
  31. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  32. * @version Release: 1.9.4
  33. * @link http://pear.php.net/package/PEAR
  34. * @since Class available since Release 0.1
  35. */
  36. class PEAR_Command_Test extends PEAR_Command_Common
  37. {
  38. var $commands = array(
  39. 'run-tests' => array(
  40. 'summary' => 'Run Regression Tests',
  41. 'function' => 'doRunTests',
  42. 'shortcut' => 'rt',
  43. 'options' => array(
  44. 'recur' => array(
  45. 'shortopt' => 'r',
  46. 'doc' => 'Run tests in child directories, recursively. 4 dirs deep maximum',
  47. ),
  48. 'ini' => array(
  49. 'shortopt' => 'i',
  50. 'doc' => 'actual string of settings to pass to php in format " -d setting=blah"',
  51. 'arg' => 'SETTINGS'
  52. ),
  53. 'realtimelog' => array(
  54. 'shortopt' => 'l',
  55. 'doc' => 'Log test runs/results as they are run',
  56. ),
  57. 'quiet' => array(
  58. 'shortopt' => 'q',
  59. 'doc' => 'Only display detail for failed tests',
  60. ),
  61. 'simple' => array(
  62. 'shortopt' => 's',
  63. 'doc' => 'Display simple output for all tests',
  64. ),
  65. 'package' => array(
  66. 'shortopt' => 'p',
  67. 'doc' => 'Treat parameters as installed packages from which to run tests',
  68. ),
  69. 'phpunit' => array(
  70. 'shortopt' => 'u',
  71. 'doc' => 'Search parameters for AllTests.php, and use that to run phpunit-based tests
  72. If none is found, all .phpt tests will be tried instead.',
  73. ),
  74. 'tapoutput' => array(
  75. 'shortopt' => 't',
  76. 'doc' => 'Output run-tests.log in TAP-compliant format',
  77. ),
  78. 'cgi' => array(
  79. 'shortopt' => 'c',
  80. 'doc' => 'CGI php executable (needed for tests with POST/GET section)',
  81. 'arg' => 'PHPCGI',
  82. ),
  83. 'coverage' => array(
  84. 'shortopt' => 'x',
  85. 'doc' => 'Generate a code coverage report (requires Xdebug 2.0.0+)',
  86. ),
  87. ),
  88. 'doc' => '[testfile|dir ...]
  89. Run regression tests with PHP\'s regression testing script (run-tests.php).',
  90. ),
  91. );
  92. var $output;
  93. /**
  94. * PEAR_Command_Test constructor.
  95. *
  96. * @access public
  97. */
  98. function PEAR_Command_Test(&$ui, &$config)
  99. {
  100. parent::PEAR_Command_Common($ui, $config);
  101. }
  102. function doRunTests($command, $options, $params)
  103. {
  104. if (isset($options['phpunit']) && isset($options['tapoutput'])) {
  105. return $this->raiseError('ERROR: cannot use both --phpunit and --tapoutput at the same time');
  106. }
  107. require_once 'PEAR/Common.php';
  108. require_once 'System.php';
  109. $log = new PEAR_Common;
  110. $log->ui = &$this->ui; // slightly hacky, but it will work
  111. $tests = array();
  112. $depth = isset($options['recur']) ? 14 : 1;
  113. if (!count($params)) {
  114. $params[] = '.';
  115. }
  116. if (isset($options['package'])) {
  117. $oldparams = $params;
  118. $params = array();
  119. $reg = &$this->config->getRegistry();
  120. foreach ($oldparams as $param) {
  121. $pname = $reg->parsePackageName($param, $this->config->get('default_channel'));
  122. if (PEAR::isError($pname)) {
  123. return $this->raiseError($pname);
  124. }
  125. $package = &$reg->getPackage($pname['package'], $pname['channel']);
  126. if (!$package) {
  127. return PEAR::raiseError('Unknown package "' .
  128. $reg->parsedPackageNameToString($pname) . '"');
  129. }
  130. $filelist = $package->getFilelist();
  131. foreach ($filelist as $name => $atts) {
  132. if (isset($atts['role']) && $atts['role'] != 'test') {
  133. continue;
  134. }
  135. if (isset($options['phpunit']) && preg_match('/AllTests\.php\\z/i', $name)) {
  136. $params[] = $atts['installed_as'];
  137. continue;
  138. } elseif (!preg_match('/\.phpt\\z/', $name)) {
  139. continue;
  140. }
  141. $params[] = $atts['installed_as'];
  142. }
  143. }
  144. }
  145. foreach ($params as $p) {
  146. if (is_dir($p)) {
  147. if (isset($options['phpunit'])) {
  148. $dir = System::find(array($p, '-type', 'f',
  149. '-maxdepth', $depth,
  150. '-name', 'AllTests.php'));
  151. if (count($dir)) {
  152. foreach ($dir as $p) {
  153. $p = realpath($p);
  154. if (!count($tests) ||
  155. (count($tests) && strlen($p) < strlen($tests[0]))) {
  156. // this is in a higher-level directory, use this one instead.
  157. $tests = array($p);
  158. }
  159. }
  160. }
  161. continue;
  162. }
  163. $args = array($p, '-type', 'f', '-name', '*.phpt');
  164. } else {
  165. if (isset($options['phpunit'])) {
  166. if (preg_match('/AllTests\.php\\z/i', $p)) {
  167. $p = realpath($p);
  168. if (!count($tests) ||
  169. (count($tests) && strlen($p) < strlen($tests[0]))) {
  170. // this is in a higher-level directory, use this one instead.
  171. $tests = array($p);
  172. }
  173. }
  174. continue;
  175. }
  176. if (file_exists($p) && preg_match('/\.phpt$/', $p)) {
  177. $tests[] = $p;
  178. continue;
  179. }
  180. if (!preg_match('/\.phpt\\z/', $p)) {
  181. $p .= '.phpt';
  182. }
  183. $args = array(dirname($p), '-type', 'f', '-name', $p);
  184. }
  185. if (!isset($options['recur'])) {
  186. $args[] = '-maxdepth';
  187. $args[] = 1;
  188. }
  189. $dir = System::find($args);
  190. $tests = array_merge($tests, $dir);
  191. }
  192. $ini_settings = '';
  193. if (isset($options['ini'])) {
  194. $ini_settings .= $options['ini'];
  195. }
  196. if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) {
  197. $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}";
  198. }
  199. if ($ini_settings) {
  200. $this->ui->outputData('Using INI settings: "' . $ini_settings . '"');
  201. }
  202. $skipped = $passed = $failed = array();
  203. $tests_count = count($tests);
  204. $this->ui->outputData('Running ' . $tests_count . ' tests', $command);
  205. $start = time();
  206. if (isset($options['realtimelog']) && file_exists('run-tests.log')) {
  207. unlink('run-tests.log');
  208. }
  209. if (isset($options['tapoutput'])) {
  210. $tap = '1..' . $tests_count . "\n";
  211. }
  212. require_once 'PEAR/RunTest.php';
  213. $run = new PEAR_RunTest($log, $options);
  214. $run->tests_count = $tests_count;
  215. if (isset($options['coverage']) && extension_loaded('xdebug')){
  216. $run->xdebug_loaded = true;
  217. } else {
  218. $run->xdebug_loaded = false;
  219. }
  220. $j = $i = 1;
  221. foreach ($tests as $t) {
  222. if (isset($options['realtimelog'])) {
  223. $fp = @fopen('run-tests.log', 'a');
  224. if ($fp) {
  225. fwrite($fp, "Running test [$i / $tests_count] $t...");
  226. fclose($fp);
  227. }
  228. }
  229. PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
  230. if (isset($options['phpunit'])) {
  231. $result = $run->runPHPUnit($t, $ini_settings);
  232. } else {
  233. $result = $run->run($t, $ini_settings, $j);
  234. }
  235. PEAR::staticPopErrorHandling();
  236. if (PEAR::isError($result)) {
  237. $this->ui->log($result->getMessage());
  238. continue;
  239. }
  240. if (isset($options['tapoutput'])) {
  241. $tap .= $result[0] . ' ' . $i . $result[1] . "\n";
  242. continue;
  243. }
  244. if (isset($options['realtimelog'])) {
  245. $fp = @fopen('run-tests.log', 'a');
  246. if ($fp) {
  247. fwrite($fp, "$result\n");
  248. fclose($fp);
  249. }
  250. }
  251. if ($result == 'FAILED') {
  252. $failed[] = $t;
  253. }
  254. if ($result == 'PASSED') {
  255. $passed[] = $t;
  256. }
  257. if ($result == 'SKIPPED') {
  258. $skipped[] = $t;
  259. }
  260. $j++;
  261. }
  262. $total = date('i:s', time() - $start);
  263. if (isset($options['tapoutput'])) {
  264. $fp = @fopen('run-tests.log', 'w');
  265. if ($fp) {
  266. fwrite($fp, $tap, strlen($tap));
  267. fclose($fp);
  268. $this->ui->outputData('wrote TAP-format log to "' .realpath('run-tests.log') .
  269. '"', $command);
  270. }
  271. } else {
  272. if (count($failed)) {
  273. $output = "TOTAL TIME: $total\n";
  274. $output .= count($passed) . " PASSED TESTS\n";
  275. $output .= count($skipped) . " SKIPPED TESTS\n";
  276. $output .= count($failed) . " FAILED TESTS:\n";
  277. foreach ($failed as $failure) {
  278. $output .= $failure . "\n";
  279. }
  280. $mode = isset($options['realtimelog']) ? 'a' : 'w';
  281. $fp = @fopen('run-tests.log', $mode);
  282. if ($fp) {
  283. fwrite($fp, $output, strlen($output));
  284. fclose($fp);
  285. $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command);
  286. }
  287. } elseif (file_exists('run-tests.log') && !is_dir('run-tests.log')) {
  288. @unlink('run-tests.log');
  289. }
  290. }
  291. $this->ui->outputData('TOTAL TIME: ' . $total);
  292. $this->ui->outputData(count($passed) . ' PASSED TESTS', $command);
  293. $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command);
  294. if (count($failed)) {
  295. $this->ui->outputData(count($failed) . ' FAILED TESTS:', $command);
  296. foreach ($failed as $failure) {
  297. $this->ui->outputData($failure, $command);
  298. }
  299. }
  300. return true;
  301. }
  302. }