11# PHPUnit Globals
22
3- Allows to use annotations to define global variables in PHPUnit test cases.
3+ Allows to use attributes to define global variables in PHPUnit test cases.
44
55[ ![ Build] ( https://github.com/jakzal/phpunit-globals/actions/workflows/build.yml/badge.svg )] ( https://github.com/jakzal/phpunit-globals/actions/workflows/build.yml )
66
7+ Supported attributes:
8+ * ` #[Env] ` for ` $_ENV `
9+ * ` #[Server] ` for ` $_SERVER `
10+ * ` #[Putenv] ` for [ ` putenv() ` ] ( http://php.net/putenv )
11+
712Supported annotations:
813
9- * ` @env ` for ` $_ENV `
10- * ` @server ` for ` $_SERVER `
11- * ` @putenv ` for [ ` putenv() ` ] ( http://php.net/putenv )
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 )
1217
1318Global variables are set before each test case is executed,
1419and brought to the original state after each test case has finished.
@@ -39,7 +44,7 @@ Remember to instruct PHPUnit to load extensions in your `phpunit.xml`:
3944
4045## Usage
4146
42- Enable the globals annotation extension in your PHPUnit configuration:
47+ Enable the globals attribute extension in your PHPUnit configuration:
4348
4449``` xml
4550<?xml version =" 1.0" encoding =" UTF-8" ?>
@@ -51,31 +56,33 @@ Enable the globals annotation extension in your PHPUnit configuration:
5156 <!-- ... -->
5257
5358 <extensions >
54- <bootstrap class =" Zalas\PHPUnit\Globals\AnnotationExtension " />
59+ <bootstrap class =" Zalas\PHPUnit\Globals\AttributeExtension " />
5560 </extensions >
56-
5761</phpunit >
5862```
5963
60- Make sure the ` AnnotationExtension ` is registered before any other extensions that might depend on global variables.
64+ > If you are using a version before PHP 8.1 you can use the ` AnnotationExtension ` instead.
65+
66+ Make sure the ` AttributeExtension ` is registered before any other extensions that might depend on global variables.
6167
62- Global variables can now be defined in annotations :
68+ Global variables can now be defined in attributes :
6369
6470``` php
6571use PHPUnit\Framework\TestCase;
72+ use Zalas\PHPUnit\Globals\Attribute\Env;
73+ use Zalas\PHPUnit\Globals\Attribute\Server;
74+ use Zalas\PHPUnit\Globals\Attribute\Putenv;
6675
6776/**
6877 * @env FOO=bar
6978 */
7079class ExampleTest extends TestCase
7180{
72- /**
73- * @env APP_ENV=foo
74- * @env APP_DEBUG=0
75- * @server APP_ENV=bar
76- * @server APP_DEBUG=1
77- * @putenv APP_HOST=localhost
78- */
81+ #[Env('APP_ENV', 'foo')]
82+ #[Env('APP_DEBUG', '0')]
83+ #[Server('APP_ENV', 'bar')]
84+ #[Server('APP_DEBUG', '1')]
85+ #[Putenv('APP_HOST', 'localhost')]
7986 public function test_global_variables()
8087 {
8188 $this->assertSame('bar', $_ENV['FOO']);
@@ -95,11 +102,9 @@ use PHPUnit\Framework\TestCase;
95102
96103class ExampleTest extends TestCase
97104{
98- /**
99- * @unset-env APP_ENV
100- * @unset-server APP_DEBUG
101- * @unset-getenv APP_HOST
102- */
105+ #[Env('APP_ENV', unset: true)]
106+ #[Server('APP_DEBUG', unset: true)]
107+ #[Putenv('APP_HOST', unset: true)]
103108 public function test_global_variables()
104109 {
105110 $this->assertArrayNotHasKey('APP_ENV', $_ENV);
@@ -115,15 +120,15 @@ replace the extension registration in `phpunit.xml`:
115120
116121``` xml
117122 <extensions >
118- <extension class =" Zalas\PHPUnit\Globals\AnnotationExtension " />
123+ <extension class =" Zalas\PHPUnit\Globals\AttributeExtension " />
119124 </extensions >
120125```
121126
122127with:
123128
124129``` xml
125130 <extensions >
126- <bootstrap class =" Zalas\PHPUnit\Globals\AnnotationExtension " />
131+ <bootstrap class =" Zalas\PHPUnit\Globals\AttributeExtension " />
127132 </extensions >
128133```
129134
0 commit comments