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.
TestCase, RefreshDatabase, Mockery) already abstract global state management via PHPUnit’s internals or Laravel-specific traits. This package’s core value—snapshotting global state—is redundant in Laravel’s default workflow, where dependency injection and service container mocking are preferred.$_GET, $_SERVER, etc.) in favor of explicit dependency injection. This package’s reliance on global state snapshotting could encourage anti-patterns (e.g., testing code that modifies $_ENV or $_SESSION directly), violating Laravel’s "explicit over implicit" principle.$_SERVER['REQUEST_METHOD'] for HTTP mocking).composer require --dev sebastian/global-state). However, no Laravel-specific integration exists—developers would need to manually implement snapshotting logic in test cases or custom test bases.TestCase already leverages PHPUnit’s process isolation (via --process-isolation flag) for test isolation, reducing the need for manual snapshotting.laravel/pint or spatie/laravel-ignition may interact unpredictably with global state modifications.$_SERVER, $_ENV, or $_SESSION during runtime (e.g., in routes, middleware) is a security and stability anti-pattern. This package could enable such behavior if misused outside tests.RefreshDatabase trait (which scopes to a single database), this package offers no built-in protection against accidental global state corruption.Request objects instead of $_SERVER).$_SESSION or $_COOKIE in parallel tests can cause conflicts (e.g., session locks, CSRF token mismatches).$GLOBALS arrays or complex objects can slow test suites. Laravel’s default isolation (process-based) is often faster for most use cases.Why not use Laravel’s built-in isolation?
$_ENV without full process isolation)?PHP version compatibility:
Alternative solutions:
Http::fake(), Route::fake(), or custom test traits) instead of a generic snapshotting library?orchestra/testbench) that already provides similar functionality with better integration?Security and stability:
Cache::put() storing global state)?Performance trade-offs:
--process-isolation?require-dev in composer.json. Laravel’s ecosystem already provides production-grade alternatives (e.g., service containers, dependency injection).TestCase already integrates with PHPUnit’s isolation features, making this a secondary tool at best.RefreshDatabase, MigrateFresh, or Http::fake() if tests rely on both global state snapshots and these traits.laravel/telescope, this requires manual initialization in test classes.Assessment phase:
$_SERVER, $_ENV, static properties). Use static analysis (e.g., PHPStan) to flag unsafe patterns.--process-isolation to determine if snapshotting offers measurable benefits.Pilot integration:
use SebastianBergmann\GlobalState\Snapshot;
class LegacyTest extends TestCase
{
private ?Snapshot $snapshot;
protected function setUp(): void
{
$this->snapshot = Snapshot::snapshot(['globals' => ['$_SERVER']]);
}
protected function tearDown(): void
{
$this->snapshot?->restore();
}
public function test_server_modification()
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$this->assertEquals('POST', $_SERVER['REQUEST_METHOD']);
// State is restored automatically after test.
}
}
Gradual rollout:
beforeEach/afterEach hooks in Jest-like setups) with this package.Deprecation plan:
RefreshDatabase or MigrateFresh (database isolation is orthogonal to global state).Http::fake() if tests modify $_SERVER or $_COOKIE—ensure consistent isolation strategies.$_SESSION (e.g., laravel-session) may behave unpredictably when snapshotted. Test with snapshotSuperGlobals() disabled by default.Phase 1: Evaluation (2 weeks)
--process-isolation vs. snapshotting for a sample test suite.Phase 2: Pilot (3 weeks)
$_SESSION).Phase 3: Rollout (4–6 weeks)
$_SERVER is modified outside snapshots).How can I help you explore Laravel packages today?