Skip to content

Commit 45e4277

Browse files
authored
Merge pull request #31 from jakzal/phpunit-10
Make the extension work with the new event system in PHPUnit 10
2 parents 8f60a69 + 181024b commit 45e4277

13 files changed

+210
-183
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ jobs:
1515
name: Build and test
1616
strategy:
1717
matrix:
18-
php: [8.0, 8.1, 8.2]
18+
php: [8.1, 8.2]
1919
deps: [high]
2020
include:
21-
- php: 8.0
21+
- php: 8.1
2222
deps: low
2323

2424
steps:
@@ -45,7 +45,7 @@ jobs:
4545

4646
- name: Upload the phar
4747
uses: actions/upload-artifact@v1
48-
if: matrix.php == '8.0' && matrix.deps == 'high'
48+
if: matrix.php == '8.1' && matrix.deps == 'high'
4949
with:
5050
name: zalas-phpunit-globals-extension.phar
5151
path: build/zalas-phpunit-globals-extension.phar
@@ -56,8 +56,7 @@ jobs:
5656
needs: tests
5757
strategy:
5858
matrix:
59-
php: [8.0, 8.1, 8.2]
60-
59+
php: [8.1, 8.2]
6160
steps:
6261
- uses: actions/checkout@v3
6362

Makefile

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ cs: tools/php-cs-fixer
3535
cs-fix: tools/php-cs-fixer
3636
PHP_CS_FIXER_IGNORE_ENV=1 tools/php-cs-fixer --allow-risky=yes --no-interaction --ansi fix
3737

38-
phpunit: tools/phpunit
39-
tools/phpunit
38+
phpunit: vendor
39+
vendor/bin/phpunit
4040
.PHONY: phpunit
4141

4242
clean:
@@ -55,7 +55,7 @@ build/zalas-phpunit-globals-extension.phar: tools/box
5555

5656
cd build/phar && \
5757
composer remove phpunit/phpunit --no-update && \
58-
composer config platform.php 8.0 && \
58+
composer config platform.php 8.1 && \
5959
composer update --no-dev -o -a
6060

6161
tools/box compile
@@ -69,23 +69,20 @@ vendor: install
6969

7070
vendor/bin/phpunit: install
7171

72-
tools: tools/php-cs-fixer tools/phpunit tools/box
72+
tools: tools/php-cs-fixer tools/box
7373
.PHONY: tools
7474

75-
tools/phpunit: vendor/bin/phpunit
76-
ln -sf ../vendor/bin/phpunit tools/phpunit
77-
7875
tools/php-cs-fixer:
7976
curl -Ls http://cs.symfony.com/download/php-cs-fixer-v3.phar -o tools/php-cs-fixer && chmod +x tools/php-cs-fixer
8077

8178
tools/box:
82-
curl -Ls https://github.com/humbug/box/releases/download/3.13.0/box.phar -o tools/box && chmod +x tools/box
79+
curl -Ls https://github.com/humbug/box/releases/download/4.2.0/box.phar -o tools/box && chmod +x tools/box
8380

8481
test-package-tools: tests/phar/tools/phpunit tests/phar/tools/phpunit.d/zalas-phpunit-globals-extension.phar
8582
.PHONY: test-package-tools
8683

87-
tests/phar/tools/phpunit:
88-
curl -Ls https://phar.phpunit.de/phpunit-9.5.10.phar -o tests/phar/tools/phpunit && chmod +x tests/phar/tools/phpunit
84+
tests/phar/tools/phpunit: vendor
85+
ln -sf $(CURDIR)/vendor/bin/phpunit tests/phar/tools/phpunit
8986

9087
tests/phar/tools/phpunit.d/zalas-phpunit-globals-extension.phar: build/zalas-phpunit-globals-extension.phar
9188
cp build/zalas-phpunit-globals-extension.phar tests/phar/tools/phpunit.d/zalas-phpunit-globals-extension.phar

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Put the extension in your PHPUnit extensions directory.
3131
Remember to instruct PHPUnit to load extensions in your `phpunit.xml`:
3232

3333
```xml
34-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.0/phpunit.xsd"
34+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file://./vendor/phpunit/phpunit/phpunit.xsd"
3535
extensionsDirectory="tools/phpunit.d"
3636
>
3737
</phpunit>
@@ -44,15 +44,14 @@ Enable the globals annotation extension in your PHPUnit configuration:
4444
```xml
4545
<?xml version="1.0" encoding="UTF-8"?>
4646
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
47-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.0/phpunit.xsd"
47+
xsi:noNamespaceSchemaLocation="file://./vendor/phpunit/phpunit/phpunit.xsd"
4848
bootstrap="vendor/autoload.php"
49-
verbose="true"
5049
colors="true">
5150

