PHPackages                             zalas/phpunit-globals - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. zalas/phpunit-globals

ActiveLibrary[Testing &amp; Quality](/categories/testing)

zalas/phpunit-globals
=====================

Allows to use attributes to define global variables in PHPUnit test cases.

v4.2.0(4mo ago)40440.5k↓37.3%913MITPHPPHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0CI passing

Since Mar 1Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/jakzal/phpunit-globals)[ Packagist](https://packagist.org/packages/zalas/phpunit-globals)[ GitHub Sponsors](https://github.com/jakzal)[ RSS](/packages/zalas-phpunit-globals/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (1)Versions (44)Used By (13)

PHPUnit Globals
===============

[](#phpunit-globals)

Allows to use attributes to define global variables in PHPUnit test cases.

[![Build](https://github.com/jakzal/phpunit-globals/actions/workflows/build.yml/badge.svg)](https://github.com/jakzal/phpunit-globals/actions/workflows/build.yml)

Supported attributes:

- `#[Env]` for `$_ENV`
- `#[Server]` for `$_SERVER`
- `#[Putenv]` for [`putenv()`](http://php.net/putenv)

> 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. The same applies to `putenv()`/`getenv()` calls.

Installation
------------

[](#installation)

### Composer

[](#composer)

```
composer require --dev zalas/phpunit-globals
```

### Phar

[](#phar)

The extension is also distributed as a PHAR, which can be downloaded from the most recent [Github Release](https://github.com/jakzal/phpunit-globals/releases).

Put the extension in your PHPUnit extensions directory. Remember to instruct PHPUnit to load extensions in your `phpunit.xml` using the `extensionsDirectory` attribute:

```

```

Usage
-----

[](#usage)

Enable the globals attribute extension in your PHPUnit configuration:

```

```

Make sure the `AttributeExtension` is **registered before** any other extensions that might depend on global variables.

Global variables can now be defined in attributes:

```
use PHPUnit\Framework\TestCase;
use Zalas\PHPUnit\Globals\Attribute\Env;
use Zalas\PHPUnit\Globals\Attribute\Server;
use Zalas\PHPUnit\Globals\Attribute\Putenv;

#[Env('FOO', 'bar')]
class ExampleTest extends TestCase
{
    #[Env('APP_ENV', 'foo')]
    #[Env('APP_DEBUG', '0')]
    #[Server('APP_ENV', 'bar')]
    #[Server('APP_DEBUG', '1')]
    #[Putenv('APP_HOST', 'localhost')]
    public function test_global_variables()
    {
        $this->assertSame('bar', $_ENV['FOO']);
        $this->assertSame('foo', $_ENV['APP_ENV']);
        $this->assertSame('0', $_ENV['APP_DEBUG']);
        $this->assertSame('bar', $_SERVER['APP_ENV']);
        $this->assertSame('1', $_SERVER['APP_DEBUG']);
        $this->assertSame('localhost', \getenv('APP_HOST'));
    }
}
```

It's also possible to mark an attribute with *unset* so it will not be present in any of the global variables:

```
use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    #[Env('APP_ENV', unset: true)]
    #[Server('APP_DEBUG', unset: true)]
    #[Putenv('APP_HOST', unset: true)]
    public function test_global_variables()
    {
        $this->assertArrayNotHasKey('APP_ENV', $_ENV);
        $this->assertArrayNotHasKey('APP_DEBUG', $_SERVER);
        $this->assertArrayNotHasKey('APP_HOST', \getenv());
    }
}
```

Updating to PHPUnit 10
----------------------

[](#updating-to-phpunit-10)

When updating from a previous version of this extension dedicated to work with PHPUnit 9, replace the extension registration in `phpunit.xml`:

```

```

with:

```

```

Contributing
------------

[](#contributing)

Please read the [Contributing guide](CONTRIBUTING.md) to learn about contributing to this project. Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.

###  Health Score

66

—

FairBetter than 99% of packages

Maintenance78

Regular maintenance activity

Popularity49

Moderate usage in the ecosystem

Community29

Small or concentrated contributor base

Maturity93

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 87.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~70 days

Recently: every ~87 days

Total

42

Last Release

120d ago

Major Versions

v2.6.0 → v3.1.22023-04-05

v2.6.3 → v3.2.02023-11-27

v2.6.4 → v3.2.12024-05-20

v2.6.5 → v3.4.02024-11-28

3.5.x-dev → v4.0.02025-02-24

PHP version history (11 changes)v1.0.1PHP ^7.1

v1.2.0PHP ^7.2

v2.1.0PHP ^7.3 || ~8.0.0

v2.2.0PHP ^7.3 || ~8.0.0 || ~8.1.0

v2.4.0PHP ^7.3 || ~8.0.0 || ~8.1.0 || ~8.2.0

v2.5.0PHP ~8.0.0 || ~8.1.0 || ~8.2.0

v3.0.0-alpha.1PHP ~8.1.0 || ~8.2.0

v3.2.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0

2.6.x-devPHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0

v3.5.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0

v4.1.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/190447?v=4)[Jakub Zalas](/maintainers/jakzal)[@jakzal](https://github.com/jakzal)

---

Top Contributors

[![jakzal](https://avatars.githubusercontent.com/u/190447?v=4)](https://github.com/jakzal "jakzal (118 commits)")[![bbatsche](https://avatars.githubusercontent.com/u/500909?v=4)](https://github.com/bbatsche "bbatsche (4 commits)")[![bytestream](https://avatars.githubusercontent.com/u/1788397?v=4)](https://github.com/bytestream "bytestream (3 commits)")[![j-schumann](https://avatars.githubusercontent.com/u/114239?v=4)](https://github.com/j-schumann "j-schumann (2 commits)")[![BenMorel](https://avatars.githubusercontent.com/u/1952838?v=4)](https://github.com/BenMorel "BenMorel (2 commits)")[![fzieris](https://avatars.githubusercontent.com/u/4941859?v=4)](https://github.com/fzieris "fzieris (2 commits)")[![aaajeetee](https://avatars.githubusercontent.com/u/9864953?v=4)](https://github.com/aaajeetee "aaajeetee (2 commits)")[![yoannrenard](https://avatars.githubusercontent.com/u/6651065?v=4)](https://github.com/yoannrenard "yoannrenard (1 commits)")[![dkarlovi](https://avatars.githubusercontent.com/u/209225?v=4)](https://github.com/dkarlovi "dkarlovi (1 commits)")

---

Tags

annotationsattributesenvenv-varsenvironmentenvironment-variablesglobalsphpphpunitphpunit-extensionphpunit-listenertesttests

### Embed Badge

![Health badge](/badges/zalas-phpunit-globals/health.svg)

```
[![Health](https://phpackages.com/badges/zalas-phpunit-globals/health.svg)](https://phpackages.com/packages/zalas-phpunit-globals)
```

###  Alternatives

[brianium/paratest

Parallel testing for PHP

2.5k129.9M905](/packages/brianium-paratest)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k41.3M38.3k](/packages/orchestra-testbench)[drupal/core-dev

require-dev dependencies from drupal/drupal; use in addition to drupal/core-recommended to run tests from drupal/core.

2022.0M321](/packages/drupal-core-dev)[webmozarts/strict-phpunit

Enables type-safe comparisons of objects in PHPUnit

30300.1k6](/packages/webmozarts-strict-phpunit)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
