Skip to content

Commit 9b69242

Browse files
authored
Merge pull request #42 from jdecool/fix-tests-2.6
Fix tests on 2.6 branch
2 parents c623f14 + 2556286 commit 9b69242

10 files changed

+319
-265
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<env name="USER" value="test" force="true"/>
1414
</php>
1515
<extensions>
16-
<extension class="Zalas\PHPUnit\Globals\AnnotationExtension"/>
17-
<extension class="Zalas\PHPUnit\Globals\AttributeExtension"/>
16+
<extension class="Zalas\PHPUnit\Globals\Tests\Stub\CombinedExtension"/>
1817
</extensions>
1918
</phpunit>

src/AnnotationExtension.php

Lines changed: 3 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3,130 +3,19 @@
33

44
namespace Zalas\PHPUnit\Globals;
55

6-
use PHPUnit\Framework\TestCase;
76
use PHPUnit\Runner\AfterTestHook;
87
use PHPUnit\Runner\BeforeTestHook;
9-
use PHPUnit\Util\Test;
108

119
class AnnotationExtension implements BeforeTestHook, AfterTestHook
1210
{
13-
private $server;
14-
private $env;
15-
private $getenv;
16-
1711
public function executeBeforeTest(string $test): void
1812
{
19-
$this->backupGlobals();
20-
$this->readGlobalAnnotations($test);
13+
GlobalsContainer::getInstance()->backupGlobals();
14+
AnnotationReader::getInstance()->readGlobalAnnotations($test);
2115
}
2216

2317
public function executeAfterTest(string $test, float $time): void
2418
{
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();
13120
}
13221
}

src/AnnotationReader.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Zalas\PHPUnit\Globals;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use PHPUnit\Util\Test;
9+
10+
class AnnotationReader
11+
{
12+
use SingletonTrait;
13+
14+
public function readGlobalAnnotations(string $test)
15+
{
16+
$globalVars = $this->parseGlobalAnnotations($test);
17+
18+
foreach ($globalVars['env'] as $name => $value) {
19+
$_ENV[$name] = $value;
20+
}
21+
foreach ($globalVars['server'] as $name => $value) {
22+
$_SERVER[$name] = $value;
23+
}
24+
foreach ($globalVars['putenv'] as $name => $value) {
25+
\putenv(\sprintf('%s=%s', $name, $value));
26+
}
27+
28+
$unsetVars = $this->findUnsetVarAnnotations($test);
29+
30+
foreach ($unsetVars['unset-env'] as $name) {
31+
unset($_ENV[$name]);
32+
}
33+
foreach ($unsetVars['unset-server'] as $name) {
34+
unset($_SERVER[$name]);
35+
}
36+
foreach ($unsetVars['unset-getenv'] as $name) {
37+
\putenv($name);
38+
}
39+
}
40+
41+
private function parseGlobalAnnotations(string $test): array
42+
{
43+
return \array_map(function (array $annotations) {
44+
return \array_reduce($annotations, function ($carry, $annotation) {
45+
list($name, $value) = \strpos($annotation, '=') ? \explode('=', $annotation, 2) : [$annotation, ''];
46+
$carry[$name] = $value;
47+
48+
return $carry;
49+
}, []);
50+
}, $this->findSetVarAnnotations($test));
51+
}
52+
53+
private function findSetVarAnnotations(string $test): array
54+
{
55+
$annotations = $this->parseTestMethodAnnotations($test);
56+
57+
return \array_filter(
58+
\array_merge_recursive(
59+
['env' => [], 'server' => [], 'putenv' => []],
60+
$annotations['class'] ?? [],
61+
$annotations['method'] ?? []
62+
),
63+
function (string $annotationName) {
64+
return \in_array($annotationName, ['env', 'server', 'putenv']);
65+
},
66+
ARRAY_FILTER_USE_KEY
67+
);
68+
}
69+
70+
private function findUnsetVarAnnotations(string $test): array
71+
{
72+
$annotations = $this->parseTestMethodAnnotations($test);
73+
74+
return \array_filter(
75+
\array_merge_recursive(
76+
['unset-env' => [], 'unset-server' => [], 'unset-getenv' => []],
77+
$annotations['class'] ?? [],
78+
$annotations['method'] ?? []
79+
),
80+
function (string $annotationName) {
81+
return \in_array($annotationName, ['unset-env', 'unset-server', 'unset-getenv']);
82+
},
83+
ARRAY_FILTER_USE_KEY
84+
);
85+
}
86+
87+
private function parseTestMethodAnnotations(string $test): array
88+
{
89+
$parts = \preg_split('/ |::/', $test);
90+
91+
if (!\class_exists($parts[0]) || !\is_subclass_of($parts[0], TestCase::class)) {
92+
return [];
93+
}
94+
95+
// @see PHPUnit\Framework\TestCase::getAnnotations
96+
return Test::parseTestMethodAnnotations(...$parts);
97+
}
98+
}

0 commit comments

Comments
 (0)