Mocking static methods and built-in functions in PHP
Testing code that relies on static methods or built-in functions can be challenging in PHP. Traditionally, you’d have to refactor your code to inject dependencies or wrap functions in testable interfaces. To allow you to write clean, maintainable tests without invasive refactors, I created MintyPHP Mocking. It allows you to write things like: $mock = new StaticMethodMock(Adder::class, $this); $mock->expect('add', [1, 2], 3); $result = Adder::add(1, 2); $mock->assertExpectationsMet(); For mocking static methods, and: $mock = new BuiltInFunctionMock('App\Service', $this); $mock->expect('microtime', [true], 1763333612.602); $service = new Service(); $timestamp = $service->getCurrentTime(); $mock->assertExpectationsMet(); For mocking PHP’s built-in functions. ...