adriansuter/php-autoload-override
Override PHP autoloading to take control of how classes are resolved. php-autoload-override lets you hook into Composer’s autoloader to swap, alias, or redirect class loading—useful for debugging, testing, and patching vendor code without forks.
composer require --dev adriansuter/php-autoload-overridebootstrap/autoload.php or test bootstrap file (e.g., tests/bootstrap.php), add:
use Adriansuter\AutoloadOverride\AutoloadOverride;
AutoloadOverride::enable();
App\Service\EmailService depends on vendor/package/SenderInterface. To mock SenderInterface without modifying EmailService, register a stub before the dependency is instantiated:
AutoloadOverride::register(\Vendor\Package\SenderInterface::class, \Tests\Stubs\MockSender::class);
This ensures all future new SenderInterface() or instanceof SenderInterface checks in the app use your stub—ideal for unit/integration tests.AutoloadOverride::register() at test bootstrap or per-test setup to inject mocks/stubs for real dependencies (e.g., external APIs, DB clients).
// In TestCase::setUp()
AutoloadOverride::register(\Monolog\Logger::class, \Tests\Stubs\NullLogger::class);
if (app()->environment('testing'))) to avoid accidental prod impact.AutoloadOverride::register(\OldPackage\Service::class, \App\Legacy\CompatibilityService::class);
register() for the same target override sequentially—last registration wins (useful for test isolation).AutoloadOverride::enable() runs before Composer’s autoloader is invoked (typically at bootstrapping, before app/container initialization).\Namespace\Classname). Aliased/namespaced references (use ...; new Class()) will resolve correctly if the FQN is used internally by Composer’s autoloader.AutoloadOverride::getRegistered() to inspect active overrides during debugging.stubFiles or parameters if type-checking mocks.How can I help you explore Laravel packages today?