Function mocking with Patchwork 11

De-opinionating the Patchwork function mocker.

Usage is a harsh teacher

I’ve experimented with the fucntion-mocker and its day-to-day usage underlines that “less is more” is a good rule of thumb to follow.
I’ve laid out an interface that made distinct differences between mocking, spying and stubbing and while that’s adherent to what test double patterns suggest I’ve noticed I make a difference among the test doubles in usage and not in creation.
All this means that I’m working to slim down and replace the current interface

// stub the function and return 23
// no verifications and expectations on it
FunctionMocker::stub('someFunction', 23);


// create a spy for the function and return 23
$spy = FunctionMocker::spy('someFunction', 23);


// will verify calls on it after using it

// create a mock of the function and return 23
// set expectations on it before using it
$mock = FunctionMocker::mock('someFunction', 23);


// expectations are then verified

with a simpler one like this

// here I am stubbing and will not make any verification 
FunctionMocker::replace('someFunction', 23);

// spying here 
$spy = FunctionMocker::replace('someFunction', 23);



Not setting any return value will make the replaced function or method return null.

function someFunction(){
    return true;


// will pass

Return values can be lambda functions too like

function anotherFunction($one, $two){
    return $two - $one;

$callback = function($one, $two){
    return $two + $one;
FunctionMocker::replace('anotherFunction', $callback);

// will pass
$this->assertEquals(8, anotherFunction(3, 5));


I’m wrapping this first iteration of the function mocker deeming it “good enough” and will release it in the next days.

I appreciate your input