Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / simpletestlib / collector.php
blob439f6b5e9dc56dc6429506b6bcd57a827b840068
1 <?php
2 /**
3 * This file contains the following classes: {@link SimpleCollector},
4 * {@link SimplePatternCollector}.
6 * @author Travis Swicegood <development@domain51.com>
7 * @package SimpleTest
8 * @subpackage UnitTester
9 * @version $Id$
12 /**
13 * The basic collector for {@link GroupTest}
15 * @see collect(), GroupTest::collect()
16 * @package SimpleTest
17 * @subpackage UnitTester
19 class SimpleCollector {
21 /**
22 * Strips off any kind of slash at the end so as to normalise the path.
23 * @param string $path Path to normalise.
24 * @return string Path without trailing slash.
26 function _removeTrailingSlash($path) {
27 if (substr($path, -1) == DIRECTORY_SEPARATOR) {
28 return substr($path, 0, -1);
29 } elseif (substr($path, -1) == '/') {
30 return substr($path, 0, -1);
31 } else {
32 return $path;
36 /**
37 * Scans the directory and adds what it can.
38 * @param object $test Group test with {@link GroupTest::addTestFile()} method.
39 * @param string $path Directory to scan.
40 * @see _attemptToAdd()
42 function collect(&$test, $path) {
43 $path = $this->_removeTrailingSlash($path);
44 if ($handle = opendir($path)) {
45 while (($entry = readdir($handle)) !== false) {
46 $this->_handle($test, $path . DIRECTORY_SEPARATOR . $entry);
48 closedir($handle);
52 /**
53 * This method determines what should be done with a given file and adds
54 * it via {@link GroupTest::addTestFile()} if necessary.
56 * This method should be overriden to provide custom matching criteria,
57 * such as pattern matching, recursive matching, etc. For an example, see
58 * {@link SimplePatternCollector::_handle()}.
60 * @param object $test Group test with {@link GroupTest::addTestFile()} method.
61 * @param string $filename A filename as generated by {@link collect()}
62 * @see collect()
63 * @access protected
65 function _handle(&$test, $file) {
66 if (! is_dir($file)) {
67 $test->addTestFile($file);
72 /**
73 * An extension to {@link SimpleCollector} that only adds files matching a
74 * given pattern.
76 * @package SimpleTest
77 * @subpackage UnitTester
78 * @see SimpleCollector
80 class SimplePatternCollector extends SimpleCollector {
81 var $_pattern;
83 /**
85 * @param string $pattern Perl compatible regex to test name against
86 * See {@link http://us4.php.net/manual/en/reference.pcre.pattern.syntax.php PHP's PCRE}
87 * for full documentation of valid pattern.s
89 function SimplePatternCollector($pattern = '/php$/i') {
90 $this->_pattern = $pattern;
93 /**
94 * Attempts to add files that match a given pattern.
96 * @see SimpleCollector::_handle()
97 * @param object $test Group test with {@link GroupTest::addTestFile()} method.
98 * @param string $path Directory to scan.
99 * @access protected
101 function _handle(&$test, $filename) {
102 if (preg_match($this->_pattern, $filename)) {
103 parent::_handle($test, $filename);