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