Subversion-Projekte lars-tiefland.laravel_shop

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\Extension;
11
 
12
use function class_exists;
13
use function sprintf;
14
use PHPUnit\Framework\TestListener;
15
use PHPUnit\Runner\Exception;
16
use PHPUnit\Runner\Hook;
17
use PHPUnit\TextUI\TestRunner;
18
use PHPUnit\TextUI\XmlConfiguration\Extension;
19
use ReflectionClass;
20
use ReflectionException;
21
 
22
/**
23
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
24
 */
25
final class ExtensionHandler
26
{
27
    /**
28
     * @throws Exception
29
     */
30
    public function registerExtension(Extension $extensionConfiguration, TestRunner $runner): void
31
    {
32
        $extension = $this->createInstance($extensionConfiguration);
33
 
34
        if (!$extension instanceof Hook) {
35
            throw new Exception(
36
                sprintf(
37
                    'Class "%s" does not implement a PHPUnit\Runner\Hook interface',
38
                    $extensionConfiguration->className()
39
                )
40
            );
41
        }
42
 
43
        $runner->addExtension($extension);
44
    }
45
 
46
    /**
47
     * @throws Exception
48
     *
49
     * @deprecated
50
     */
51
    public function createTestListenerInstance(Extension $listenerConfiguration): TestListener
52
    {
53
        $listener = $this->createInstance($listenerConfiguration);
54
 
55
        if (!$listener instanceof TestListener) {
56
            throw new Exception(
57
                sprintf(
58
                    'Class "%s" does not implement the PHPUnit\Framework\TestListener interface',
59
                    $listenerConfiguration->className()
60
                )
61
            );
62
        }
63
 
64
        return $listener;
65
    }
66
 
67
    /**
68
     * @throws Exception
69
     */
70
    private function createInstance(Extension $extensionConfiguration): object
71
    {
72
        $this->ensureClassExists($extensionConfiguration);
73
 
74
        try {
75
            $reflector = new ReflectionClass($extensionConfiguration->className());
76
        } catch (ReflectionException $e) {
77
            throw new Exception(
78
                $e->getMessage(),
79
                $e->getCode(),
80
                $e
81
            );
82
        }
83
 
84
        if (!$extensionConfiguration->hasArguments()) {
85
            return $reflector->newInstance();
86
        }
87
 
88
        return $reflector->newInstanceArgs($extensionConfiguration->arguments());
89
    }
90
 
91
    /**
92
     * @throws Exception
93
     */
94
    private function ensureClassExists(Extension $extensionConfiguration): void
95
    {
96
        if (class_exists($extensionConfiguration->className(), false)) {
97
            return;
98
        }
99
 
100
        if ($extensionConfiguration->hasSourceFile()) {
101
            /**
102
             * @noinspection PhpIncludeInspection
103
             *
104
             * @psalm-suppress UnresolvableInclude
105
             */
106
            require_once $extensionConfiguration->sourceFile();
107
        }
108
 
109
        if (!class_exists($extensionConfiguration->className())) {
110
            throw new Exception(
111
                sprintf(
112
                    'Class "%s" does not exist',
113
                    $extensionConfiguration->className()
114
                )
115
            );
116
        }
117
    }
118
}