diff --git a/README.md b/README.md
index d658d65..b1331ac 100644
--- a/README.md
+++ b/README.md
@@ -9,15 +9,8 @@ Supported attributes:
* `#[Server]` for `$_SERVER`
* `#[Putenv]` for [`putenv()`](http://php.net/putenv)
-Supported annotations:
-
- * `@env` and `@unset-env` for `$_ENV`
- * `@server` and `@unset-server` for `$_SERVER`
- * `@putenv` and `@unset-getenv` for [`putenv()`](http://php.net/putenv)
-
-> Annotations are deprecated since v3.1.0 and will be removed in v3.5.0.
-> The latest annotation support is planned to be removed is when PHPUnit 12 is released.
-> Annotation support is complete, so if you plan on using them keep using v3.4 of this package.
+> Annotations were previously supported up until v3.5.0 (inclusive).
+> Annotation support is complete, so if you plan on using them keep using v3.5 of this package.
Global variables are set before each test case is executed,
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:
```
-> If you are using a version before PHP 8.1 you can use the `AnnotationExtension` instead.
-
Make sure the `AttributeExtension` is **registered before** any other extensions that might depend on global variables.
Global variables can now be defined in attributes:
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index a6eb332..518b225 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -8,6 +8,6 @@
-
+
diff --git a/src/AnnotationExtension.php b/src/AnnotationExtension.php
deleted file mode 100644
index 0dc3287..0000000
--- a/src/AnnotationExtension.php
+++ /dev/null
@@ -1,23 +0,0 @@
-registerSubscribers($globalsBackup, $globalsAnnotationReader, $globalsRestoration);
- }
-}
diff --git a/src/GlobalsAnnotationReader.php b/src/GlobalsAnnotationReader.php
deleted file mode 100644
index 3c88b41..0000000
--- a/src/GlobalsAnnotationReader.php
+++ /dev/null
@@ -1,116 +0,0 @@
-readGlobalAnnotations($event->test());
- }
-
- private function readGlobalAnnotations(TestMethod $method)
- {
- $globalVars = $this->parseGlobalAnnotations($method);
-
- foreach ($globalVars['env'] as $name => $value) {
- $_ENV[$name] = $value;
- }
- foreach ($globalVars['server'] as $name => $value) {
- $_SERVER[$name] = $value;
- }
- foreach ($globalVars['putenv'] as $name => $value) {
- \putenv(\sprintf('%s=%s', $name, $value));
- }
-
- $unsetVars = $this->findUnsetVarAnnotations($method);
-
- foreach ($unsetVars['unset-env'] as $name) {
- unset($_ENV[$name]);
- }
- foreach ($unsetVars['unset-server'] as $name) {
- unset($_SERVER[$name]);
- }
- foreach ($unsetVars['unset-getenv'] as $name) {
- \putenv($name);
- }
- }
-
- private function parseGlobalAnnotations(TestMethod $method): array
- {
- return \array_map(function (array $annotations) {
- return \array_reduce($annotations, function ($carry, $annotation) {
- list($name, $value) = \strpos($annotation, '=') ? \explode('=', $annotation, 2) : [$annotation, ''];
- $carry[$name] = $value;
-
- return $carry;
- }, []);
- }, $this->findSetVarAnnotations($method));
- }
-
- private function findSetVarAnnotations(TestMethod $method): array
- {
- $annotations = $this->parseTestMethodAnnotations($method);
-
- return \array_filter(
- \array_merge_recursive(
- ['env' => [], 'server' => [], 'putenv' => []],
- $annotations['class'] ?? [],
- $annotations['method'] ?? []
- ),
- function (string $annotationName) {
- return \in_array($annotationName, ['env', 'server', 'putenv']);
- },
- ARRAY_FILTER_USE_KEY
- );
- }
-
- private function findUnsetVarAnnotations(TestMethod $method): array
- {
- $annotations = $this->parseTestMethodAnnotations($method);
-
- return \array_filter(
- \array_merge_recursive(
- ['unset-env' => [], 'unset-server' => [], 'unset-getenv' => []],
- $annotations['class'] ?? [],
- $annotations['method'] ?? []
- ),
- function (string $annotationName) {
- return \in_array($annotationName, ['unset-env', 'unset-server', 'unset-getenv']);
- },
- ARRAY_FILTER_USE_KEY
- );
- }
-
- // https://github.com/sebastianbergmann/phpunit/blob/9.5/src/Util/Test.php
- private function parseTestMethodAnnotations(TestMethod $method): array
- {
- $registry = Registry::getInstance();
- $className = $method->className();
- $methodName = $method->methodName();
-
- if (null !== $methodName) {
- try {
- return [
- 'method' => $registry->forMethod($className, $methodName)->symbolAnnotations(),
- 'class' => $registry->forClassName($className)->symbolAnnotations(),
- ];
- } catch (Exception $methodNotFound) {
- // ignored
- }
- }
-
- return [
- 'method' => null,
- 'class' => $registry->forClassName($className)->symbolAnnotations(),
- ];
- }
-}
diff --git a/src/GlobalsAttributeReader.php b/src/GlobalsAttributeReader.php
index 46ed978..df8390b 100644
--- a/src/GlobalsAttributeReader.php
+++ b/src/GlobalsAttributeReader.php
@@ -3,9 +3,15 @@
namespace Zalas\PHPUnit\Globals;
+use PHPUnit\Event\Code\Test;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Test\PreparationStarted;
use PHPUnit\Event\Test\PreparationStartedSubscriber;
+use ReflectionAttribute;
+use ReflectionClass;
+use ReflectionException;
+use ReflectionMethod;
+use RuntimeException;
use Zalas\PHPUnit\Globals\Attribute\Env;
use Zalas\PHPUnit\Globals\Attribute\Putenv;
use Zalas\PHPUnit\Globals\Attribute\Server;
@@ -17,7 +23,7 @@ public function notify(PreparationStarted $event): void
$this->readGlobalAttributes($event->test());
}
- private function readGlobalAttributes(TestMethod $method): void
+ private function readGlobalAttributes(Test $method): void
{
$attributes = $this->parseTestMethodAttributes($method);
$setVars = $this->findSetVarAttributes($attributes);
@@ -84,27 +90,32 @@ private function findUnsetVarAttributes(array $attributes): array
/**
* @return array
*/
- private function parseTestMethodAttributes(TestMethod $method): array
+ private function parseTestMethodAttributes(Test $method): array
{
+ if (!$method instanceof TestMethod) {
+ return [];
+ }
+
$className = $method->className();
$methodName = $method->methodName();
- $methodAttributes = null;
-
- if (null !== $methodName) {
+ try {
$methodAttributes = $this->collectGlobalsFromAttributes(
- (new \ReflectionMethod($className, $methodName))->getAttributes()
+ (new ReflectionMethod($className, $methodName))->getAttributes()
);
- }
- return \array_merge(
- $methodAttributes,
- $this->collectGlobalsFromAttributes((new \ReflectionClass($className))->getAttributes())
- );
+ return \array_merge(
+ $methodAttributes,
+ $this->collectGlobalsFromAttributes((new ReflectionClass($className))->getAttributes())
+ );
+ } catch (ReflectionException $e) {
+ // There would need to be a bug in PHPUnit for the ReflectionException to be thrown.
+ throw new RuntimeException("Failed to parse test method $className::$methodName", 0, $e);
+ }
}
/**
- * @param array<\ReflectionAttribute> $attributes
+ * @param array $attributes
* @return array
*/
private function collectGlobalsFromAttributes(array $attributes): array
diff --git a/src/GlobalsBackup.php b/src/GlobalsBackup.php
index 897bc67..621782a 100644
--- a/src/GlobalsBackup.php
+++ b/src/GlobalsBackup.php
@@ -7,9 +7,9 @@
final class GlobalsBackup implements PreparationStartedSubscriber
{
- private $server;
- private $env;
- private $getenv;
+ private array $server;
+ private array $env;
+ private array $getenv;
public function notify(PreparationStarted $event): void
{
diff --git a/tests/AnnotationExtensionTest.php b/tests/AnnotationExtensionTest.php
deleted file mode 100644
index 340c45c..0000000
--- a/tests/AnnotationExtensionTest.php
+++ /dev/null
@@ -1,134 +0,0 @@
- 'test_foo'], $_ENV);
- self::assertArraySubset(['APP_DEBUG' => '1'], $_SERVER);
- self::assertArraySubset(['APP_HOST' => 'dev'], \getenv());
- }
-
- public function test_it_reads_global_variables_from_class_annotations(): void
- {
- self::assertArraySubset(['APP_ENV' => 'test'], $_ENV);
- self::assertArraySubset(['APP_DEBUG' => '0'], $_SERVER);
- self::assertArraySubset(['APP_HOST' => 'localhost'], \getenv());
- }
-
- /**
- * @env FOO=foo
- * @server BAR=bar
- * @putenv BAZ=baz
- */
- public function test_it_reads_additional_global_variables_from_methods(): void
- {
- self::assertArraySubset(['APP_ENV' => 'test'], $_ENV);
- self::assertArraySubset(['APP_DEBUG' => '0'], $_SERVER);
- self::assertArraySubset(['APP_HOST' => 'localhost'], \getenv());
- self::assertArraySubset(['FOO' => 'foo'], $_ENV);
- self::assertArraySubset(['BAR' => 'bar'], $_SERVER);
- self::assertArraySubset(['BAZ' => 'baz'], \getenv());
- }
-
- /**
- * @env APP_ENV=test_foo
- * @env APP_ENV=test_foo_bar
- * @server APP_DEBUG=1
- * @server APP_DEBUG=2
- * @putenv APP_HOST=host1
- * @putenv APP_HOST=host2
- */
- public function test_it_reads_the_latest_var_defined(): void
- {
- self::assertArraySubset(['APP_ENV' => 'test_foo_bar'], $_ENV);
- self::assertArraySubset(['APP_DEBUG' => '2'], $_SERVER);
- self::assertArraySubset(['APP_HOST' => 'host2'], \getenv());
- }
-
- /**
- * @env APP_ENV
- * @server APP_DEBUG
- * @putenv APP_HOST
- */
- public function test_it_reads_empty_vars(): void
- {
- self::assertArraySubset(['APP_ENV' => ''], $_ENV);
- self::assertArraySubset(['APP_DEBUG' => ''], $_SERVER);
- self::assertArraySubset(['APP_HOST' => ''], \getenv());
- }
-
- /**
- * @unset-env APP_ENV
- * @unset-server APP_DEBUG
- * @unset-getenv APP_HOST
- * @unset-env USER
- */
- public function test_it_unsets_vars(): void
- {
- $this->assertArrayNotHasKey('USER', $_ENV);
- $this->assertArrayNotHasKey('APP_ENV', $_ENV);
- $this->assertArrayNotHasKey('APP_DEBUG', $_SERVER);
- $this->assertArrayNotHasKey('APP_HOST', \getenv());
- }
-
- public function test_it_backups_the_state(): void
- {
- // this test is only here so the next one could verify the state is brought back
-
- $_ENV['FOO'] = 'env_foo';
- $_SERVER['BAR'] = 'server_bar';
- \putenv('FOO=putenv_foo');
- \putenv('USER=foobar');
-
- $this->assertArrayHasKey('FOO', $_ENV);
- $this->assertArrayHasKey('BAR', $_SERVER);
- $this->assertSame('putenv_foo', \getenv('FOO'));
- $this->assertSame('foobar', \getenv('USER'));
- }
-
- #[Depends('test_it_backups_the_state')]
- public function test_it_cleans_up_after_itself(): void
- {
- $this->assertArrayNotHasKey('FOO', $_ENV);
- $this->assertArrayNotHasKey('BAR', $_SERVER);
- $this->assertFalse(\getenv('FOO'), 'It removes environment variables initialised in a test.');
- $this->assertNotSame('foobar', \getenv('USER'), 'It restores environment variables changed in a test.');
- $this->assertNotFalse(\getenv('USER'), 'It restores environment variables changed in a test.');
- }
-
- /**
- * Provides a replacement for the assertion deprecated in PHPUnit 8 and removed in PHPUnit 9.
- * @param array $subset
- * @param array $array
- */
- public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void
- {
- self::assertSame($array, \array_replace_recursive($array, $subset));
- }
-}
diff --git a/tests/Stub/CombinedExtension.php b/tests/Stub/CombinedExtension.php
deleted file mode 100644
index 24d0f2c..0000000
--- a/tests/Stub/CombinedExtension.php
+++ /dev/null
@@ -1,30 +0,0 @@
-registerSubscribers(
- $globalsBackup,
- $globalsAttributeReader,
- $globalsAnnotationReader,
- $globalsRestoration
- );
- }
-}
diff --git a/tests/phar/phpunit.xml b/tests/phar/phpunit.xml
index c06cb45..b8c17be 100644
--- a/tests/phar/phpunit.xml
+++ b/tests/phar/phpunit.xml
@@ -11,6 +11,6 @@
-
+
diff --git a/tests/phar/tests/PharTest.php b/tests/phar/tests/PharTest.php
index cd01fa7..9bc5b3a 100644
--- a/tests/phar/tests/PharTest.php
+++ b/tests/phar/tests/PharTest.php
@@ -7,16 +7,6 @@
class PharTest extends TestCase
{
- /**
- * @env APP_ENV=test_foo
- * @server APP_DEBUG=1
- */
- public function test_it_reads_global_variables_from_method_annotations()
- {
- $this->assertArraySubset(['APP_ENV' => 'test_foo'], $_ENV);
- $this->assertArraySubset(['APP_DEBUG' => '1'], $_SERVER);
- }
-
#[Env("APP_ENV", "test_foo")]
#[Server("APP_DEBUG", "1")]
public function test_it_reads_global_variables_from_method_attributes()