Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Autoload Override Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Improving Test Reliability: Enables deterministic unit testing for classes dependent on global functions (e.g., \rand(), \time(), \file_get_contents()), reducing flaky test failures and improving CI/CD stability.
  • Build vs. Buy Decision: Avoids reinventing a custom autoload override solution, saving development time and reducing technical debt. The package is battle-tested and maintained.
  • Roadmap Alignment: Supports initiatives to modernize PHP test suites, particularly for legacy codebases or services where global functions are hardcoded. Aligns with Laravel’s focus on testability and maintainability.
  • Use Cases:
    • Legacy Code Refactoring: Gradually replace global functions in tests without modifying production code.
    • Integration Testing: Mock external dependencies (e.g., \curl_exec()) in isolated test environments.
    • Edge Case Testing: Simulate rare conditions (e.g., high/low random values) without refactoring business logic.
    • Laravel-Specific Scenarios: Override global helpers (e.g., \Str::random(), \Cache::get()) in unit tests where facades or mocks aren’t sufficient.

When to Consider This Package

  • Adopt if:

    • Your Laravel/PHP codebase relies on global functions (e.g., \rand(), \time(), \file_get_contents(), \curl_exec()) that cannot be mocked via Laravel’s built-in tools (e.g., Http::fake(), DB::fake()).
    • You prioritize deterministic unit tests and need to override global functions per-class or globally during testing.
    • Your team uses PHP 8.2+ and PSR-4 autoloading (standard in Laravel).
    • You’re willing to integrate a dev-only dependency into your test bootstrap (e.g., bootstrap/app.php or a TestingServiceProvider).
    • You’re testing legacy code where refactoring to dependency injection is not feasible in the short term.
  • Look elsewhere if:

    • Your tests can avoid global functions entirely (e.g., by using Laravel’s facades, services, or dependency injection).
    • You’re using PHP < 8.2 or a non-PSR-4 autoloader (incompatible).
    • Overriding global functions introduces complexity that outweighs the benefits (e.g., for simple projects with minimal global function usage).
    • Your team prefers alternative mocking frameworks (e.g., Mockery) for class/method mocking, and global functions are rare.
    • You’re already using Laravel’s testing helpers (e.g., Http::fake(), Queue::fake()) that cover your use cases.

How to Pitch It (Stakeholders)

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:

  • Works with PSR-4 autoloading (standard in Laravel) and PHP 8.2+.
  • Dev-only dependency: Installed via Composer --dev and configured in bootstrap/app.php or a TestingServiceProvider.
  • Complements Laravel’s tools: Use alongside Http::fake(), DB::fake(), etc., for edge cases where global functions are hardcoded.
  • No production impact: Overrides are test-scoped and don’t affect runtime behavior.

Tradeoffs:

  • Requires a bootstrap setup (one-time cost).
  • Best for legacy code or cases where refactoring to DI is impractical.
  • Not a replacement for Laravel’s built-in mocking (e.g., facades, services).

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:

  1. Install: composer require --dev adriansuter/php-autoload-override.
  2. Configure: Add overrides to bootstrap/app.php or a TestingServiceProvider using OverrideFactory.
  3. Test: Use 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?

  • No refactoring: Mock global functions without changing production code.
  • Deterministic tests: Eliminate flakiness from \rand(), \time(), etc.
  • Laravel-friendly: Works with PSR-4 and integrates with Laravel’s autoloading.

When to Avoid It:

  • If you can refactor to use Laravel facades/services instead.
  • For simple projects where global functions are rare.
  • If you prefer Mockery for class/method mocking (this is for global functions only)."*
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata