-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
GlobalsAttributeReader.php
126 lines (104 loc) · 3.83 KB
/
GlobalsAttributeReader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace Zalas\PHPUnit\Globals;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Test\PreparationStarted;
use PHPUnit\Event\Test\PreparationStartedSubscriber;
use Zalas\PHPUnit\Globals\Attribute\Env;
use Zalas\PHPUnit\Globals\Attribute\Putenv;
use Zalas\PHPUnit\Globals\Attribute\Server;
final class GlobalsAttributeReader implements PreparationStartedSubscriber
{
public function notify(PreparationStarted $event): void
{
$this->readGlobalAttributes($event->test());
}
private function readGlobalAttributes(TestMethod $method): void
{
$attributes = $this->parseTestMethodAttributes($method);
$setVars = $this->findSetVarAttributes($attributes);
foreach ($setVars as $var) {
match (true) {
$var instanceof Env => $_ENV[$var->name] = $var->value,
$var instanceof Server => $_SERVER[$var->name] = $var->value,
$var instanceof Putenv => \putenv(\sprintf('%s=%s', $var->name, $var->value))
};
}
$unsetVars = $this->findUnsetVarAttributes($attributes);
foreach ($unsetVars as $var) {
$callable = match (true) {
$var instanceof Env => static function ($var) {
unset($_ENV[$var->name]);
},
$var instanceof Server => static function ($var) {
unset($_SERVER[$var->name]);
},
$var instanceof Putenv => static function ($var) {
\putenv($var->name);
}
};
$callable($var);
}
}
/**
* @param array<Env|Putenv|Server> $attributes
*/
private function findSetVarAttributes(array $attributes): array
{
$attributes = \array_filter(
$attributes,
static fn (Env|Server|Putenv $attribute) => false === $attribute->unset,
ARRAY_FILTER_USE_BOTH
);
\usort($attributes, static fn (Env|Server|Putenv $a, Env|Server|Putenv $b) => $a->getTarget() <=> $b->getTarget());
return $attributes;
}
/**
* @param array<Env|Putenv|Server> $attributes
*/
private function findUnsetVarAttributes(array $attributes): array
{
$attributes = \array_filter(
$attributes,
static fn (Env|Server|Putenv $attribute) => true === $attribute->unset,
ARRAY_FILTER_USE_BOTH
);
\usort($attributes, static fn (Env|Server|Putenv $a, Env|Server|Putenv $b) => $a->getTarget() <=> $b->getTarget());
return $attributes;
}
/**
* @return array<Env|Putenv|Server>
*/
private function parseTestMethodAttributes(TestMethod $method): array
{
$className = $method->className();
$methodName = $method->methodName();
$methodAttributes = null;
if (null !== $methodName) {
$methodAttributes = $this->collectGlobalsFromAttributes(
(new \ReflectionMethod($className, $methodName))->getAttributes()
);
}
return \array_merge(
$methodAttributes,
$this->collectGlobalsFromAttributes((new \ReflectionClass($className))->getAttributes())
);
}
/**
* @param array<\ReflectionAttribute> $attributes
* @return array<Env|Putenv|Server>
*/
private function collectGlobalsFromAttributes(array $attributes): array
{
$globals = [];
foreach ($attributes as $attribute) {
if (!\str_starts_with($attribute->getName(), 'Zalas\\PHPUnit\\Globals\\Attribute\\')) {
continue;
}
/** @var Env|Server|Putenv $instance */
$instance = $attribute->newInstance();
$globals[] = $instance->withTarget($attribute->getTarget());
}
return $globals;
}
}