Skip to content

Drop annotation support #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -62,8 +55,6 @@ Enable the globals attribute extension in your PHPUnit configuration:
</phpunit>
```

> 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:
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<env name="USER" value="test" force="true"/>
</php>
<extensions>
<bootstrap class="Zalas\PHPUnit\Globals\Tests\Stub\CombinedExtension" />
<bootstrap class="Zalas\PHPUnit\Globals\AttributeExtension" />
</extensions>
</phpunit>
23 changes: 0 additions & 23 deletions src/AnnotationExtension.php

This file was deleted.

116 changes: 0 additions & 116 deletions src/GlobalsAnnotationReader.php

This file was deleted.

35 changes: 23 additions & 12 deletions src/GlobalsAttributeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -84,27 +90,32 @@ private function findUnsetVarAttributes(array $attributes): array
/**
* @return array<Env|Putenv|Server>
*/
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<ReflectionAttribute> $attributes
* @return array<Env|Putenv|Server>
*/
private function collectGlobalsFromAttributes(array $attributes): array
Expand Down
6 changes: 3 additions & 3 deletions src/GlobalsBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
134 changes: 0 additions & 134 deletions tests/AnnotationExtensionTest.php

This file was deleted.

Loading