Mocking object constructor params

Hi,

I’m using PHPUnit and I’m having a difficult time figuring out how to mock an object whose contructor requires a set of parameters in order to properly construct it.


$mock = $this->getMock('mnc\\util\\Point', array('getName', 'getUrl', 'getModule'));

When I run this I get an error “Missing argument 1” because the class Point requires 3 parameters in order to properly be built:


$p = new Point('nameTest', 'url/test', 'modueTest');

I’m unable to find any type of useful results from a google search or the PHPUnit manual. Any help would be nice.

Thanks!

From my limited understanding, you don’t appear to be passing the constructor arguments in - which is specified using a third argument

The third (optional) parameter may hold a parameter array that is passed to the mock object’s constructor

Depending on your PHP version, I’d recommend using Mockery instead

http://blog.astrumfutura.com/2010/05/the-mockery-php-mock-objects-made-simple/

often it’s just simpler to make stub classes that return the necessary values - this is easier to achieve if you use dependency injection