Mock Object Framework
Posted: Mon Nov 12, 2007 9:01 am
The long version is here: http://blog.astrumfutura.com/archives/3 ... -Wins.html
As a few have noticed, my previously suggested project called PHPSpec (BDD for PHP) has being progressing rapidly. Recently in using PHPSpec I verified the one thing it really lacks is Mock Object support. We were aware this was going to be a problem eventually.
Subsequent to the blog post, the idea of PHPMock has become a little more concrete. It will be executed and released as an independent Mock Object (and Stub) framework for PHP, capable of integration with PHPSpec, PHPT, and PHPUnit. SimpleTest will likely follow once it's migrated to PHP5 (afterall two of PHPSpec's developers are also SimpleTesters
).
Sebastian Bergmann responded to the blog suggesting a standalone framework would be a good solution. At the moment PHPUnit's stock Mock Objects are fairly difficult to use and lack a few features like indexed return values.
To pass the suggestion by here for comment (where else?), I've written some semblance of PHPMock code along with a set of BDD specs, available from:
http://phpmock.googlecode.com/svn/trunk/ (this is also the subversion url)
The code so far implements an API of the form:
Or a simple Stub generator:
Are there specific use cases anyone would like to see? Any other comments are of course welcome
.
As a few have noticed, my previously suggested project called PHPSpec (BDD for PHP) has being progressing rapidly. Recently in using PHPSpec I verified the one thing it really lacks is Mock Object support. We were aware this was going to be a problem eventually.
Subsequent to the blog post, the idea of PHPMock has become a little more concrete. It will be executed and released as an independent Mock Object (and Stub) framework for PHP, capable of integration with PHPSpec, PHPT, and PHPUnit. SimpleTest will likely follow once it's migrated to PHP5 (afterall two of PHPSpec's developers are also SimpleTesters
Sebastian Bergmann responded to the blog suggesting a standalone framework would be a good solution. At the moment PHPUnit's stock Mock Objects are fairly difficult to use and lack a few features like indexed return values.
To pass the suggestion by here for comment (where else?), I've written some semblance of PHPMock code along with a set of BDD specs, available from:
http://phpmock.googlecode.com/svn/trunk/ (this is also the subversion url)
The code so far implements an API of the form:
Code: Select all
// create Mock/Stub
$class = PHPMock::mock('Album')->getMockClassName();
$mock = new $class;
// set expectations
$mock->shouldReceive('setName')->once()->with('Highway To Hell');
$mock->shouldReceive('setArtist')->once()->with('AC/DC');
$mock->shouldReceive('getArtist')->times(2)->andReturn('AC/DC');
$mock->shouldReceive('getName')->zeroOrMoreTimes()->andReturn('Highway To Hell', 'Highway To Hell', 'StopAsking!');
// do actual stuff
$mock->setName('Highway To Hell');
$mock->setArtist('AC/DC');
$mock->getArtist(); // AC/DC
$mock->getArtist(); // AC/DC
// last return value is persistent on all subsequent calls
$mock->getName(); // Highway To Hell
$mock->getName(); // Highway To Hell
$mock->getName(); // StopAsking!
$mock->getName(); // StopAsking!
$mock->getName(); // StopAsking!
// check all expectations are verified
$mock->verify(); // throws exception by defaultCode: Select all
$class = PHPMock::mock('Album')->getMockClassName();
$stub = new $class;
$stub->shouldReceive( array('getName'=>'Black Album', 'getArtist'=>'Metallica') );
// as before, the LAST return value for a method is persistently returned on all subsequent calls
$stub->getName(); // Black Album
$stub->getArtist(); // Metallica