PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/kerphi/contrib/pfcInstaller2/PEAR/Command/Test.php

https://bitbucket.org/shashwat_dinasource/bitscentral
PHP | 275 lines | 214 code | 11 blank | 50 comment | 44 complexity | a537a76b067ccdac893469fb95c1c9cc MD5 | raw file
  1. <?php
  2. /**
  3. * PEAR_Command_Test (run-tests)
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.0 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category pear
  14. * @package PEAR
  15. * @author Stig Bakken <ssb@php.net>
  16. * @author Martin Jansen <mj@php.net>
  17. * @author Greg Beaver <cellog@php.net>
  18. * @copyright 1997-2006 The PHP Group
  19. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  20. * @version CVS: $Id: Test.php,v 1.9 2006/02/03 22:28:08 cellog Exp $
  21. * @link http://pear.php.net/package/PEAR
  22. * @since File available since Release 0.1
  23. */
  24. /**
  25. * base class
  26. */
  27. require_once 'PEAR/Command/Common.php';
  28. /**
  29. * PEAR commands for login/logout
  30. *
  31. * @category pear
  32. * @package PEAR
  33. * @author Stig Bakken <ssb@php.net>
  34. * @author Martin Jansen <mj@php.net>
  35. * @author Greg Beaver <cellog@php.net>
  36. * @copyright 1997-2006 The PHP Group
  37. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  38. * @version Release: 1.4.11
  39. * @link http://pear.php.net/package/PEAR
  40. * @since Class available since Release 0.1
  41. */
  42. class PEAR_Command_Test extends PEAR_Command_Common
  43. {
  44. // {{{ properties
  45. var $commands = array(
  46. 'run-tests' => array(
  47. 'summary' => 'Run Regression Tests',
  48. 'function' => 'doRunTests',
  49. 'shortcut' => 'rt',
  50. 'options' => array(
  51. 'recur' => array(
  52. 'shortopt' => 'r',
  53. 'doc' => 'Run tests in child directories, recursively. 4 dirs deep maximum',
  54. ),
  55. 'ini' => array(
  56. 'shortopt' => 'i',
  57. 'doc' => 'actual string of settings to pass to php in format " -d setting=blah"',
  58. 'arg' => 'SETTINGS'
  59. ),
  60. 'realtimelog' => array(
  61. 'shortopt' => 'l',
  62. 'doc' => 'Log test runs/results as they are run',
  63. ),
  64. 'quiet' => array(
  65. 'shortopt' => 'q',
  66. 'doc' => 'Only display detail for failed tests',
  67. ),
  68. 'simple' => array(
  69. 'shortopt' => 's',
  70. 'doc' => 'Display simple output for all tests',
  71. ),
  72. 'package' => array(
  73. 'shortopt' => 'p',
  74. 'doc' => 'Treat parameters as installed packages from which to run tests',
  75. ),
  76. 'phpunit' => array(
  77. 'shortopt' => 'u',
  78. 'doc' => 'Search parameters for AllTests.php, and use that to run phpunit-based tests',
  79. ),
  80. ),
  81. 'doc' => '[testfile|dir ...]
  82. Run regression tests with PHP\'s regression testing script (run-tests.php).',
  83. ),
  84. );
  85. var $output;
  86. // }}}
  87. // {{{ constructor
  88. /**
  89. * PEAR_Command_Test constructor.
  90. *
  91. * @access public
  92. */
  93. function PEAR_Command_Test(&$ui, &$config)
  94. {
  95. parent::PEAR_Command_Common($ui, $config);
  96. }
  97. // }}}
  98. // {{{ doRunTests()
  99. function doRunTests($command, $options, $params)
  100. {
  101. require_once 'PEAR/Common.php';
  102. require_once 'PEAR/RunTest.php';
  103. require_once 'System.php';
  104. $log = new PEAR_Common;
  105. $log->ui = &$this->ui; // slightly hacky, but it will work
  106. $run = new PEAR_RunTest($log, $options);
  107. $tests = array();
  108. if (isset($options['recur'])) {
  109. $depth = 4;
  110. } else {
  111. $depth = 1;
  112. }
  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'])) {
  136. if (!preg_match('/AllTests\.php$/i', $name)) {
  137. continue;
  138. }
  139. } else {
  140. if (!preg_match('/\.phpt$/', $name)) {
  141. continue;
  142. }
  143. }
  144. $params[] = $atts['installed_as'];
  145. }
  146. }
  147. }
  148. foreach ($params as $p) {
  149. if (is_dir($p)) {
  150. if (isset($options['phpunit'])) {
  151. $dir = System::find(array($p, '-type', 'f',
  152. '-maxdepth', $depth,
  153. '-name', 'AllTests.php'));
  154. } else {
  155. $dir = System::find(array($p, '-type', 'f',
  156. '-maxdepth', $depth,
  157. '-name', '*.phpt'));
  158. }
  159. $tests = array_merge($tests, $dir);
  160. } else {
  161. if (isset($options['phpunit'])) {
  162. if (!preg_match('/AllTests\.php$/i', $p)) {
  163. continue;
  164. }
  165. $tests[] = $p;
  166. } else {
  167. if (!@file_exists($p)) {
  168. if (!preg_match('/\.phpt$/', $p)) {
  169. $p .= '.phpt';
  170. }
  171. $dir = System::find(array(dirname($p), '-type', 'f',
  172. '-maxdepth', $depth,
  173. '-name', $p));
  174. $tests = array_merge($tests, $dir);
  175. } else {
  176. $tests[] = $p;
  177. }
  178. }
  179. }
  180. }
  181. $ini_settings = '';
  182. if (isset($options['ini'])) {
  183. $ini_settings .= $options['ini'];
  184. }
  185. if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) {
  186. $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}";
  187. }
  188. if ($ini_settings) {
  189. $this->ui->outputData('Using INI settings: "' . $ini_settings . '"');
  190. }
  191. $skipped = $passed = $failed = array();
  192. $this->ui->outputData('Running ' . count($tests) . ' tests', $command);
  193. $start = time();
  194. if (isset($options['realtimelog'])) {
  195. @unlink('run-tests.log');
  196. }
  197. foreach ($tests as $t) {
  198. if (isset($options['realtimelog'])) {
  199. $fp = @fopen('run-tests.log', 'a');
  200. if ($fp) {
  201. fwrite($fp, "Running test $t...");
  202. fclose($fp);
  203. }
  204. }
  205. PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
  206. $result = $run->run($t, $ini_settings);
  207. PEAR::staticPopErrorHandling();
  208. if (PEAR::isError($result)) {
  209. $this->ui->log(0, $result->getMessage());
  210. continue;
  211. }
  212. if (isset($options['realtimelog'])) {
  213. $fp = @fopen('run-tests.log', 'a');
  214. if ($fp) {
  215. fwrite($fp, "$result\n");
  216. fclose($fp);
  217. }
  218. }
  219. if ($result == 'FAILED') {
  220. $failed[] = $t;
  221. }
  222. if ($result == 'PASSED') {
  223. $passed[] = $t;
  224. }
  225. if ($result == 'SKIPPED') {
  226. $skipped[] = $t;
  227. }
  228. }
  229. $total = date('i:s', time() - $start);
  230. if (count($failed)) {
  231. $output = "TOTAL TIME: $total\n";
  232. $output .= count($passed) . " PASSED TESTS\n";
  233. $output .= count($skipped) . " SKIPPED TESTS\n";
  234. $output .= count($failed) . " FAILED TESTS:\n";
  235. foreach ($failed as $failure) {
  236. $output .= $failure . "\n";
  237. }
  238. if (isset($options['realtimelog'])) {
  239. $fp = @fopen('run-tests.log', 'a');
  240. } else {
  241. $fp = @fopen('run-tests.log', 'w');
  242. }
  243. if ($fp) {
  244. fwrite($fp, $output, strlen($output));
  245. fclose($fp);
  246. $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command);
  247. }
  248. } elseif (@file_exists('run-tests.log') && !@is_dir('run-tests.log')) {
  249. @unlink('run-tests.log');
  250. }
  251. $this->ui->outputData('TOTAL TIME: ' . $total);
  252. $this->ui->outputData(count($passed) . ' PASSED TESTS', $command);
  253. $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command);
  254. if (count($failed)) {
  255. $this->ui->outputData(count($failed) . ' FAILED TESTS:', $command);
  256. foreach ($failed as $failure) {
  257. $this->ui->outputData($failure, $command);
  258. }
  259. }
  260. return true;
  261. }
  262. // }}}
  263. }
  264. ?>