4 use Symfony\Component\Finder\Finder
;
7 * Search for PHPUnit test cases
12 * @return array <string> class names
14 public static function _findTestClasses($path) {
16 // 'loading' => $path,
17 // get_included_files()
20 $origClasses = get_declared_classes();
22 $newClasses = get_declared_classes();
24 return preg_grep('/Test$/', array_diff(
32 * @return array (string $file => string $class)
33 * (string $file => string $class)
36 public static function findTestClasses($paths) {
37 $testClasses = array();
38 $finder = new Finder();
40 foreach ($paths as $path) {
42 foreach ($finder->files()->in($paths)->name('*Test.php') as $file) {
43 $testClass = self
::_findTestClasses((string) $file);
44 if (count($testClass) == 1) {
45 $testClasses[(string) $file] = array_shift($testClass);
47 elseif (count($testClass) > 1) {
48 throw new \
Exception("Too many classes in $file");
51 throw new \
Exception("Too few classes in $file");
55 elseif (is_file($path)) {
56 $testClass = self
::_findTestClasses($path);
57 if (count($testClass) == 1) {
58 $testClasses[$path] = array_shift($testClass);
60 elseif (count($testClass) > 1) {
61 throw new \
Exception("Too many classes in $path");
64 throw new \
Exception("Too few classes in $path");
76 * each element is an array with keys:
81 public static function findTestsByPath($paths) {
83 $testClasses = self
::findTestClasses($paths);
84 foreach ($testClasses as $testFile => $testClass) {
85 $clazz = new \
ReflectionClass($testClass);
86 foreach ($clazz->getMethods() as $method) {
87 if (preg_match('/^test/', $method->name
)) {
90 'class' => $testClass,
91 'method' => $method->name