Steps to improve dependency mocking

Maybe I’m being over-protective to the Dependency Mocker class but I’ve updated it again after some code bashing on it.

The problem

I do like TDD (Test Driven Developing) code but do not like, as anyone that’s not fond of useless typing, the chore it sometimes can be.
Keeping classes small and method shorts does help a lot but still some typing cannot be avoided; that’s why I’ve developed a class, tad_DependencyMocker, to help me deal with that load.
Being a week old it came a long way to suit my needs and I’ve hardened its methods to stand some trial and expose a little as an API as possible.

A better solution to my problems

Quoting the README file to the line I’ve narrowed the required input needed for the class to properly mock dependencies to make a major divide:

  1. classes will know about their dependencies
  2. much of the mocking required in tests should be automated

Point 1 I’m addressing using the @depends notation in a class public method documentation block and point 2 I’m addressing parsing that information.
Given this class

class ClassOne { protected $d; /** * @depends D */ public function __construct(D $d){ $this->d = $d; } /** * @depends A, BInterface, CInterface */ public function methodOne(A $a, BInterface $b, CInterface $c){ $a->method(); $b->method(); $c->method(); $this->d->method(); } } 

Standard dependency mocking to test methodOne method would require:

// file ClassOneTest.php public function test_methodOne_will_call_methods(){ // per-test setup $A = $this->getMockBuilder('A') ->disableOriginalConstructor() ->setMethods(array('method') ->getMock(); $BInterface = $this->getMockBuilder('BInterface') ->disableOriginalConstructor() ->setMethods(array('method', 'fooMethod', 'bazMethod')) ->getMock(); $CInterface = $this->getMockBuilder('CInterface') ->disableOriginalConstructor() ->setMethods(array('method', 'barMethod')) ->getMock(); $D = $this->getMockBuilder('D') ->disableOriginalConstructor() ->setMethods(array('method')) ->getMock(); $A->expects($this->once())->method('method'); $BInterface->expects($this->once())->method('method'); $BInterface->expects($this->once())->method('fooMethod'); $BInterface->expects($this->once())->method('bazMethod'); $CInterface->expects($this->once())->method('method'); $CInterface->expects($this->once())->method('barMethod'); $D->expects($this->once())->method('method'); $sut = new ClassOne($D); $sut->methodOne(); } 

The same code could be tested like this using the Dependency Mocker class and
providing some inline documentation:

// file ClassOneTest.php public function test_methodOne_will_call_methods(){ // per-test setup $methods = array('__construct', 'methodOne'); $extraMethods = array( 'BInterface' => array('fooMethod', 'bazMethod'), 'CInterface' => array('barMethod') ); // common setup extract(tad_DependencyMocker::on('ClassOne') ->forMethods($methods) ->setExtraMethods($extraMethods) ->getMocksArray()); // per-test setup $A->expects($this->once())->method('method'); $BInterface->expects($this->once())->method('method'); $BInterface->expects($this->once())->method('fooMethod'); $BInterface->expects($this->once())->method('bazMethod'); $CInterface->expects($this->once())->method('method'); $CInterface->expects($this->once())->method('barMethod'); $D->expects($this->once())->method('method'); $sut = new ClassOne($D); $sut->methodOne(); } 

Test code could be slimmed down even more with refactoring and DRY principles.

Next

I’m thinking about some form of automatic code documentation.

I appreciate your input