Function mocking with Patchwork 03

An update on the things to come for the function mocking project.

Unified API

Current release of function mocker will return PHPUnit objects, enriched extensions of the mocked one, when using the mock method on an instance method like this


and then would set expectations and verifications on it the PHPUnit way.
That did ruin the idea of a unified API the mocker could expose and I’m working to allow for the same methods to be called on anything the FunctionMocker class will return.

Going opinionated

PHPUnit has the getMock method to make it all (and the getMockBuilder chain with it) and I wanted to make code more readable using the same terminology (to a point) that’s used in other testing engines, like jasmine, to define what kind of replacement is being requested or built for testing purposes. That’s why the next iteration of the tool will have its API divided to get and set stubs, spies and mocks.
The calls will not differ much and an example is

// any call to to `someFunction` will return 23
FunctionMocker::stub('someFunction', 23);

// spy the calls on someFunction to verify them later   
// return 23 when called
$spy = FunctionMocker::spy('someFunction', 23);



// mock someFunction and verify that's called with this arguments
// return null when called
// fails if it's not called like that
$args = ['post_id' => 13, 'post_content' => 'foo'];
FunctionMocker::mock('someFunction', $args);

someFunction(['post_id' => 13, 'post_content' => 'foo']);

The API as well as the tool still needs scoping and work but that’s the general direction it’s moving.