PHPUnit getMock Method Signature
Mark Mzyk | June 16, 2011
In writing unit tests for PHP, I normally use PHPUnit. I frequently use mocks, but forget the params for the getMock call after the first two arguments, as I don’t use the rest frequently. Looking up the method signature through Google is frustratingly difficult, so here it is for future reference.
PHPUnit getMock takes a possible seven params.
They are, in order:
- String – Required – the class name to mock
- Array – Optional – the methods to mock
- Array – Optional – arguments to pass to the mock’s constructor
- String – Optional – a class name for the mock
- Boolean – Optional – disable the call to the original class’ constructor
- Boolean – Optional – disable the call to the original object’s clone method
- Boolean – Optional – disable autoload during the mock object creation
The getMock implementation can be found on github, in the phpunit-mock-objects repo, in Generator.php.
Documentation on what I just explained can be found in the PHP 3.4 docs with arduous searching (see Table 22.6 TestCase), but I’ve not found the same page in the 3.5 docs.
I’ve never used beyond the third argument to getMock, so I have no idea how useful the rest are.