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);

someFunction();

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

someFunction();
someFunction();
someFunction();

// will verify calls on it after using it
$spy->wasCalled...

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

someFunction();
someFunction();
someFunction();

// expectations are then verified
$mock->verify();

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);

someFunction();
someFunction();
someFunction();

$spy->wasCalled...

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

function someFunction(){
    return true;
}

FunctionMocker::replace('someFunction');

// will pass
$this->assertNull(someFunction());

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));

Next

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