1<?php
2/*
3 * This file is part of PHPUnit.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11/**
12 * The standard test suite loader.
13 */
14class PHPUnit_Runner_StandardTestSuiteLoader implements PHPUnit_Runner_TestSuiteLoader
15{
16    /**
17     * @param string $suiteClassName
18     * @param string $suiteClassFile
19     *
20     * @return ReflectionClass
21     *
22     * @throws PHPUnit_Framework_Exception
23     */
24    public function load($suiteClassName, $suiteClassFile = '')
25    {
26        $suiteClassName = str_replace('.php', '', $suiteClassName);
27
28        if (empty($suiteClassFile)) {
29            $suiteClassFile = PHPUnit_Util_Filesystem::classNameToFilename(
30                $suiteClassName
31            );
32        }
33
34        if (!class_exists($suiteClassName, false)) {
35            $loadedClasses = get_declared_classes();
36
37            $filename = PHPUnit_Util_Fileloader::checkAndLoad($suiteClassFile);
38
39            $loadedClasses = array_values(
40                array_diff(get_declared_classes(), $loadedClasses)
41            );
42        }
43
44        if (!class_exists($suiteClassName, false) && !empty($loadedClasses)) {
45            $offset = 0 - strlen($suiteClassName);
46
47            foreach ($loadedClasses as $loadedClass) {
48                $class = new ReflectionClass($loadedClass);
49                if (substr($loadedClass, $offset) === $suiteClassName &&
50                    $class->getFileName() == $filename) {
51                    $suiteClassName = $loadedClass;
52                    break;
53                }
54            }
55        }
56
57        if (!class_exists($suiteClassName, false) && !empty($loadedClasses)) {
58            $testCaseClass = 'PHPUnit_Framework_TestCase';
59
60            foreach ($loadedClasses as $loadedClass) {
61                $class     = new ReflectionClass($loadedClass);
62                $classFile = $class->getFileName();
63
64                if ($class->isSubclassOf($testCaseClass) &&
65                    !$class->isAbstract()) {
66                    $suiteClassName = $loadedClass;
67                    $testCaseClass  = $loadedClass;
68
69                    if ($classFile == realpath($suiteClassFile)) {
70                        break;
71                    }
72                }
73
74                if ($class->hasMethod('suite')) {
75                    $method = $class->getMethod('suite');
76
77                    if (!$method->isAbstract() &&
78                        $method->isPublic() &&
79                        $method->isStatic()) {
80                        $suiteClassName = $loadedClass;
81
82                        if ($classFile == realpath($suiteClassFile)) {
83                            break;
84                        }
85                    }
86                }
87            }
88        }
89
90        if (class_exists($suiteClassName, false)) {
91            $class = new ReflectionClass($suiteClassName);
92
93            if ($class->getFileName() == realpath($suiteClassFile)) {
94                return $class;
95            }
96        }
97
98        throw new PHPUnit_Framework_Exception(
99            sprintf(
100                "Class '%s' could not be found in '%s'.",
101                $suiteClassName,
102                $suiteClassFile
103            )
104        );
105    }
106
107    /**
108     * @param ReflectionClass $aClass
109     *
110     * @return ReflectionClass
111     */
112    public function reload(ReflectionClass $aClass)
113    {
114        return $aClass;
115    }
116}
117