5251
<!-- ... -->
5352

5453
<extensions>
55-
<extension class="Zalas\PHPUnit\Globals\AnnotationExtension" />
54+
<bootstrap class="Zalas\PHPUnit\Globals\AnnotationExtension" />
5655
</extensions>
5756

5857
</phpunit>
@@ -109,23 +108,22 @@ class ExampleTest extends TestCase
109108
}
110109
}
111110
```
111+
## Updating to PHPUnit 10
112112

113-
## Updating to PHPUnit 8
114-
115-
When updating from a previous version of this extension that used to work with PHPUnit older than v8,
116-
replace the listener registration in `phpunit.xml`:
113+
When updating from a previous version of this extension dedicated to work with PHPUnit 9,
114+
replace the extension registration in `phpunit.xml`:
117115

118116
```xml
119-
<listeners>
120-
<listener class="Zalas\PHPUnit\Globals\AnnotationListener" />
121-
</listeners>
117+
<extensions>
118+
<extension class="Zalas\PHPUnit\Globals\AnnotationExtension" />
119+
</extensions>
122120
```
123121

124-
with the extension registration:
122+
with:
125123

126124
```xml
127125
<extensions>
128-
<extension class="Zalas\PHPUnit\Globals\AnnotationExtension" />
126+
<bootstrap class="Zalas\PHPUnit\Globals\AnnotationExtension" />
129127
</extensions>
130128
```
131129

composer.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
}
1111
],
1212
"require": {
13-
"php": "~8.0.0 || ~8.1.0 || ~8.2.0",
14-
"phpunit/phpunit": "^9.0"
15-
},
16-
"conflict": {
17-
"phpunit/phpunit": "9.5.0"
13+
"php": "~8.1.0 || ~8.2.0",
14+
"phpunit/phpunit": "^10.0"
1815
},
1916
"autoload": {
2017
"psr-4": {
@@ -26,9 +23,11 @@
2623
"Zalas\\PHPUnit\\Globals\\Tests\\": "tests/"
2724
}
2825
},
26+
"prefer-stable": true,
27+
"minimum-stability": "dev",
2928
"extra": {
3029
"branch-alias": {
31-
"dev-master": "2.x-dev"
30+
"dev-master": "3.x-dev"
3231
}
3332
}
3433
}

manifest.xml.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<phar xmlns="https://phar.io/xml/manifest/1.0">
33
<contains name="zalas/phpunit-globals" version="@@version@@" type="extension">
4-
<extension for="phpunit/phpunit" compatible="^9.0"/>
4+
<extension for="phpunit/phpunit" compatible="^10.0"/>
55
</contains>
66

77
<copyright>
@@ -10,6 +10,6 @@
1010
</copyright>
1111

1212
<requires>
13-
<php version="^8.0"/>
13+
<php version="^8.1"/>
1414
</requires>
1515
</phar>

phpunit.xml.dist

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" beStrictAboutOutputDuringTests="true" beStrictAboutTodoAnnotatedTests="true" colors="true" verbose="true">
3-
<coverage processUncoveredFiles="true">
4-
<include>
5-
<directory suffix=".php">src</directory>
6-
</include>
7-
</coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file://./vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" beStrictAboutOutputDuringTests="true" beStrictAboutTodoAnnotatedTests="true" colors="true">
83
<testsuite name="default">
94
<directory suffix="Test.php">tests</directory>
105
<exclude>tests/phar</exclude>
@@ -13,6 +8,6 @@
138
<env name="USER" value="test" force="true"/>
149
</php>
1510
<extensions>
16-
<extension class="Zalas\PHPUnit\Globals\AnnotationExtension"/>
11+
<bootstrap class="Zalas\PHPUnit\Globals\AnnotationExtension" />
1712
</extensions>
1813
</phpunit>

src/AnnotationExtension.php

Lines changed: 10 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -3,129 +3,18 @@
33

44
namespace Zalas\PHPUnit\Globals;
55

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;
910

10-
class AnnotationExtension implements BeforeTestHook, AfterTestHook
11+
final class AnnotationExtension implements Extension
1112
{
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
7514
{
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);
13019
}
13120
}

0 commit comments

Comments
 (0)