Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
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
namespace PHPUnit\Runner;
11
 
12
use function array_diff;
13
use function array_values;
14
use function basename;
15
use function class_exists;
16
use function get_declared_classes;
17
use function sprintf;
18
use function stripos;
19
use function strlen;
20
use function substr;
21
use PHPUnit\Framework\TestCase;
22
use PHPUnit\Util\FileLoader;
23
use ReflectionClass;
24
use ReflectionException;
25
 
26
/**
27
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
28
 *
29
 * @deprecated see https://github.com/sebastianbergmann/phpunit/issues/4039
30
 */
31
final class StandardTestSuiteLoader implements TestSuiteLoader
32
{
33
    /**
34
     * @throws Exception
35
     */
36
    public function load(string $suiteClassFile): ReflectionClass
37
    {
38
        $suiteClassName = basename($suiteClassFile, '.php');
39
        $loadedClasses  = get_declared_classes();
40
 
41
        if (!class_exists($suiteClassName, false)) {
42
            /* @noinspection UnusedFunctionResultInspection */
43
            FileLoader::checkAndLoad($suiteClassFile);
44
 
45
            $loadedClasses = array_values(
46
                array_diff(get_declared_classes(), $loadedClasses)
47
            );
48
 
49
            if (empty($loadedClasses)) {
50
                throw $this->exceptionFor($suiteClassName, $suiteClassFile);
51
            }
52
        }
53
 
54
        if (!class_exists($suiteClassName, false)) {
55
            $offset = 0 - strlen($suiteClassName);
56
 
57
            foreach ($loadedClasses as $loadedClass) {
58
                // @see https://github.com/sebastianbergmann/phpunit/issues/5020
59
                if (stripos(substr($loadedClass, $offset - 1), '\\' . $suiteClassName) === 0 ||
60
                    stripos(substr($loadedClass, $offset - 1), '_' . $suiteClassName) === 0) {
61
                    $suiteClassName = $loadedClass;
62
 
63
                    break;
64
                }
65
            }
66
        }
67
 
68
        if (!class_exists($suiteClassName, false)) {
69
            throw $this->exceptionFor($suiteClassName, $suiteClassFile);
70
        }
71
 
72
        try {
73
            $class = new ReflectionClass($suiteClassName);
74
            // @codeCoverageIgnoreStart
75
        } catch (ReflectionException $e) {
76
            throw new Exception(
77
                $e->getMessage(),
78
                $e->getCode(),
79
                $e
80
            );
81
        }
82
        // @codeCoverageIgnoreEnd
83
 
84
        if ($class->isSubclassOf(TestCase::class) && !$class->isAbstract()) {
85
            return $class;
86
        }
87
 
88
        if ($class->hasMethod('suite')) {
89
            try {
90
                $method = $class->getMethod('suite');
91
                // @codeCoverageIgnoreStart
92
            } catch (ReflectionException $e) {
93
                throw new Exception(
94
                    $e->getMessage(),
95
                    $e->getCode(),
96
                    $e
97
                );
98
            }
99
            // @codeCoverageIgnoreEnd
100
 
101
            if (!$method->isAbstract() && $method->isPublic() && $method->isStatic()) {
102
                return $class;
103
            }
104
        }
105
 
106
        throw $this->exceptionFor($suiteClassName, $suiteClassFile);
107
    }
108
 
109
    public function reload(ReflectionClass $aClass): ReflectionClass
110
    {
111
        return $aClass;
112
    }
113
 
114
    private function exceptionFor(string $className, string $filename): Exception
115
    {
116
        return new Exception(
117
            sprintf(
118
                "Class '%s' could not be found in '%s'.",
119
                $className,
120
                $filename
121
            )
122
        );
123
    }
124
}