PageRenderTime 87ms CodeModel.GetById 32ms RepoModel.GetById 2ms app.codeStats 0ms

/tr/lib/spikephpcoverage/src/cli/driver.php

https://bitbucket.org/idler/mmp/
PHP | 264 lines | 187 code | 36 blank | 41 comment | 36 complexity | 1934cd7bc57d219f4d790b0273776095 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * $Id: license.txt 13981 2005-03-16 08:09:28Z eespino $
  4. *
  5. * Copyright(c) 2004-2005, SpikeSource Inc. All Rights Reserved.
  6. * Licensed under the Open Source License version 2.1
  7. * (See http://www.spikesource.com/license.html)
  8. */
  9. ?>
  10. <?php
  11. /**
  12. * This driver file can be used to initialize and generate PHPCoverage
  13. * report when PHPCoverage is used with a non-PHP test tool - like HttpUnit
  14. * It can, of course, be used for PHP test tools like SimpleTest and PHPUnit
  15. *
  16. * Notes:
  17. * * Option parsing courtesy of "daevid at daevid dot com" (http://php.planetmirror.com/manual/en/function.getopt.php)
  18. * * Contributed by Ed Espino <eespino@spikesource.com>
  19. */
  20. if(!defined("__PHPCOVERAGE_HOME")) {
  21. define("__PHPCOVERAGE_HOME", dirname(dirname(__FILE__)));
  22. }
  23. require_once __PHPCOVERAGE_HOME . "/conf/phpcoverage.conf.php";
  24. require_once __PHPCOVERAGE_HOME . "/util/Utility.php";
  25. // ######################################################################
  26. // ######################################################################
  27. function usage() {
  28. global $util;
  29. echo "Usage: " . $_SERVER['argv'][0] . " <options>\n";
  30. echo "\n";
  31. echo " Options: \n";
  32. echo " --phpcoverage-home <path> OR -p <path> Path to PHPCoverage home (defaults to PHPCOVERAGE_HOME environment property)\n";
  33. echo " --init Initialize PHPCoverage Reporting\n";
  34. echo " --report Generate PHPCoverage Reports\n";
  35. echo " --cleanup Remove existing PHPCoverage data\n";
  36. echo " init options \n";
  37. echo " --cov-url <url> Specify application default url\n";
  38. echo " --tmp-dir <path> Specify tmp directory location (Defaults to '" . $util->getTmpDir() . "')\n";
  39. echo " --cov-file-name <name> Specify coverage data file name (Defaults to 'phpcoverage.data.xml')\n";
  40. echo " report options \n";
  41. echo " --cov-data-file <path> Coverage data file path [use this instead of --cov-url for a local file path]\n";
  42. echo " --report-name <name> Report name\n";
  43. echo " --report-dir <path> Report directory path (Defaults to 'report')\n";
  44. echo " --appbase-path <path> Application base path (Defaults to PHPCOVERAGE_APPBASE_PATH if specified on the command line)\n";
  45. echo " --include-paths <path1,path2,...> Comma-separated paths to include in code coverage report. (Includes appbase-path by default)\n";
  46. echo " --exclude-paths <path1,path2,...> Comma-separated paths to exclude from code coverage report.\n";
  47. echo " --print-summary Print coverage report summary to console.\n";
  48. echo " other options \n";
  49. echo " --verbose OR -v Print verbose information\n";
  50. echo " --help OR -h Display this usage information\n";
  51. exit;
  52. }
  53. //
  54. // Setup command line argument processing
  55. //
  56. $OPTION["p"] = false;
  57. $OPTION['verbose'] = false;
  58. $OPTION['init'] = false;
  59. $OPTION['report'] = false;
  60. $OPTION['cleanup'] = false;
  61. $OPTION['cov-url'] = false;
  62. $OPTION['report-name'] = false;
  63. $OPTION['report-dir'] = false;
  64. $OPTION['tmp-dir'] = false;
  65. $OPTION['cov-file-name'] = false;
  66. $OPTION['cov-data-file'] = false;
  67. $OPTION['appbase-path'] = false;
  68. //
  69. // loop through our arguments and see what the user selected
  70. //
  71. for ($i = 1; $i < $_SERVER["argc"]; $i++) {
  72. switch($_SERVER["argv"][$i]) {
  73. case "--phpcoverage-home":
  74. case "-p":
  75. $OPTION['p'] = $_SERVER['argv'][++$i];
  76. break;
  77. case "-v":
  78. case "--verbose":
  79. $OPTION['verbose'] = true;
  80. break;
  81. case "--init":
  82. $OPTION['init'] = true;
  83. break;
  84. case "--report":
  85. $OPTION['report'] = true;
  86. break;
  87. case "--cleanup":
  88. $OPTION['cleanup'] = true;
  89. break;
  90. case "--cov-url":
  91. $OPTION['cov-url'] = $_SERVER['argv'][++$i] . "/" . "phpcoverage.remote.top.inc.php";
  92. break;
  93. case "--tmp-dir":
  94. $OPTION['tmp-dir'] = $_SERVER['argv'][++$i];
  95. break;
  96. case "--cov-file-name":
  97. $OPTION['cov-file-name'] = $_SERVER['argv'][++$i];
  98. break;
  99. case "--cov-data-file":
  100. $OPTION['cov-data-file'] = $_SERVER['argv'][++$i];
  101. break;
  102. case "--report-name":
  103. $OPTION['report-name'] = $_SERVER['argv'][++$i];
  104. break;
  105. case "--report-dir":
  106. $OPTION['report-dir'] = $_SERVER['argv'][++$i];
  107. break;
  108. case "--appbase-path":
  109. $OPTION['appbase-path'] = $_SERVER['argv'][++$i];
  110. break;
  111. case "--include-paths":
  112. $OPTION['include-paths'] = $_SERVER['argv'][++$i];
  113. break;
  114. case "--exclude-paths":
  115. $OPTION['exclude-paths'] = $_SERVER['argv'][++$i];
  116. break;
  117. case "--print-summary":
  118. $OPTION['print-summary'] = true;
  119. break;
  120. case "--help":
  121. case "-h":
  122. usage();
  123. break;
  124. }
  125. }
  126. if($OPTION['p'] == false) {
  127. $OPTION['p'] = __PHPCOVERAGE_HOME;
  128. if(empty($OPTION['p']) || !is_dir($OPTION['p'])) {
  129. die("PHPCOVERAGE_HOME does not exist. [" . $OPTION['p'] . "]");
  130. }
  131. }
  132. putenv("PHPCOVERAGE_HOME=" . $OPTION['p']);
  133. require_once $OPTION['p'] . "/phpcoverage.inc.php";
  134. require_once PHPCOVERAGE_HOME . "/remote/RemoteCoverageRecorder.php";
  135. require_once PHPCOVERAGE_HOME . "/reporter/HtmlCoverageReporter.php";
  136. // Initializations
  137. $includePaths = array();
  138. $excludePaths = array();
  139. if (!$OPTION['cov-url']){
  140. if(!$OPTION['report'] && !$OPTION['cov-data-file']) {
  141. echo "ERROR: No --cov-url option specified.\n";
  142. exit(1);
  143. }
  144. }
  145. if($OPTION['init']) {
  146. if(!$OPTION['tmp-dir']) {
  147. $OPTION['tmp-dir'] = $util->getTmpDir();
  148. }
  149. if(!$OPTION['cov-file-name']) {
  150. $OPTION['cov-file-name'] = "phpcoverage.data.xml";
  151. }
  152. }
  153. if($OPTION['report']) {
  154. if (!$OPTION['report-name']){
  155. echo "ERROR: No --report-name option specified.\n";
  156. exit(1);
  157. }
  158. if(!$OPTION['report-dir']) {
  159. if(!empty($PHPCOVERAGE_REPORT_DIR)) {
  160. $OPTION["report-dir"] = $PHPCOVERAGE_REPORT_DIR;
  161. }
  162. else {
  163. $OPTION["report-dir"] = "report";
  164. }
  165. }
  166. if(empty($OPTION['appbase-path']) && !empty($PHPCOVERAGE_APPBASE_PATH)) {
  167. $OPTION['appbase-path'] = realpath($PHPCOVERAGE_APPBASE_PATH);
  168. }
  169. if(isset($OPTION['include-paths'])) {
  170. $includePaths = explode(",", $OPTION['include-paths']);
  171. }
  172. if(isset($OPTION['appbase-path']) && !empty($OPTION["appbase-path"])) {
  173. $includePaths[] = $OPTION['appbase-path'];
  174. }
  175. if(isset($OPTION['exclude-paths'])) {
  176. $excludePaths = explode(",", $OPTION['exclude-paths']);
  177. }
  178. }
  179. if ($OPTION['verbose']){
  180. echo "Options: " . print_r($OPTION, true) . "\n";
  181. echo "include-paths: " . print_r($includePaths, true) . "\n";
  182. echo "exclude-paths: " . print_r($excludePaths, true) . "\n";
  183. }
  184. //
  185. //
  186. //
  187. if ($OPTION['init']){
  188. echo "PHPCoverage: init " . $OPTION['cov-url'] . "?phpcoverage-action=init&cov-file-name=". urlencode($OPTION["cov-file-name"]) . "&tmp-dir=" . urlencode($OPTION['tmp-dir']) . "\n";
  189. //
  190. // Initialize the PHPCoverage reporting framework
  191. //
  192. file_get_contents($OPTION['cov-url'] . "?phpcoverage-action=init&cov-file-name=". urlencode($OPTION["cov-file-name"]) . "&tmp-dir=" . urlencode($OPTION['tmp-dir']));
  193. }
  194. else if ($OPTION['report']){
  195. //
  196. // Retrieve coverage data (xml) from the PHPCoverage reporting framework
  197. //
  198. if($OPTION['cov-data-file']) {
  199. if(!is_readable($OPTION['cov-data-file'])) {
  200. echo "Error: Cannot read cov-data-file: " . $OPTION['cov-data-file'] . "\n";
  201. exit(1);
  202. }
  203. $xmlUrl = $OPTION['cov-data-file'];
  204. }
  205. else {
  206. echo "PHPCoverage: report " . $OPTION['cov-url'] . "?phpcoverage-action=get-coverage-xml" . "\n";
  207. $xmlUrl = $OPTION['cov-url'] . "?phpcoverage-action=get-coverage-xml";
  208. }
  209. //
  210. // Configure reporter, and generate the PHPCoverage report
  211. //
  212. $covReporter = new HtmlCoverageReporter($OPTION['report-name'], "", $OPTION["report-dir"]);
  213. //
  214. // Notice the coverage recorder is of type RemoteCoverageRecorder
  215. //
  216. $cov = new RemoteCoverageRecorder($includePaths, $excludePaths, $covReporter);
  217. $cov->generateReport($xmlUrl, true);
  218. $covReporter->printTextSummary($OPTION["report-dir"] . "/report.txt");
  219. // Should the summary be printed to console ?
  220. if(isset($OPTION['print-summary']) && $OPTION['print-summary']) {
  221. $covReporter->printTextSummary();
  222. }
  223. }
  224. else if ($OPTION['cleanup']){
  225. echo "PHPCoverage: cleanup " . $OPTION['cov-url'] . "?phpcoverage-action=cleanup";
  226. file_get_contents($OPTION['cov-url'] . "?phpcoverage-action=cleanup");
  227. }
  228. ?>