zalas/phpunit-globals
A lightweight PHPUnit extension that backs up and restores PHP globals between tests so leaked global state can't break your suite. Register the listener, add the trait, and annotate which globals to preserve for clean, predictable test runs.
$GLOBALS/superglobals via PHP attributes, so tests depending on global state (legacy bootstrap files, framework constants, $_SERVER/$_ENV) state their environment explicitly rather than inheriting mutations from a shared setUp(). This supports a decision to reduce order-dependent, flaky test suites instead of tolerating them.#[DataProvider], #[TestWith]), this keeps global-state setup in the same declarative style — a codebase-wide convention decision, not just a one-off tool.Adopt (or copy the pattern) when:
composer.json before committing).setUp() methods and individual test bodies.Look elsewhere (or vendor/reimplement) when:
#[BackupGlobals], #[RunInSeparateProcess], or explicit setUp()/tearDown() already cover the cases. Prefer the framework's own mechanism.To executives: This is a small MIT-licensed developer-productivity utility for our PHP test suite. It lets engineers declare global test variables in one readable line instead of mutating global state by hand, which means fewer flaky, order-dependent tests, less boilerplate, and a smoother path through our PHP/PHPUnit upgrades. The newly released v4.2.0 adds compatibility with PHPUnit 13, which is a meaningful signal: the maintainer is actively tracking upstream PHPUnit majors, so this dependency is less likely to strand us mid-upgrade than its size suggests. The remaining risk is unchanged — one maintainer and little downstream adoption, so it is not battle-tested by the ecosystem. Recommendation: treat this as a pattern decision rather than a product purchase. Either adopt it pinned/vendored with a scheduled review at each PHPUnit major, or implement the equivalent internally in under a day and accept that we then own that upgrade churn. Direct cost is negligible; the decision should hinge on whether our dependency policy tolerates single-maintainer test-only libraries that demonstrate active maintenance.
To engineering: The package gives declarative, per-test control of $GLOBALS and superglobals through PHP attributes, replacing ad-hoc setUp() mutations and making a test's environmental assumptions visible at the top of the class. It fits naturally if we're already attribute-driven. v4.2.0 only widens the supported PHPUnit range to include 13.0 — no API or behavioral changes are called out, so it should be a drop-in minor bump with low upgrade cost. Caveats that still stand: pin the version and confirm the supported PHPUnit range in composer.json matches ours; reflection overhead is real at suite scale; and #[BackupGlobals] or an internal trait may cover most of the need. My ask is unchanged, with one adjustment: run the one-hour spike comparing this against a ~30-line internal trait, but now explicitly factor in the recurring cost of re-validating that trait against each new PHPUnit major — v4.2.0 is one concrete data point that the package absorbs that cost for us. Then standardize the winner and document it in our testing guidelines.
How can I help you explore Laravel packages today?