Weave 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

Function Mock Laravel Package

avris/function-mock

Mock PHP functions in unit tests without changing your code. avris/function-mock lets you replace and control built-in and user-defined function calls at runtime, making it easier to test legacy code, edge cases, and hard-to-isolate behavior.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Testing Strategy: Enables deterministic unit/integration testing for legacy or third-party PHP/Laravel codebases where global functions (e.g., file_get_contents(), config(), or custom helpers) are deeply embedded. Reduces reliance on monolithic frameworks like PHPUnit’s built-in mocking or Laravel’s Mockery for procedural code.
  • Legacy Modernization: Accelerates refactoring by isolating global function dependencies, allowing incremental migration to class-based or dependency-injected architectures (e.g., replacing config('app.timezone') with a service container-bound config class).
  • Performance-Critical Paths: Lightweight alternative to heavier mocking solutions (e.g., Mockery) when testing functions like microtime(), rand(), or custom global helpers in high-throughput systems (e.g., Laravel queues, cron jobs).
  • Build vs. Buy: Justifies buying this package over custom solutions when:
    • The team lacks time/resources to build a mocking utility.
    • The codebase has <1,000 global function calls (scalability threshold).
    • Mocking needs are procedural (not OOP-focused) and Laravel-specific (e.g., mocking app() or route() helpers).
  • Roadmap Prioritization: Useful for teams adopting:
    • Laravel’s testing improvements (e.g., Pest, PHPUnit 10) where global function mocking is a gap.
    • Serverless PHP (e.g., AWS Lambda, Bref) where global functions interact with external services (e.g., file_get_contents() for API calls).
    • Microservices where shared procedural libraries (e.g., utility classes) need isolated testing.

When to Consider This Package

Adopt when:

  • Your Laravel codebase relies on global functions (e.g., str_replace, date(), config(), or custom helpers like helper('str.slug')).
  • You need fine-grained control over function return values/side effects (e.g., simulating API failures in file_get_contents() or database errors in DB::connection()).
  • Testing procedural code or legacy systems where OOP mocking (e.g., PHPUnit’s createMock() or Laravel’s Mockery) is impractical.
  • You’re avoiding monolithic frameworks (e.g., PHPUnit’s mocking) for performance or simplicity in high-volume test suites (e.g., CI pipelines).
  • You lack time/resources to refactor global functions into testable classes/interfaces but need to isolate them for testing.

Look elsewhere if:

  • Your codebase is fully OOP with no global functions (use PHPUnit/Mockery/Laravel’s Mockery instead).
  • You need mocking for classes/interfaces (this package is function-only).
  • The package’s lack of stars/activity (0 stars) raises maintenance concerns (consider alternatives like:
    • Laravel-specific: orchestra/testbench for Laravel-focused mocking.
    • General PHP: mockery/mockery for OOP, brick/math for math functions.
    • Custom: Runtime function overriding via runkit or opcache for advanced use cases).
  • You require async/mocking closures (this package targets global functions, not callables or promises).
  • Your tests involve static methods (use Mockery::mock() or PHPUnit’s getMockBuilder()).

How to Pitch It (Stakeholders)

For Executives: "This package lets our Laravel team mock global functions like file_get_contents(), config(), or custom helpers in tests, eliminating flaky CI builds and speeding up legacy code refactoring. For example, we can simulate API timeouts or database failures without rewriting core logic—saving dev time and improving test reliability. It’s a low-risk, high-impact tool for teams maintaining procedural PHP or migrating to modern Laravel patterns. Cost: Free (open-source); Risk: Minimal (lightweight, isolated to tests)."

For Engineering (Laravel PMs/Architects): "avris/function-mock is a minimalist alternative to PHPUnit/Mockery for mocking global functions in Laravel. Key benefits:

  • Zero Laravel bloat: Just 1 class (~500 LOC), no framework dependencies.
  • Flexible: Mock return values, exceptions, or side effects (e.g., track calls to Log::error()).
  • Performance: No reflection overhead like Mockery; ideal for high-volume tests (e.g., Laravel queues).
  • Laravel-friendly: Works alongside Mockery, Pest, or PHPUnit without conflicts. Use case: Mock date('Y-m-d') to test time-sensitive logic or file_get_contents() to avoid real HTTP calls in tests. Tradeoff: Not for OOP mocking—pair with Laravel’s Mockery for classes. Risk: Unmaintained (0 stars), but trivial to fork if needed. Recommendation: Use for legacy procedural code; refactor globals to classes long-term."*

For Developers: "Need to test str_replace() or config('app.timezone') calls without hitting real services? This package lets you:

use Avris\FunctionMock\FunctionMock;

$mock = new FunctionMock();
$mock->mock('file_get_contents', fn() => 'fake-api-response');
$mock->mock('date', fn() => '2023-01-01'); // Freeze time in tests

Perfect for Laravel when:

  • You’re testing service classes that call file_get_contents() or DB::raw().
  • You need to simulate edge cases (e.g., json_encode() throwing errors).
  • You’re avoiding test pollution from global state (e.g., app() or route() helpers). Pro tip: Use in setUp() for PHPUnit or beforeEach for Pest to keep tests clean. Warning: Doesn’t mock classes—use Mockery for those!"*
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.
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels
wazza/laravel-db-encryption
hokny/laravel-generator
vonsogt/backup
jengo/queues
jeremykenedy/laravel-observability
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums