Skip to content

Commit 49e3d6f

Browse files
authored
Merge pull request #54 from jakzal/drop-annotations
Drop annotation support
2 parents db46362 + af198b0 commit 49e3d6f

10 files changed

+30
-341
lines changed

README.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,8 @@ Supported attributes:
99
* `#[Server]` for `$_SERVER`
1010
* `#[Putenv]` for [`putenv()`](http://php.net/putenv)
1111

12-
Supported annotations:
13-
14-
* `@env` and `@unset-env` for `$_ENV`
15-
* `@server` and `@unset-server` for `$_SERVER`
16-
* `@putenv` and `@unset-getenv` for [`putenv()`](http://php.net/putenv)
17-
18-
> Annotations are deprecated since v3.1.0 and will be removed in v3.5.0.
19-
> The latest annotation support is planned to be removed is when PHPUnit 12 is released.
20-
> Annotation support is complete, so if you plan on using them keep using v3.4 of this package.
12+
> Annotations were previously supported up until v3.5.0 (inclusive).
13+
> Annotation support is complete, so if you plan on using them keep using v3.5 of this package.
2114
2215
Global variables are set before each test case is executed,
2316
and brought to the original state after each test case has finished.
@@ -62,8 +55,6 @@ Enable the globals attribute extension in your PHPUnit configuration:
6255
</phpunit>
6356
```
6457

65-
> If you are using a version before PHP 8.1 you can use the `AnnotationExtension` instead.
66-
6758
Make sure the `AttributeExtension` is **registered before** any other extensions that might depend on global variables.
6859

6960
Global variables can now be defined in attributes:

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
<env name="USER" value="test" force="true"/>
99
</php>
1010
<extensions>
11-
<bootstrap class="Zalas\PHPUnit\Globals\Tests\Stub\CombinedExtension" />
11+
<bootstrap class="Zalas\PHPUnit\Globals\AttributeExtension" />
1212
</extensions>
1313
</phpunit>

src/AnnotationExtension.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/GlobalsAnnotationReader.php

Lines changed: 0 additions & 116 deletions
This file was deleted.

src/GlobalsAttributeReader.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33

44
namespace Zalas\PHPUnit\Globals;
55

6+
use PHPUnit\Event\Code\Test;
67
use PHPUnit\Event\Code\TestMethod;
78
use PHPUnit\Event\Test\PreparationStarted;
89
use PHPUnit\Event\Test\PreparationStartedSubscriber;
10+
use ReflectionAttribute;
11+
use ReflectionClass;
12+
use ReflectionException;
13+
use ReflectionMethod;
14+
use RuntimeException;
915
use Zalas\PHPUnit\Globals\Attribute\Env;
1016
use Zalas\PHPUnit\Globals\Attribute\Putenv;
1117
use Zalas\PHPUnit\Globals\Attribute\Server;
@@ -17,7 +23,7 @@ public function notify(PreparationStarted $event): void
1723
$this->readGlobalAttributes($event->test());
1824
}
1925

20-
private function readGlobalAttributes(TestMethod $method): void
26+
private function readGlobalAttributes(Test $method): void
2127
{
2228
$attributes = $this->parseTestMethodAttributes($method);
2329
$setVars = $this->findSetVarAttributes($attributes);
@@ -84,27 +90,32 @@ private function findUnsetVarAttributes(array $attributes): array
8490
/**
8591
* @return array<Env|Putenv|Server>
8692
*/
87-
private function parseTestMethodAttributes(TestMethod $method): array
93+
private function parseTestMethodAttributes(Test $method): array
8894
{
95+
if (!$method instanceof TestMethod) {
96+
return [];
97+
}
98+
8999
$className = $method->className();
90100
$methodName = $method->methodName();
91101

92-
$methodAttributes = null;
93-
94-
if (null !== $methodName) {
102+
try {
95103
$methodAttributes = $this->collectGlobalsFromAttributes(
96-
(new \ReflectionMethod($className, $methodName))->getAttributes()
104+
(new ReflectionMethod($className, $methodName))->getAttributes()
97105
);
98-
}
99106

100-
return \array_merge(
101-
$methodAttributes,
102-
$this->collectGlobalsFromAttributes((new \ReflectionClass($className))->getAttributes())
103-
);
107+
return \array_merge(
108+
$methodAttributes,
109+
$this->collectGlobalsFromAttributes((new ReflectionClass($className))->getAttributes())
110+
);
111+
} catch (ReflectionException $e) {
112+
// There would need to be a bug in PHPUnit for the ReflectionException to be thrown.
113+
throw new RuntimeException("Failed to parse test method $className::$methodName", 0, $e);
114+
}
104115
}
105116

106117
/**
107-
* @param array<\ReflectionAttribute> $attributes
118+
* @param array<ReflectionAttribute> $attributes
108119
* @return array<Env|Putenv|Server>
109120
*/
110121
private function collectGlobalsFromAttributes(array $attributes): array

src/GlobalsBackup.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
final class GlobalsBackup implements PreparationStartedSubscriber
99
{
10-
private $server;
11-
private $env;
12-
private $getenv;
10+
private array $server;
11+
private array $env;
12+
private array $getenv;
1313

1414
public function notify(PreparationStarted $event): void
1515
{

tests/AnnotationExtensionTest.php

Lines changed: 0 additions & 134 deletions
This file was deleted.

0 commit comments

Comments
 (0)