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(), \file_get_contents()) is required. Fits seamlessly into Laravel’s testing ecosystem (PHPUnit).tests/bootstrap.php or phpunit.xml).setmock() in older PHPUnit) for global functions.--dev dependency).tests/bootstrap.php or phpunit.xml).\time() in a scheduled task test).\file_exists() in disk checks).@phpstan-ignore-next-line).forClass() to limit scope).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Test Flakiness | Medium | Ensure MockRegistry::reset() is called in tearDown() to avoid bleed-over. |
| Performance | Low | Overhead only during test execution (AST parsing is minimal). |
| Tooling Conflicts | Low | Explicitly document overrides in test classes; ignore modified files in static analyzers. |
| Laravel-Specific | Medium | Test with Laravel’s test helpers (e.g., refreshDatabase()) to ensure no conflicts. |
| Future PHP Versions | Low | Package supports PHP 8.2+; Laravel drops PHP 8.1 in v10.x. |
setmock()) across the entire codebase, or only in specific modules?createMock(), partialMock())? Will it reduce reliance on these?\str_random()).// @override \rand()).\Hash::make()), is there a Facade-based alternative?\file_get_contents()).MockRegistry (recommended over manual overrides).beforeEach/afterEach for setup/teardown).\time() for delayed jobs).\str_replace()).\time() for deterministic Carbon tests.$app, app()) unless absolutely necessary.\rand() dependencies).setmock()) with php-autoload-override.// Before: Manual mocking
\Mockery::mock('function', '\rand')->andReturn(42);
// After: Using OverrideFactory
OverrideFactory::create()
->forClass(MyService::class, ['rand' => \rand(...)])
->apply($classLoader);
tests/bootstrap.php).OverrideFactory::build() for reusable test cases (e.g., AbstractTestCase).// Overrides: \rand, \time).| Component | Compatibility | Notes |
|---|---|---|
| Laravel 10.x/11.x | High | PHP 8.2+ support aligns with Laravel’s requirements. |
| PHPUnit 9.x/10.x | High | Works with PHPUnit’s bootstrap system. |
| Pest 2.x | High | Integrates with Pest’s hooks. |
| Static Analyzers | Medium | May flag modified AST; suppress warnings or whitelist test files. |
| Xdebug | High | No impact on debugging. |
| Custom Autoloaders | Low | Only works with Composer’s PSR-4 autoloader. |
OverrideFactory in tests/bootstrap.php.MockRegistry::reset() to base test classes.\rand(), \time()).setmock('\rand') with MockRegistry::set(MyClass::class, 'rand', 42).## Testing Global Functions
Use `php-autoload-override` to mock `\rand()`, `\time()`, etc.
```php
// tests/bootstrap.php
OverrideFactory::create()
->forClass(MyClass::class, ['rand' => \rand(...)])
->apply($classLoader);
bootstrap.php or base test classes.MockRegistry and OverrideFactory.MockRegistry::reset() missing in tearDown()).reset().
tearDown() in base test classes.How can I help you explore Laravel packages today?