BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testing.class.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. class Testing
  3. {
  4. private static $ClassDirectories = array("classes");
  5. private static $Classes = [];
  6. /**
  7. * Initialize the testasble classes into a map keyed by class name
  8. */
  9. public static function init()
  10. {
  11. self::load_classes();
  12. }
  13. /**
  14. * Gets the class
  15. */
  16. public static function get_classes()
  17. {
  18. return self::$Classes;
  19. }
  20. /**
  21. * Loads all the classes within given directories
  22. */
  23. private static function load_classes()
  24. {
  25. foreach (self::$ClassDirectories as $Directory) {
  26. $Directory = SERVER_ROOT . "/" . $Directory . "/";
  27. foreach (glob($Directory . "*.php") as $FileName) {
  28. self::get_class_name($FileName);
  29. }
  30. }
  31. }
  32. /**
  33. * Gets the class and adds into the map
  34. */
  35. private static function get_class_name($FileName)
  36. {
  37. $Tokens = token_get_all(file_get_contents($FileName));
  38. $IsTestable = false;
  39. $IsClass = false;
  40. foreach ($Tokens as $Token) {
  41. if (is_array($Token)) {
  42. if (!$IsTestable && $Token[0] == T_DOC_COMMENT && strpos($Token[1], "@TestClass")) {
  43. $IsTestable = true;
  44. }
  45. if ($IsTestable && $Token[0] == T_CLASS) {
  46. $IsClass = true;
  47. } elseif ($IsClass && $Token[0] == T_STRING) {
  48. $ReflectionClass = new ReflectionClass($Token[1]);
  49. if (count(self::get_testable_methods($ReflectionClass))) {
  50. self::$Classes[$Token[1]] = new ReflectionClass($Token[1]);
  51. }
  52. $IsTestable = false;
  53. $IsClass = false;
  54. }
  55. }
  56. }
  57. }
  58. /**
  59. * Checks if class exists in the map
  60. */
  61. public static function has_class($Class)
  62. {
  63. return array_key_exists($Class, self::$Classes);
  64. }
  65. /**
  66. * Checks if class has a given testable methood
  67. */
  68. public static function has_testable_method($Class, $Method)
  69. {
  70. $TestableMethods = self::get_testable_methods($Class);
  71. foreach ($TestableMethods as $TestMethod) {
  72. if ($TestMethod->getName() === $Method) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. /**
  79. * Get testable methods in a class, a testable method has a @Test
  80. */
  81. public static function get_testable_methods($Class)
  82. {
  83. if (is_string($Class)) {
  84. $ReflectionClass = self::$Classes[$Class];
  85. } else {
  86. $ReflectionClass = $Class;
  87. }
  88. $ReflectionMethods = $ReflectionClass->getMethods();
  89. $TestableMethods = [];
  90. foreach ($ReflectionMethods as $Method) {
  91. if ($Method->isPublic() && $Method->isStatic() && strpos($Method->getDocComment(), "@Test")) {
  92. $TestableMethods[] = $Method;
  93. }
  94. }
  95. return $TestableMethods;
  96. }
  97. /**
  98. * Get the class comment
  99. */
  100. public static function get_class_comment($Class)
  101. {
  102. $ReflectionClass = self::$Classes[$Class];
  103. return trim(str_replace(array("@TestClass", "*", "/"), "", $ReflectionClass->getDocComment()));
  104. }
  105. /**
  106. * Get the undocumented methods in a class
  107. */
  108. public static function get_undocumented_methods($Class)
  109. {
  110. $ReflectionClass = self::$Classes[$Class];
  111. $Methods = [];
  112. foreach ($ReflectionClass->getMethods() as $Method) {
  113. if (!$Method->getDocComment()) {
  114. $Methods[] = $Method;
  115. }
  116. }
  117. return $Methods;
  118. }
  119. /**
  120. * Get the documented methods
  121. */
  122. public static function get_documented_methods($Class)
  123. {
  124. $ReflectionClass = self::$Classes[$Class];
  125. $Methods = [];
  126. foreach ($ReflectionClass->getMethods() as $Method) {
  127. if ($Method->getDocComment()) {
  128. $Methods[] = $Method;
  129. }
  130. }
  131. return $Methods;
  132. }
  133. /**
  134. * Get all methods in a class
  135. */
  136. public static function get_methods($Class)
  137. {
  138. return self::$Classes[$Class]->getMethods();
  139. }
  140. /**
  141. * Get a method comment
  142. */
  143. public static function get_method_comment($Method)
  144. {
  145. return trim(str_replace(array("*", "/"), "", $Method->getDocComment()));
  146. }
  147. }