It’s good enough a function mocker to be released.
The giants who lent the shoulders
The whole function and method mocking thing is made possible by patchwork by antecedent and PHPUnit and those are the giants I’ve used to climb: kudos.
Code pitch
I’m not copying and pasting the README from the repo here but for the code pitch; in short terms “test everything” (that nasty legacy code too).
This can be written in a PHPUnit test suite
use tad\FunctionMocker\FunctionMocker;
class SomeClassTest extends \PHPUnit_Framework_TestCase {
public function setUp(){
FunctionMocker::setUp();
}
public function tearDown(){
FunctionMocker::tearDown();
}
public function testSomeMethodCallsSomeFunction(){
// Setup
$functionMock = FunctionMocker::replace('some_function');
// Exercise
some_function();
// Assert
$functionMock->wasCalledTimes(1);
}
public function testSomeMethodCallsSomeStaticMethod(){
// Setup
$staticMethod = FunctionMocker::replace('Post::get_post_title', 'Post title');
// Exercise
$this->assertEquals('Post title', Post::get_post_title());
// Assert
$staticMethod->wasCalledTimes(1);
}
public function testSomeMethodCallsSomeInstanceMethod(){
// Setup
$replacement = FunctionMocker::replace('Dependency::methodOne');
FunctionMocker::replace('Dependency::methodTwo');
// Exercise
$caller = function(Dependency $dependency){
$dependency->methodOne();
$dependency->methodTwo();
};
$caller($replacement);
// Assert
$replacement->wasCalledTimes(1, 'methodOne');
$replacement->wasCalledTimes(1, 'methodTwo');
}
}
On GitHub
Here it is on GitHub.
Next
I will undergo the shaming experience of inspecting the code using SensioLabInsight and do some refactor along the way. Beside bug-fixes I will be using the tool a little to put some mileage on it.