PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/scripts/dev/find_tested.php

http://github.com/infusion/PHP
PHP | 222 lines | 154 code | 44 blank | 24 comment | 37 complexity | cc4e372810b1370b09035ec941a9d019 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. $usage = <<<USAGE
  3. Usage: php find_tested.php [path_to_test_files] ([extension])
  4. Outputs test coverage information for functions and methods in csv format.
  5. Supplying an optional extension name outputs only information for functions and methods from that extension.
  6. Output format:
  7. Extension, Class Name, Method/Function Name, Test Status, Test Files
  8. A test status of "verify" for a method means that there is at least one other method of the same name, so test coverage must be verified manually.
  9. USAGE;
  10. /* method record fields */
  11. define("CLASS_NAME", "CLASS_NAME");
  12. define("METHOD_NAME", "METHOD_NAME");
  13. define("EXTENSION_NAME", "EXTENSION_NAME");
  14. define("IS_DUPLICATE", "IS_DUPLICATE");
  15. define("IS_TESTED", "IS_TESTED");
  16. define("TESTS", "TESTS");
  17. // process command line args
  18. $num_params = $argc;
  19. if ($num_params < 2 || $num_params > 3) {
  20. die($usage);
  21. }
  22. $extension_test_path = $argv[1];
  23. if ($num_params == 3) {
  24. $extension_name = $argv[2];
  25. // check extension exists
  26. $extensions = get_loaded_extensions();
  27. if (!in_array($extension_name, $extensions)) {
  28. echo "Error: extension $extension_name is not loaded. Loaded extensions:\n";
  29. foreach($extensions as $extension) {
  30. echo "$extension\n";
  31. }
  32. die();
  33. }
  34. } else {
  35. $extension_name = false;
  36. }
  37. $method_info = populate_method_info();
  38. if ($extension_name != false) {
  39. // get only the methods from the extension we are querying
  40. $extension_method_info = array();
  41. foreach($method_info as $method_record) {
  42. if (strcasecmp($extension_name, $method_record[EXTENSION_NAME]) == 0) {
  43. $extension_method_info[] = $method_record;
  44. }
  45. }
  46. } else {
  47. $extension_method_info = $method_info;
  48. }
  49. get_phpt_files($extension_test_path, $count, $phpt_files);
  50. $extension_method_info = mark_methods_as_tested($extension_method_info, $phpt_files);
  51. foreach($extension_method_info as $record) {
  52. echo $record[EXTENSION_NAME] . ",";
  53. echo $record[CLASS_NAME] . ",";
  54. echo $record[METHOD_NAME] . ",";
  55. echo $record[IS_TESTED] . ",";
  56. echo $record[TESTS] . "\n";
  57. }
  58. /**
  59. * Marks the "tested" status of methods in $method_info according
  60. * to whether they are tested in $phpt_files
  61. */
  62. function mark_methods_as_tested($method_info, $phpt_files) {
  63. foreach($phpt_files as $phpt_file) {
  64. $tested_functions = extract_tests($phpt_file);
  65. foreach($tested_functions as $tested_function) {
  66. // go through method info array marking this funtion as tested
  67. foreach($method_info as &$current_method_record) {
  68. if (strcasecmp($tested_function, $current_method_record[METHOD_NAME]) == 0) {
  69. // matched the method name
  70. if ($current_method_record[IS_DUPLICATE] == true) {
  71. // we cannot be sure which class this method corresponds to,
  72. // so mark method as needing to be verified
  73. $current_method_record[IS_TESTED] = "verify";
  74. } else {
  75. $current_method_record[IS_TESTED] = "yes";
  76. }
  77. $current_method_record[TESTS] .= $phpt_file . "; ";
  78. }
  79. }
  80. }
  81. }
  82. return $method_info;
  83. }
  84. /**
  85. * returns an array containing a record for each defined method.
  86. */
  87. function populate_method_info() {
  88. $method_info = array();
  89. // get functions
  90. $all_functions = get_defined_functions();
  91. $internal_functions = $all_functions["internal"];
  92. foreach ($internal_functions as $function) {
  93. // populate new method record
  94. $function_record = array();
  95. $function_record[CLASS_NAME] = "Function";
  96. $function_record[METHOD_NAME] = $function;
  97. $function_record[IS_TESTED] = "no";
  98. $function_record[TESTS] = "";
  99. $function_record[IS_DUPLICATE] = false;
  100. // record the extension that the function belongs to
  101. $reflectionFunction = new ReflectionFunction($function);
  102. $extension = $reflectionFunction->getExtension();
  103. if ($extension != null) {
  104. $function_record[EXTENSION_NAME] = $extension->getName();
  105. } else {
  106. $function_record[EXTENSION_NAME] = "";
  107. }
  108. // insert new method record into info array
  109. $method_info[] = $function_record;
  110. }
  111. // get methods
  112. $all_classes = get_declared_classes();
  113. foreach ($all_classes as $class) {
  114. $reflectionClass = new ReflectionClass($class);
  115. $methods = $reflectionClass->getMethods();
  116. foreach ($methods as $method) {
  117. // populate new method record
  118. $new_method_record = array();
  119. $new_method_record[CLASS_NAME] = $reflectionClass->getName();
  120. $new_method_record[METHOD_NAME] = $method->getName();
  121. $new_method_record[IS_TESTED] = "no";
  122. $new_method_record[TESTS] = "";
  123. $extension = $reflectionClass->getExtension();
  124. if ($extension != null) {
  125. $new_method_record[EXTENSION_NAME] = $extension->getName();
  126. } else {
  127. $new_method_record[EXTENSION_NAME] = "";
  128. }
  129. // check for duplicate method names
  130. $new_method_record[IS_DUPLICATE] = false;
  131. foreach ($method_info as &$current_record) {
  132. if (strcmp($current_record[METHOD_NAME], $new_method_record[METHOD_NAME]) == 0) {
  133. $new_method_record[IS_DUPLICATE] = true;
  134. $current_record[IS_DUPLICATE] = true;
  135. }
  136. }
  137. // insert new method record into info array
  138. $method_info[] = $new_method_record;
  139. }
  140. }
  141. return $method_info;
  142. }
  143. function get_phpt_files($dir, &$phpt_file_count, &$all_phpt)
  144. {
  145. $thisdir = dir($dir.'/'); //include the trailing slash
  146. while(($file = $thisdir->read()) !== false) {
  147. if ($file != '.' && $file != '..') {
  148. $path = $thisdir->path.$file;
  149. if(is_dir($path) == true) {
  150. get_phpt_files($path , $phpt_file_count , $all_phpt);
  151. } else {
  152. if (preg_match("/\w+\.phpt$/", $file)) {
  153. $all_phpt[$phpt_file_count] = $path;
  154. $phpt_file_count++;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. function extract_tests($file) {
  161. $code = file_get_contents($file);
  162. if (!preg_match('/--FILE--\s*(.*)\s*--(EXPECTF|EXPECTREGEX|EXPECT)?--/is', $code, $r)) {
  163. // print "Unable to get code in ".$file."\n";
  164. return array();
  165. }
  166. $tokens = token_get_all($r[1]);
  167. $functions = array_filter($tokens, 'filter_functions');
  168. $functions = array_map( 'map_token_value',$functions);
  169. $functions = array_unique($functions);
  170. return $functions;
  171. }
  172. function filter_functions($x) {
  173. return $x[0] == 307;
  174. }
  175. function map_token_value($x) {
  176. return $x[1];
  177. }
  178. ?>