sebastian/global-state
sebastian/global-state snapshots and restores PHP global state (globals, superglobals, ini settings, etc.), extracted from PHPUnit as a standalone component. Useful for test isolation and detecting side effects by capturing state before and after code runs.
$_SESSION, $_SERVER) or third-party libraries that pollute globals, where flaky tests block CI/CD pipelines.$_GET resets, register_shutdown_function hacks) with a maintainable, community-backed solution, reducing technical debt.$_ENV instead of all superglobals), reducing contention in CI environments.Adopt when:
$_GET, $_POST, $_SERVER, $_ENV) or static properties in a way that causes test pollution (e.g., TestCase A breaks TestCase B).$_SESSION for stateful services) and needs a "safety net" for global state.$_ENV or specific static classes) to optimize test performance in large suites.Avoid when:
--process-isolation) suffices.sebastian/global-state adds unnecessary complexity.$_SERVER['REQUEST_TIME']) that shouldn’t be restored to past values.For Executives:
*"This package solves a hidden bottleneck in our test suite: global state pollution is causing flaky tests, slowing down releases, and increasing debugging costs. By adopting sebastian/global-state, we can:
$_SESSION, $_ENV) between test cases.For Engineering Leaders: *"This is a low-risk, high-reward tool for teams struggling with:
$_GET modified by one test breaking another).Key benefits:
unset($_SESSION) in tearDown()).$_ENV instead of all superglobals).Trade-offs:
For Developers:
*"If you’ve ever seen a test pass locally but fail in CI because $_SESSION or $_SERVER was left dirty, this package is for you. It lets you:
$snapshot = Snapshot::snapshot(['globals' => ['$_GET', '$_POST']]);
// ... test logic ...
$snapshot->restore(); // boom, clean slate.
$_ENV or clearing static caches.Gotchas:
['globals' => ['$_ENV']]).Start here:
composer require --dev sebastian/global-state
Then wrap your test logic in snapshots. Works with PHPUnit, Pest, or raw scripts."*
How can I help you explore Laravel packages today?