2011年1月2日 星期日

PHPMock: Independent Mock Object/Stub Framework

Sometimes I think my readers must get terribly bored with my one track entries – I seem to roll through phases: a few weeks of OpenID, then a few of PHPSpec, then a detour for several months on X . That’s what you get for reading an open source developer’s weblog – we can only really work on one thing at a time so there’s a lack of variety.

Anyway, after that minor detour, this one’s a brief notice that we’ve commenced work (as of a while ago really) on PHPMock.

If you remember, I had one of those long winded blog posts about Mock Objects and Stubs in SimpleTest/PHPUnit and PHPT. Now that I’ve really launched into PHPSpec development, I’m finding a few spots here and there where my attention levels drop and I need a diversion. Which means PHPMock is seeing activity – there’s a whole branch written over a few days to get an API discussion going between developers.

The purpose of PHPMock is to write an independent Mock Object/Stub framework – something you can use in any scenario or xDD approach – from a PHPUnit Unit Test, to a PHPSpec Spec, to a PHPT test file. SimpleTest isn’t left out either – PHPMock is self contained so it should travel well.

Once we get over the Mock vs Stub approach question to such a framework (Are Mocks complicated Stubs, or Stubs simplified Mocks?) we’ll have a firm design and an API to develop with. I’m currently favouring an API in a simple readable form, e.g.

[geshi lang=php]// setup
$mockPerson = PHPMock::mock(‘Person’);
$mockPerson->shouldReceive(‘hasName’)->withNoArgs()->once()->andReturn(false);

// implemented use case
$mockPerson->hasName(); //FALSE

// verification of Mock expectations
$mockPerson->verify(); //TRUE[/geshi]

Not sure if it will stay that way, have some alternate API appended (less verbose), or end up as something different. I’ll let you know.

A good feeling about this small project is that we have peers checking in. Sebastian Bergmann for PHPUnit, me for PHPSpec, and Travis Swicegood for PHPT and maybe SimpleTest. So at least it’s not just me rumbling around enforcing my divine will .

If you want to look around the experiemental branch code, we’re hosted over at http://code.google.com/p/phpmock/.

reference : http://blog.astrumfutura.com/2007/11/phpmock-independent-mock-objectstub-framework/

沒有留言:

wibiya widget