|
3 | 3 |
|
4 | 4 | namespace Zalas\PHPUnit\Globals; |
5 | 5 |
|
6 | | -use PHPUnit\Runner\AfterTestHook; |
7 | | -use PHPUnit\Runner\BeforeTestHook; |
8 | | -use PHPUnit\Util\Test; |
| 6 | +use PHPUnit\Runner\Extension\Extension; |
| 7 | +use PHPUnit\Runner\Extension\Facade; |
| 8 | +use PHPUnit\Runner\Extension\ParameterCollection; |
| 9 | +use PHPUnit\TextUI\Configuration\Configuration; |
9 | 10 |
|
10 | | -class AnnotationExtension implements BeforeTestHook, AfterTestHook |
| 11 | +final class AnnotationExtension implements Extension |
11 | 12 | { |
12 | | - private $server; |
13 | | - private $env; |
14 | | - private $getenv; |
15 | | - |
16 | | - public function executeBeforeTest(string $test): void |
17 | | - { |
18 | | - $this->backupGlobals(); |
19 | | - $this->readGlobalAnnotations($test); |
20 | | - } |
21 | | - |
22 | | - public function executeAfterTest(string $test, float $time): void |
23 | | - { |
24 | | - $this->restoreGlobals(); |
25 | | - } |
26 | | - |
27 | | - private function backupGlobals(): void |
28 | | - { |
29 | | - $this->server = $_SERVER; |
30 | | - $this->env = $_ENV; |
31 | | - $this->getenv = \getenv(); |
32 | | - } |
33 | | - |
34 | | - private function restoreGlobals(): void |
35 | | - { |
36 | | - $_SERVER = $this->server; |
37 | | - $_ENV = $this->env; |
38 | | - |
39 | | - foreach (\array_diff_assoc($this->getenv, \getenv()) as $name => $value) { |
40 | | - \putenv(\sprintf('%s=%s', $name, $value)); |
41 | | - } |
42 | | - foreach (\array_diff_assoc(\getenv(), $this->getenv) as $name => $value) { |
43 | | - \putenv($name); |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - private function readGlobalAnnotations(string $test) |
48 | | - { |
49 | | - $globalVars = $this->parseGlobalAnnotations($test); |
50 | | - |
51 | | - foreach ($globalVars['env'] as $name => $value) { |
52 | | - $_ENV[$name] = $value; |
53 | | - } |
54 | | - foreach ($globalVars['server'] as $name => $value) { |
55 | | - $_SERVER[$name] = $value; |
56 | | - } |
57 | | - foreach ($globalVars['putenv'] as $name => $value) { |
58 | | - \putenv(\sprintf('%s=%s', $name, $value)); |
59 | | - } |
60 | | - |
61 | | - $unsetVars = $this->findUnsetVarAnnotations($test); |
62 | | - |
63 | | - foreach ($unsetVars['unset-env'] as $name) { |
64 | | - unset($_ENV[$name]); |
65 | | - } |
66 | | - foreach ($unsetVars['unset-server'] as $name) { |
67 | | - unset($_SERVER[$name]); |
68 | | - } |
69 | | - foreach ($unsetVars['unset-getenv'] as $name) { |
70 | | - \putenv($name); |
71 | | - } |
72 | | - } |
73 | | - |
74 | | - private function parseGlobalAnnotations(string $test): array |
| 13 | + public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void |
75 | 14 | { |
76 | | - return \array_map(function (array $annotations) { |
77 | | - return \array_reduce($annotations, function ($carry, $annotation) { |
78 | | - list($name, $value) = \strpos($annotation, '=') ? \explode('=', $annotation, 2) : [$annotation, '']; |
79 | | - $carry[$name] = $value; |
80 | | - |
81 | | - return $carry; |
82 | | - }, []); |
83 | | - }, $this->findSetVarAnnotations($test)); |
84 | | - } |
85 | | - |
86 | | - private function findSetVarAnnotations(string $test): array |
87 | | - { |
88 | | - $annotations = $this->parseTestMethodAnnotations($test); |
89 | | - |
90 | | - return \array_filter( |
91 | | - \array_merge_recursive( |
92 | | - ['env' => [], 'server' => [], 'putenv' => []], |
93 | | - $annotations['class'] ?? [], |
94 | | - $annotations['method'] ?? [] |
95 | | - ), |
96 | | - function (string $annotationName) { |
97 | | - return \in_array($annotationName, ['env', 'server', 'putenv']); |
98 | | - }, |
99 | | - ARRAY_FILTER_USE_KEY |
100 | | - ); |
101 | | - } |
102 | | - |
103 | | - private function findUnsetVarAnnotations(string $test): array |
104 | | - { |
105 | | - $annotations = $this->parseTestMethodAnnotations($test); |
106 | | - |
107 | | - return \array_filter( |
108 | | - \array_merge_recursive( |
109 | | - ['unset-env' => [], 'unset-server' => [], 'unset-getenv' => []], |
110 | | - $annotations['class'] ?? [], |
111 | | - $annotations['method'] ?? [] |
112 | | - ), |
113 | | - function (string $annotationName) { |
114 | | - return \in_array($annotationName, ['unset-env', 'unset-server', 'unset-getenv']); |
115 | | - }, |
116 | | - ARRAY_FILTER_USE_KEY |
117 | | - ); |
118 | | - } |
119 | | - |
120 | | - private function parseTestMethodAnnotations(string $test): array |
121 | | - { |
122 | | - $parts = \preg_split('/ |::/', $test); |
123 | | - |
124 | | - if (!\class_exists($parts[0])) { |
125 | | - return []; |
126 | | - } |
127 | | - |
128 | | - // @see PHPUnit\Framework\TestCase::getAnnotations |
129 | | - return Test::parseTestMethodAnnotations(...$parts); |
| 15 | + $globalsBackup = new GlobalsBackup(); |
| 16 | + $globalsAnnotationReader = new GlobalsAnnotationReader(); |
| 17 | + $globalsRestoration = new GlobalsRestoration($globalsBackup); |
| 18 | + $facade->registerSubscribers($globalsBackup, $globalsAnnotationReader, $globalsRestoration); |
130 | 19 | } |
131 | 20 | } |
0 commit comments