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, static variables) in PHP-based applications, critical for CI/CD pipelines and regression testing.TestCase abstracts much of this, this package enables granular control for legacy codebases or custom test runners (e.g., PestPHP, custom CLI tools) where Laravel’s defaults fall short.$_ENV for config-heavy tests) instead of full process isolation (e.g., --process-isolation in PHPUnit).Adopt when:
$_GET, $_SESSION, static variables, $GLOBALS) and exhibit flakiness.Avoid when:
TestCase with default isolation (no global state pollution).--process-isolation and don’t need selective state control.For Executives:
"This package solves a critical but invisible problem: flaky tests. In PHP applications—especially those migrating to Laravel—global variables like $_SESSION or static caches can corrupt test environments, wasting engineering time on false failures. By snapshotting and restoring state between tests, we eliminate this noise, accelerating CI/CD cycles and reducing risk in releases. It’s a low-cost, high-impact fix for legacy systems or complex integrations where Laravel’s defaults aren’t enough. Think of it as ‘undo’ for your tests—guaranteeing consistency without rewriting code."
For Engineering/DevOps:
*"Problem: Global state leaks between tests cause intermittent failures, slowing down development and CI.
Solution: sebastian/global-state lets us capture and restore PHP’s superglobals, static properties, and INI settings with one line of code. Unlike PHPUnit’s process isolation (which is heavy), this gives us selective control—only snapshot what we need (e.g., $_ENV for config tests) and skip the rest. It’s battle-tested by PHPUnit itself and integrates seamlessly with Laravel’s testing stack.
Trade-offs:
For Developers:
*"Need to test code that touches $_SERVER, $_SESSION, or static variables? This package lets you snapshot the state before a test and restore it afterward, ensuring isolation without rewriting the code under test.
Example:
// In your test’s setUp():
$this->snapshot = Snapshot::snapshot(['globals' => ['$_GET']]);
// Later, restore:
$this->snapshot->restore();
Why not just use --process-isolation? Because it’s slower and heavier—this gives you fine-grained control (e.g., skip snapshotting $_SESSION if you don’t need it).
Gotchas:
$_SESSION/$_COOKIE unless you handle race conditions.How can I help you explore Laravel packages today?