PHPUnitUtil.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * $Id: PHPUnitUtil.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. /**
  22. * Various utility functions
  23. *
  24. * @author Michiel Rook <michiel.rook@gmail.com>
  25. * @version $Id: PHPUnitUtil.php 905 2010-10-05 16:28:03Z mrook $
  26. * @package phing.tasks.ext.phpunit
  27. * @since 2.1.0
  28. */
  29. class PHPUnitUtil
  30. {
  31. protected static $definedClasses = array();
  32. /**
  33. * Returns the package of a class as defined in the docblock of the class using @package
  34. *
  35. * @param string the name of the class
  36. * @return string the name of the package
  37. */
  38. static function getPackageName($classname)
  39. {
  40. $reflect = new ReflectionClass($classname);
  41. if (preg_match('/@package[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches))
  42. {
  43. return $matches[1];
  44. }
  45. else
  46. {
  47. return "default";
  48. }
  49. }
  50. /**
  51. * Returns the subpackage of a class as defined in the docblock of the class
  52. * using @subpackage
  53. *
  54. * @param string $classname the name of the class
  55. *
  56. * @author Benjamin Schultz <bschultz@proqrent.de>
  57. * @return string|null the name of the subpackage
  58. */
  59. public static function getSubpackageName($classname)
  60. {
  61. $reflect = new ReflectionClass($classname);
  62. if (preg_match('/@subpackage[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches)) {
  63. return $matches[1];
  64. } else {
  65. return null;
  66. }
  67. }
  68. /**
  69. * Derives the classname from a filename.
  70. * Assumes that there is only one class defined in that particular file, and that
  71. * the naming follows the dot-path (Java) notation scheme.
  72. *
  73. * @param string the filename
  74. * @return string the name fo the class
  75. */
  76. public static function getClassFromFileName($filename)
  77. {
  78. $filename = basename($filename);
  79. $rpos = strrpos($filename, '.');
  80. if ($rpos != -1)
  81. {
  82. $filename = substr($filename, 0, $rpos);
  83. }
  84. return $filename;
  85. }
  86. /**
  87. * @param string the filename
  88. * @param Path optional classpath
  89. * @return array list of classes defined in the file
  90. */
  91. public static function getDefinedClasses($filename, $classpath = NULL)
  92. {
  93. $filename = realpath($filename);
  94. if (!file_exists($filename))
  95. {
  96. throw new Exception("File '" . $filename . "' does not exist");
  97. }
  98. if (isset(self::$definedClasses[$filename]))
  99. {
  100. return self::$definedClasses[$filename];
  101. }
  102. Phing::__import($filename, $classpath);
  103. $declaredClasses = get_declared_classes();
  104. foreach ($declaredClasses as $classname)
  105. {
  106. $reflect = new ReflectionClass($classname);
  107. self::$definedClasses[$reflect->getFilename()][] = $classname;
  108. if (is_array(self::$definedClasses[$reflect->getFilename()]))
  109. {
  110. self::$definedClasses[$reflect->getFilename()] = array_unique(self::$definedClasses[$reflect->getFilename()]);
  111. }
  112. }
  113. if (isset(self::$definedClasses[$filename]))
  114. {
  115. return self::$definedClasses[$filename];
  116. }
  117. else
  118. {
  119. return array();
  120. }
  121. }
  122. }