[ Content | View menu ]

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:

  1. String – Required – the class name to mock
  2. Array – Optional – the methods to mock
  3. Array – Optional – arguments to pass to the mock’s constructor
  4. String – Optional – a class name for the mock
  5. Boolean – Optional – disable the call to the original class’ constructor
  6. Boolean – Optional – disable the call to the original object’s clone method
  7. 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.

Filed in: Programming.

2 Comments

  1. Comment by Brian Wigginton:

    Thanks for this, really helpful.

    June 14, 2012 @ 14:48
  2. Pingback from PHPUnit Mocking | JMC's Capricious Contemplations:

    […] real divideBy() function to test that. getMock() has a lot of useful options, refer to the api or Mark Mzyk’s blog post I found on the getMock method […]

    January 17, 2013 @ 14:16