@@ -10,6 +10,12 @@ Supported annotations:
1010 * ` @server ` for ` $_SERVER `
1111 * ` @putenv ` for [ ` putenv() ` ] ( http://php.net/putenv )
1212
13+ Supported attributes:
14+
15+ * ` #[Env] ` for ` $_ENV `
16+ * ` #[Server] ` for ` $_SERVER `
17+ * ` #[Putenv] ` for [ ` putenv() ` ] ( http://php.net/putenv )
18+
1319Global variables are set before each test case is executed,
1420and brought to the original state after each test case has finished.
1521The same applies to ` putenv() ` /` getenv() ` calls.
@@ -52,13 +58,14 @@ Enable the globals annotation extension in your PHPUnit configuration:
5258 <!-- ... -->
5359
5460 <extensions >
55- <extension class =" Zalas\PHPUnit\Globals\AnnotationExtension" />
61+ <extension class =" Zalas\PHPUnit\Globals\AnnotationExtension" /> <!-- if you want to use annotations -->
62+ <extension class =" Zalas\PHPUnit\Globals\AttributeExtension" /> <!-- if you want to use attributes -->
5663 </extensions >
5764
5865</phpunit >
5966```
6067
61- Make sure the ` AnnotationExtension ` is registered before any other extensions that might depend on global variables.
68+ Make sure the ` AnnotationExtension ` or ` AttributeExtension ` is registered before any other extensions that might depend on global variables.
6269
6370Global variables can now be defined in annotations:
6471
@@ -89,6 +96,34 @@ class ExampleTest extends TestCase
8996}
9097```
9198
99+ Global variables can also be defined with attributes:
100+
101+ ``` php
102+ use PHPUnit\Framework\TestCase;
103+ use Zalas\PHPUnit\Globals\Attribute\Env;
104+ use Zalas\PHPUnit\Globals\Attribute\Server;
105+ use Zalas\PHPUnit\Globals\Attribute\Putenv;
106+
107+ #[Env('FOO=bar')]
108+ class ExampleTest extends TestCase
109+ {
110+ #[Env('APP_ENV=foo')]
111+ #[Env('APP_DEBUG=0')]
112+ #[Server('APP_ENV=bar')]
113+ #[Server('APP_DEBUG=1')]
114+ #[Putenv('APP_HOST=localhost')]
115+ public function test_global_variables()
116+ {
117+ $this->assertSame('bar', $_ENV['FOO']);
118+ $this->assertSame('foo', $_ENV['APP_ENV']);
119+ $this->assertSame('0', $_ENV['APP_DEBUG']);
120+ $this->assertSame('bar', $_SERVER['APP_ENV']);
121+ $this->assertSame('1', $_SERVER['APP_DEBUG']);
122+ $this->assertSame('localhost', \getenv('APP_HOST'));
123+ }
124+ }
125+ ```
126+
92127It's also possible to mark a variable as _ unset_ so it will not be present in any of the global variables:
93128
94129``` php
0 commit comments