Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Global State

Global State Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Snapshotting of global state, factored out of PHPUnit into a stand-alone component

Frequently asked questions about Global State
When should I use sebastian/global-state in a Laravel project instead of PHPUnit’s built-in isolation?
Use this package only for edge cases like testing legacy code that relies on superglobals (e.g., $_SERVER, $_ENV) or static properties, or when using non-PHPUnit test runners (e.g., custom CLI scripts or Swoole workers). Laravel’s TestCase::refreshApplication() or --process-isolation already handles most global state isolation needs.
Does sebastian/global-state work with Laravel’s default PHPUnit setup (v10+)?
Yes, but it’s redundant since PHPUnit v10+ embeds this internally. If you’re using --no-process-isolation or custom test runners, you can add it as a dev dependency. Otherwise, Laravel’s built-in isolation (e.g., refreshApplication()) is preferred to avoid version skew risks.
How do I install sebastian/global-state for Laravel testing?
Run `composer require --dev sebastian/global-state` to add it as a development dependency. For selective snapshotting, use `Snapshot::snapshot(['globals' => ['$_ENV']])` in your test setup. Avoid global snapshots in production or non-test code.
Will this package break if Laravel upgrades PHP beyond 8.2 (e.g., to 8.3+)?
Potential risks exist if Laravel’s PHPUnit version drops support for older PHP versions (e.g., v9.x doesn’t support PHP 8.3). Check compatibility tables and consider pinning versions in `composer.json` if using this package outside PHPUnit’s default setup.
Can sebastian/global-state replace Laravel’s Mockery or Faker for dependency isolation?
No. This package only snapshots/restores global state (e.g., $_SERVER, $GLOBALS), while Mockery/Faker are for mocking dependencies. For better test isolation, refactor code to use dependency injection instead of relying on globals or statics.
Is it safe to use this package in production Laravel apps?
No. This package is designed for test isolation only. Modifying superglobals (e.g., $_SERVER, $_SESSION) in production can introduce security risks (e.g., path traversal) or state corruption. Use Laravel’s service container or explicit configuration instead.
How do I test legacy code with static properties or superglobal dependencies using this package?
Snapshot the specific globals or static properties before tests run, then restore them afterward. Example: `Snapshot::snapshot(['globals' => ['$myStaticClass->value']])`. Pair this with Laravel’s `beforeApplicationDestroyed()` to clean up if needed.
What are the performance implications of using global state snapshots in Laravel tests?
Snapshotting large superglobals (e.g., $_SESSION) or $GLOBALS can slow tests significantly. For performance-critical suites, limit snapshots to only the necessary globals or use Laravel’s --process-isolation instead.
Are there alternatives to sebastian/global-state for Laravel test isolation?
Yes. For most cases, use Laravel’s built-in tools: `TestCase::refreshApplication()`, `--process-isolation`, or Pest’s isolation features. For static properties, refactor to use Laravel’s service container or dependency injection. Avoid globals entirely.
How do I handle race conditions when restoring superglobals (e.g., $_SESSION) in parallel tests?
This package isn’t designed for concurrent test environments. Race conditions can occur if multiple tests restore conflicting superglobal states. Use `--process-isolation` in PHPUnit or refactor tests to avoid shared state. For Swoole/Lumen, consider request-scoped isolation instead.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata