PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/test-suite/lib/simpletest/collector.php

http://github.com/swiftmailer/swiftmailer
PHP | 122 lines | 45 code | 8 blank | 69 comment | 10 complexity | 956b69d601869e1f62bb182377bfebe6 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file contains the following classes: {@link SimpleCollector},
  4. * {@link SimplePatternCollector}.
  5. *
  6. * @author Travis Swicegood <development@domain51.com>
  7. * @package SimpleTest
  8. * @subpackage UnitTester
  9. * @version $Id: collector.php 1784 2008-04-26 13:07:14Z pp11 $
  10. */
  11. /**
  12. * The basic collector for {@link GroupTest}
  13. *
  14. * @see collect(), GroupTest::collect()
  15. * @package SimpleTest
  16. * @subpackage UnitTester
  17. */
  18. class SimpleCollector {
  19. /**
  20. * Strips off any kind of slash at the end so as to normalise the path.
  21. * @param string $path Path to normalise.
  22. * @return string Path without trailing slash.
  23. */
  24. protected function removeTrailingSlash($path) {
  25. if (substr($path, -1) == DIRECTORY_SEPARATOR) {
  26. return substr($path, 0, -1);
  27. } elseif (substr($path, -1) == '/') {
  28. return substr($path, 0, -1);
  29. } else {
  30. return $path;
  31. }
  32. }
  33. /**
  34. * Scans the directory and adds what it can.
  35. * @param object $test Group test with {@link GroupTest::addTestFile()} method.
  36. * @param string $path Directory to scan.
  37. * @see _attemptToAdd()
  38. */
  39. function collect(&$test, $path) {
  40. $path = $this->removeTrailingSlash($path);
  41. if ($handle = opendir($path)) {
  42. while (($entry = readdir($handle)) !== false) {
  43. if ($this->isHidden($entry)) {
  44. continue;
  45. }
  46. $this->handle($test, $path . DIRECTORY_SEPARATOR . $entry);
  47. }
  48. closedir($handle);
  49. }
  50. }
  51. /**
  52. * This method determines what should be done with a given file and adds
  53. * it via {@link GroupTest::addTestFile()} if necessary.
  54. *
  55. * This method should be overriden to provide custom matching criteria,
  56. * such as pattern matching, recursive matching, etc. For an example, see
  57. * {@link SimplePatternCollector::_handle()}.
  58. *
  59. * @param object $test Group test with {@link GroupTest::addTestFile()} method.
  60. * @param string $filename A filename as generated by {@link collect()}
  61. * @see collect()
  62. * @access protected
  63. */
  64. protected function handle(&$test, $file) {
  65. if (is_dir($file)) {
  66. return;
  67. }
  68. $test->addFile($file);
  69. }
  70. /**
  71. * Tests for hidden files so as to skip them. Currently
  72. * only tests for Unix hidden files.
  73. * @param string $filename Plain filename.
  74. * @return boolean True if hidden file.
  75. * @access private
  76. */
  77. protected function isHidden($filename) {
  78. return strncmp($filename, '.', 1) == 0;
  79. }
  80. }
  81. /**
  82. * An extension to {@link SimpleCollector} that only adds files matching a
  83. * given pattern.
  84. *
  85. * @package SimpleTest
  86. * @subpackage UnitTester
  87. * @see SimpleCollector
  88. */
  89. class SimplePatternCollector extends SimpleCollector {
  90. private $pattern;
  91. /**
  92. *
  93. * @param string $pattern Perl compatible regex to test name against
  94. * See {@link http://us4.php.net/manual/en/reference.pcre.pattern.syntax.php PHP's PCRE}
  95. * for full documentation of valid pattern.s
  96. */
  97. function __construct($pattern = '/php$/i') {
  98. $this->pattern = $pattern;
  99. }
  100. /**
  101. * Attempts to add files that match a given pattern.
  102. *
  103. * @see SimpleCollector::_handle()
  104. * @param object $test Group test with {@link GroupTest::addTestFile()} method.
  105. * @param string $path Directory to scan.
  106. * @access protected
  107. */
  108. protected function handle(&$test, $filename) {
  109. if (preg_match($this->pattern, $filename)) {
  110. parent::handle($test, $filename);
  111. }
  112. }
  113. }
  114. ?>