3 use Symfony\Component\Finder\Finder
;
6 * Search for PHPUnit test cases
10 * @return array<string> class names
12 static function _findTestClasses($path) {
14 // 'loading' => $path,
15 // get_included_files()
18 $origClasses = get_declared_classes();
20 $newClasses = get_declared_classes();
22 return preg_grep('/Test$/', array_diff(
29 * @return array (string $file => string $class)
31 static function findTestClasses($paths) {
32 $testClasses = array();
33 $finder = new Finder();
35 foreach ($paths as $path) {
37 foreach ($finder->files()->in($paths)->name('*Test.php') as $file) {
38 $testClass = self
::_findTestClasses((string) $file);
39 if (count($testClass) == 1) {
40 $testClasses[(string) $file] = array_shift($testClass);
42 elseif (count($testClass) > 1) {
43 throw new \
Exception("Too many classes in $file");
46 throw new \
Exception("Too few classes in $file");
50 elseif (is_file($path)) {
51 $testClass = self
::_findTestClasses($path);
52 if (count($testClass) == 1) {
53 $testClasses[$path] = array_shift($testClass);
55 elseif (count($testClass) > 1) {
56 throw new \
Exception("Too many classes in $path");
59 throw new \
Exception("Too few classes in $path");
68 * @param array $testClasses
69 * @return array each element is an array with keys:
74 static function findTestsByPath($paths) {
76 $testClasses = self
::findTestClasses($paths);
77 foreach ($testClasses as $testFile => $testClass) {
78 $clazz = new \
ReflectionClass($testClass);
79 foreach ($clazz->getMethods() as $method) {
80 if (preg_match('/^test/', $method->name
)) {
83 'class' => $testClass,
84 'method' => $method->name