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.4KB

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