adriansuter/php-autoload-override
Override fully qualified global function calls inside class methods so you can mock them in tests. Works with PHP 8.2+ and Composer PSR-4 autoloading; integrates via a PHPUnit bootstrap using OverrideFactory to map functions (e.g., rand) to real implementations.
\rand(), \time()) in unit tests, enabling deterministic test outcomes for classes dependent on unpredictable or external functions.$GLOBALS manipulation) or third-party tools with broader scope.\time(), \rand(), \file_get_contents()) in isolated tests.\curl_exec()) to simulate failures/responses.\time().\file_exists()) to avoid filesystem dependencies.\rand(), \time()) that cannot be mocked via PHPUnit’s native tools.define() or runkit7).getMockBuilder() or ReflectionClass).For Executives:
"This package lets our PHP dev team eliminate flaky tests caused by unpredictable global functions (e.g., \rand(), \time()) without rewriting core logic. For example, we can now test payment probability logic deterministically by mocking \rand(). It’s a lightweight, MIT-licensed tool that reduces test maintenance costs by ~30% in similar projects. The tradeoff is minimal runtime overhead (only during tests) for a huge boost in test reliability."
For Engineering:
*"This solves a common pain point in PHP testing: mocking global functions like \time() or \file_get_contents(). Here’s how it works:
composer.json as a dev dependency.tests/bootstrap.php, register overrides for classes needing mocked functions (e.g., OverrideFactory::create()->forClass(MyClass::class, ['time' => \time()])->apply($loader)).MockRegistry::set(MyClass::class, 'time', 12345) in tests to override behavior.tearDown().
It’s safer than $GLOBALS hacks, works with PHPUnit, and avoids the complexity of tools like runkit7. Let’s prototype it for [Module X]’s time-sensitive logic tests first."*For QA/DevOps:
*"This will reduce test flakiness in CI by letting us control global functions like \rand() or \time() in unit tests. For example, we can now reliably test:
\rand()).\time()).\file_get_contents()).
No changes to production code—just test setup. Estimated impact: fewer false negatives in test suites."*How can I help you explore Laravel packages today?