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.
\rand(), \time(), \file_get_contents()), reducing flaky test failures and improving CI/CD stability.\curl_exec()) in isolated test environments.\Str::random(), \Cache::get()) in unit tests where facades or mocks aren’t sufficient.Adopt if:
\rand(), \time(), \file_get_contents(), \curl_exec()) that cannot be mocked via Laravel’s built-in tools (e.g., Http::fake(), DB::fake()).bootstrap/app.php or a TestingServiceProvider).Look elsewhere if:
Http::fake(), Queue::fake()) that cover your use cases.For Executives:
"This package solves a critical pain point in our PHP test suites: flaky tests caused by global functions like \rand() or \time(). By allowing us to mock these functions per-class during tests, we can eliminate non-deterministic behavior, reduce test failures in CI/CD, and improve the reliability of our Laravel services. It’s a lightweight, MIT-licensed solution that integrates seamlessly with our existing tooling—requiring only a one-time setup in our test bootstrap. The payoff? Faster, more maintainable tests with minimal upfront effort."
For Engineering/Tech Leads:
*"The php-autoload-override package lets us mock global PHP functions (e.g., \rand(), \file_get_contents()) in unit tests by overriding them at the class level during test execution. Here’s why it’s a strong fit for Laravel:
--dev and configured in bootstrap/app.php or a TestingServiceProvider.Http::fake(), DB::fake(), etc., for edge cases where global functions are hardcoded.Tradeoffs:
Proposal: Pilot this in our legacy payment service (where \rand() is used for fraud detection) to demonstrate the ROI in test reliability. If successful, we can expand it to other high-risk test suites."*
For Developers:
*"This package lets you mock global functions (like \rand() or \time()) in your PHPUnit tests by overriding them per-class during test execution. Here’s how it works in Laravel:
composer require --dev adriansuter/php-autoload-override.bootstrap/app.php or a TestingServiceProvider using OverrideFactory.MockRegistry::set() to inject mock values (e.g., MockRegistry::set(MyClass::class, 'rand', 42)).Example Use Case:
// bootstrap/app.php
OverrideFactory::create()
->forClass(\App\Services\FraudDetector::class, ['rand' => \rand(...)])
->apply($loader);
// Test
MockRegistry::set(\App\Services\FraudDetector::class, 'rand', 99);
$detector = new FraudDetector();
$this->assertTrue($detector->isHighRisk());
Why Use It?
\rand(), \time(), etc.When to Avoid It:
How can I help you explore Laravel packages today?