- 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.