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.

167 lines
5.9 KiB

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License, |
  9. // | that is available at http://www.php.net/license/3_0.txt. |
  10. // | If you did not receive a copy of the PHP license and are unable to |
  11. // | obtain it through the world-wide-web, please send a note to |
  12. // | license@php.net so we can mail you a copy immediately. |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: SetupDecorator.php,v 1.12 2005/05/14 05:58:38 sebastian Exp $
  16. //
  17. /**
  18. * This decorator actually just adds the functionality to read the
  19. * test-suite classes from a given directory and instanciate them
  20. * automatically, use it as given in the example below.
  21. *
  22. * <code>
  23. * <?php
  24. * $gui = new PHPUnit_GUI_SetupDecorator(new PHPUnit_GUI_HTML());
  25. * $gui->getSuitesFromDir('/path/to/dir/tests','.*\.php$',array('index.php','sql.php'));
  26. * $gui->show();
  27. * ?>
  28. * </code>
  29. *
  30. * The example calls this class and tells it to:
  31. *
  32. * - find all file under the directory /path/to/dir/tests
  33. * - for files, which end with '.php' (this is a piece of a regexp, that's why the . is escaped)
  34. * - and to exclude the files 'index.php' and 'sql.php'
  35. * - and include all the files that are left in the tests.
  36. *
  37. * Given that the path (the first parameter) ends with 'tests' it will be assumed
  38. * that the classes are named tests_* where * is the directory plus the filename,
  39. * according to PEAR standards.
  40. *
  41. * So that:
  42. *
  43. * - 'testMe.php' in the dir 'tests' bill be assumed to contain a class tests_testMe
  44. * - '/moretests/aTest.php' should contain a class 'tests_moretests_aTest'
  45. *
  46. * @author Wolfram Kriesing <wolfram@kriesing.de>
  47. * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  48. * @category Testing
  49. * @package PHPUnit
  50. * @subpackage GUI
  51. */
  52. class PHPUnit_GUI_SetupDecorator
  53. {
  54. /**
  55. *
  56. *
  57. */
  58. function PHPUnit_GUI_SetupDecorator(&$gui)
  59. {
  60. $this->_gui = &$gui;
  61. }
  62. /**
  63. * just forwarding the action to the decorated class.
  64. *
  65. */
  66. function show($showPassed=TRUE)
  67. {
  68. $this->_gui->show($showPassed);
  69. }
  70. /**
  71. * Setup test suites that can be found in the given directory
  72. * Using the second parameter you can also choose a subsets of the files found
  73. * in the given directory. I.e. only all the files that contain '_UnitTest_',
  74. * in order to do this simply call it like this:
  75. * <code>getSuitesFromDir($dir,'.*_UnitTest_.*')</code>.
  76. * There you can already see that the pattern is built for the use within a regular expression.
  77. *
  78. * @param string the directory where to search for test-suite files
  79. * @param string the pattern (a regexp) by which to find the files
  80. * @param array an array of file names that shall be excluded
  81. */
  82. function getSuitesFromDir($dir, $filenamePattern = '', $exclude = array())
  83. {
  84. if ($dir{strlen($dir)-1} == DIRECTORY_SEPARATOR) {
  85. $dir = substr($dir, 0, -1);
  86. }
  87. $files = $this->_getFiles($dir, $filenamePattern, $exclude, realpath($dir . '/..'));
  88. asort($files);
  89. foreach ($files as $className => $aFile) {
  90. include_once($aFile);
  91. if (class_exists($className)) {
  92. $suites[] =& new PHPUnit_TestSuite($className);
  93. } else {
  94. trigger_error("$className could not be found in $dir$aFile!");
  95. }
  96. }
  97. $this->_gui->addSuites($suites);
  98. }
  99. /**
  100. * This method searches recursively through the directories
  101. * to find all the files that shall be added to the be visible.
  102. *
  103. * @param string the path where find the files
  104. * @param srting the string pattern by which to find the files
  105. * @param string the file names to be excluded
  106. * @param string the root directory, which serves as the prefix to the fully qualified filename
  107. * @access private
  108. */
  109. function _getFiles($dir, $filenamePattern, $exclude, $rootDir)
  110. {
  111. $files = array();
  112. if ($dp = opendir($dir)) {
  113. while (FALSE !== ($file = readdir($dp))) {
  114. $filename = $dir . DIRECTORY_SEPARATOR . $file;
  115. $match = TRUE;
  116. if ($filenamePattern && !preg_match("~$filenamePattern~", $file)) {
  117. $match = FALSE;
  118. }
  119. if (sizeof($exclude)) {
  120. foreach ($exclude as $aExclude) {
  121. if (strpos($file, $aExclude) !== FALSE) {
  122. $match = FALSE;
  123. break;
  124. }
  125. }
  126. }
  127. if (is_file($filename) && $match) {
  128. $tmp = str_replace($rootDir, '', $filename);
  129. if (strpos($tmp, DIRECTORY_SEPARATOR) === 0) {
  130. $tmp = substr($tmp, 1);
  131. }
  132. if (strpos($tmp, '/') === 0) {
  133. $tmp = substr($tmp, 1);
  134. }
  135. $className = str_replace(DIRECTORY_SEPARATOR, '_', $tmp);
  136. $className = basename($className, '.php');
  137. $files[$className] = $filename;
  138. }
  139. if ($file != '.' && $file != '..' && is_dir($filename)) {
  140. $files = array_merge($files, $this->_getFiles($filename, $filenamePattern, $exclude, $rootDir));
  141. }
  142. }
  143. closedir($dp);
  144. }
  145. return $files;
  146. }
  147. }
  148. ?>