- Can I use Patchwork to mock Laravel’s core functions like `Str::of()` or `Cache::remember()` in tests?
- Patchwork can intercept Laravel’s facade methods (e.g., `Str::of()`) or static calls, but proceed with caution. Overriding core functions may destabilize Laravel’s assumptions or conflict with OPcache. Test thoroughly in isolation and avoid patching in production. For facades, consider Laravel’s built-in mocking tools first.
- How do I install Patchwork in a Laravel project without breaking production?
- Add Patchwork as a dev dependency via Composer (`composer require antecedent/patchwork --dev`) and scope patches to test environments using `.env` flags or CI checks. Avoid applying patches in production unless absolutely necessary, as runtime rewrites can interfere with OPcache and performance.
- Will Patchwork work with Laravel’s service container or facade mocking?
- Patchwork operates at a lower level than Laravel’s container or facade mocking, so it can complement or override those systems. For example, you can patch a static method while keeping container bindings intact. However, patches applied to the same method may conflict—prioritize one approach per test scenario.
- Does Patchwork support PHP 8.2 and Laravel 10+?
- Patchwork’s last release (2025) suggests compatibility with PHP 8.1+, but validate with Laravel 10+ and PHP 8.2+ in your environment. Check for conflicts with Laravel’s bootstrap/app.php or autoloader, as runtime rewrites may require adjustments like disabling OPcache in tests or using `opcache_reset()`.
- How can I scope patches to specific test classes or methods in PHPUnit/Pest?
- Apply patches in `setUp()` (PHPUnit) or `beforeEach()` (Pest) to ensure they’re isolated per test. Use a cleanup method (e.g., `tearDown()`) to revert patches. For Pest, leverage its `beforeAll`/`afterAll` hooks to manage patch lifecycle. Avoid global patches unless necessary.
- Are there performance concerns with Patchwork in production?
- Patchwork’s runtime rewrites bypass OPcache optimizations, which can degrade performance. In production, avoid patches unless critical, and consider disabling OPcache or using `opcache_reset()` sparingly. Benchmark patches in staging to assess overhead before deployment.
- Can Patchwork patch third-party SDKs or static classes in Laravel?
- Yes, Patchwork excels at targeting legacy or third-party code, including static classes or SDKs without source access. For example, you can stub a static `LegacyAuth::check()` method or intercept calls to an external API client. This is ideal for testing inherited codebases where refactoring isn’t feasible.
- How do I handle static analysis warnings (Psalm/PHPStan) for patched methods?
- Static analyzers may flag patched methods as undefined or unused. Mitigate this by adding `@method` or `@mixin` PHPDoc annotations to your test classes or using custom Psalm/PHPStan rules to ignore patched code. Document patches clearly in your test suite to avoid confusion.
- What’s the difference between Patchwork and Laravel’s built-in mocking tools (Mockery, partial mocks)?
- Patchwork enables runtime monkey patching of *any* function or method, including globals and statics, while Laravel’s mocking tools focus on container-bound classes. Use Patchwork for legacy code or third-party SDKs where DI isn’t an option, and Mockery for Laravel-specific dependencies.
- How do I debug issues caused by Patchwork patches in Laravel?
- Patchwork may obscure stack traces for patched code, making debugging tricky. Log patch applications explicitly and use `Xdebug` to step through rewritten methods. Avoid patching in production unless you have robust rollback mechanisms, and test patches in isolation first.