|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Zalas\PHPUnit\Globals; |
| 5 | + |
| 6 | +use PHPUnit\Runner\AfterTestHook; |
| 7 | +use PHPUnit\Runner\BeforeTestHook; |
| 8 | +use Zalas\PHPUnit\Globals\Attribute\Env; |
| 9 | +use Zalas\PHPUnit\Globals\Attribute\Putenv; |
| 10 | +use Zalas\PHPUnit\Globals\Attribute\Server; |
| 11 | + |
| 12 | +final class AttributeExtension implements BeforeTestHook, AfterTestHook |
| 13 | +{ |
| 14 | + private $server; |
| 15 | + private $env; |
| 16 | + private $getenv; |
| 17 | + |
| 18 | + public function executeBeforeTest(string $test): void |
| 19 | + { |
| 20 | + $this->backupGlobals(); |
| 21 | + $this->readGlobalAttributes($test); |
| 22 | + } |
| 23 | + |
| 24 | + public function executeAfterTest(string $test, float $time): void |
| 25 | + { |
| 26 | + $this->restoreGlobals(); |
| 27 | + } |
| 28 | + |
| 29 | + private function backupGlobals(): void |
| 30 | + { |
| 31 | + $this->server = $_SERVER; |
| 32 | + $this->env = $_ENV; |
| 33 | + $this->getenv = \getenv(); |
| 34 | + } |
| 35 | + |
| 36 | + private function restoreGlobals(): void |
| 37 | + { |
| 38 | + $_SERVER = $this->server; |
| 39 | + $_ENV = $this->env; |
| 40 | + |
| 41 | + foreach (\array_diff_assoc($this->getenv, \getenv()) as $name => $value) { |
| 42 | + \putenv(\sprintf('%s=%s', $name, $value)); |
| 43 | + } |
| 44 | + foreach (\array_diff_assoc(\getenv(), $this->getenv) as $name => $value) { |
| 45 | + \putenv($name); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private function readGlobalAttributes(string $test) |
| 50 | + { |
| 51 | + $globalVars = $this->parseGlobalAttributes($test); |
| 52 | + |
| 53 | + foreach ($globalVars['env'] as $name => $value) { |
| 54 | + $_ENV[$name] = $value; |
| 55 | + } |
| 56 | + foreach ($globalVars['server'] as $name => $value) { |
| 57 | + $_SERVER[$name] = $value; |
| 58 | + } |
| 59 | + foreach ($globalVars['putenv'] as $name => $value) { |
| 60 | + \putenv(\sprintf('%s=%s', $name, $value)); |
| 61 | + } |
| 62 | + |
| 63 | + $unsetVars = $this->findUnsetVarAttributes($test); |
| 64 | + |
| 65 | + foreach ($unsetVars['unset-env'] as $name) { |
| 66 | + unset($_ENV[$name]); |
| 67 | + } |
| 68 | + foreach ($unsetVars['unset-server'] as $name) { |
| 69 | + unset($_SERVER[$name]); |
| 70 | + } |
| 71 | + foreach ($unsetVars['unset-getenv'] as $name) { |
| 72 | + \putenv($name); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private function parseGlobalAttributes(string $test): array |
| 77 | + { |
| 78 | + $globals = ['env' => [], 'server' => [], 'putenv' => []]; |
| 79 | + |
| 80 | + $attributes = $this->findSetVarAttributes($test); |
| 81 | + foreach ($attributes as $attribute) { |
| 82 | + match (true) { |
| 83 | + $attribute instanceof Env => $globals['env'][$attribute->name] = $attribute->value, |
| 84 | + $attribute instanceof Server => $globals['server'][$attribute->name] = $attribute->value, |
| 85 | + $attribute instanceof Putenv => $globals['putenv'][$attribute->name] = $attribute->value, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + return $globals; |
| 90 | + } |
| 91 | + |
| 92 | + private function findSetVarAttributes(string $test): array |
| 93 | + { |
| 94 | + $attributes = $this->parseTestMethodAttributes($test); |
| 95 | + |
| 96 | + $attributes = \array_filter( |
| 97 | + $attributes, |
| 98 | + static fn (Env|Server|Putenv $attribute) => false === $attribute->unset, |
| 99 | + ARRAY_FILTER_USE_BOTH |
| 100 | + ); |
| 101 | + |
| 102 | + \usort($attributes, static fn (Env|Server|Putenv $a, Env|Server|Putenv $b) => $a->getTarget() <=> $b->getTarget()); |
| 103 | + |
| 104 | + return $attributes; |
| 105 | + } |
| 106 | + |
| 107 | + private function findUnsetVarAttributes(string $test): array |
| 108 | + { |
| 109 | + $unset = ['unset-env' => [], 'unset-server' => [], 'unset-getenv' => []]; |
| 110 | + |
| 111 | + $attributes = $this->parseTestMethodAttributes($test); |
| 112 | + foreach ($attributes as $attribute) { |
| 113 | + if (false === $attribute->unset) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + match (true) { |
| 118 | + $attribute instanceof Env => $unset['unset-env'][] = $attribute->name, |
| 119 | + $attribute instanceof Server => $unset['unset-server'][] = $attribute->name, |
| 120 | + $attribute instanceof Putenv => $unset['unset-getenv'][] = $attribute->name, |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + return $unset; |
| 125 | + } |
| 126 | + |
| 127 | + private function parseTestMethodAttributes(string $test): array |
| 128 | + { |
| 129 | + $parts = \preg_split('/ |::/', $test); |
| 130 | + |
| 131 | + if (!\class_exists($parts[0])) { |
| 132 | + return []; |
| 133 | + } |
| 134 | + |
| 135 | + $methodAttributes = []; |
| 136 | + if (!empty($parts[1])) { |
| 137 | + $methodAttributes = $this->collectGlobalsFromAttributes( |
| 138 | + (new \ReflectionMethod($parts[0], $parts[1]))->getAttributes() |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + return \array_merge( |
| 143 | + $this->collectGlobalsFromAttributes((new \ReflectionClass($parts[0]))->getAttributes()), |
| 144 | + $methodAttributes, |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + private function collectGlobalsFromAttributes(array $attributes): array |
| 149 | + { |
| 150 | + $globals = []; |
| 151 | + |
| 152 | + foreach ($attributes as $attribute) { |
| 153 | + if (!\str_starts_with($attribute->getName(), 'Zalas\\PHPUnit\\Globals\\Attribute\\')) { |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + /** @var Env|Server|Putenv $instance */ |
| 158 | + $instance = $attribute->newInstance(); |
| 159 | + $globals[] = $instance->withTarget($attribute->getTarget()); |
| 160 | + } |
| 161 | + |
| 162 | + return $globals; |
| 163 | + } |
| 164 | +} |
0 commit comments