adriansuter/php-autoload-override
Override fully qualified function calls inside PHP class methods so you can mock them in tests. Works with Composer PSR-4 autoloading (PHP 8.2+). Use OverrideFactory to map functions (e.g., rand) to custom implementations for deterministic PHPUnit tests.
\Str::random(), \Carbon\Carbon::now()) in unit/integration tests.TestingServiceProvider or AppServiceProvider (e.g., during boot()).Hash::make()) by allowing mocks for underlying functions.\file_get_contents()).Http::fake(), DB::fake()).\error_log() or \mail() could break Laravel’s internal mechanisms.composer require --dev adriansuter/php-autoload-override.tests/bootstrap.php (Laravel’s default test bootstrap).AppServiceProvider::boot() (for runtime use).// tests/bootstrap.php
use AdrianSuter\Autoload\Override\OverrideFactory;
OverrideFactory::create()
->forClass(\App\Services\RandomService::class, ['random_int' => fn() => 42])
->apply($this->getClassLoader());
bootstrap/app.php or a service provider).\Hash::make()) by targeting the facade’s helper class.| Risk Area | Impact | Mitigation |
|---|---|---|
| Test Pollution | Overrides leaking between tests. | Use MockRegistry::reset() in tearDown() or afterEach(). |
| Performance Overhead | Runtime autoload modifications. | Restrict to test/dev environments (dev dependency). |
| Static Analysis Conflicts | False positives in PHPStan/Psalm. | Suppress warnings or document overrides in PHPDoc. |
| Namespace Collisions | Overriding functions in shared classes. | Scope overrides to specific classes (e.g., \App\* vs. global). |
| OPcache Issues | Cached bytecode ignoring overrides. | Disable OPcache in phpunit.xml or use opcache_reset(). |
| Legacy Code Dependencies | Breaking changes if global functions are hardcoded. | Gradually refactor to dependency injection or interfaces. |
\Str::random(), \Carbon\Carbon::now()) a bottleneck in test determinism?Http::fake()) suffice for most use cases?tests/bootstrap.php.\rand(), \file_get_contents()).composer require --dev adriansuter/php-autoload-override
tests/bootstrap.php for 1–2 critical classes.TestingServiceProvider for larger teams.| Component | Compatibility |
|---|---|
| Laravel 10+ | ✅ Full support (PHP 8.2+, PSR-4). |
| Laravel 9.x | ⚠️ Partial (PHP 8.1 may work but not officially supported). |
| PHPUnit/Pest | ✅ Native integration via bootstrap. |
| Mockery | ✅ Complements Mockery for global function mocking. |
| OPcache | ⚠️ May require opcache_reset() or disabled caching in tests. |
| Static Analyzers | ⚠️ May need PHPDoc suppression or configuration tweaks. |
tests/bootstrap.php.\Str::random(), \Carbon\Carbon::now()).bootstrap.php or TestingServiceProvider).--dev dependency).OverrideFactory and MockRegistry reduce runtime errors.tearDown() or Pest hooks).OverrideFactory).MockRegistry API for setting/resetting mocks.MockRegistry::reset()).composer dump-autoload to refresh autoloader.php -r "var_dump(class_alias_exists('...'))" to debug overrides.MockRegistry (lightweight for typical use cases).How can I help you explore Laravel packages today?