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.
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.config('app.timezone') with a service container-bound config class).microtime(), rand(), or custom global helpers in high-throughput systems (e.g., Laravel queues, cron jobs).app() or route() helpers).file_get_contents() for API calls).Adopt when:
str_replace, date(), config(), or custom helpers like helper('str.slug')).file_get_contents() or database errors in DB::connection()).createMock() or Laravel’s Mockery) is impractical.Look elsewhere if:
Mockery instead).orchestra/testbench for Laravel-focused mocking.mockery/mockery for OOP, brick/math for math functions.runkit or opcache for advanced use cases).Mockery::mock() or PHPUnit’s getMockBuilder()).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:
Log::error()).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:
file_get_contents() or DB::raw().json_encode() throwing errors).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!"*How can I help you explore Laravel packages today?