Cleaner and more versatile dependency mocker

I’ve updated the tad_TestableObject class part of the tdd-helpers package to remove needless code and dependencies and allow for easier dependency mocking.

The small interface problem

The abstract class is meant as a parent class for objects meant to be testable in a PHPUnit test case.
The main sugar methods the class implements at the moment are:

  • getMocksFor($methodNameOrArray)
  • getMocksArrayFor($methodNameOrArray)

and those static method signatures pretty much say it all. Previous versions of the methods did require more dependencies to be passed in, like a PHPUnit_Framework_TestCase to get the job done but those dependencies I’ve removed in the current iteration.

New usage possibilities

While the getMocksFor method will return a stdClass object the getMocksArrayFor method will return an array. Without repeating what’s in the package README file the latter method allows me a concise mocking I’m loving like

// file ClassOneTest.php

public function test_methodOne_will_call_methods(){

    extract(ClassOne::getMocksArrayFor(array('__construct', 'methodOne')));

    $A->expects($this->once())->method('method');
    $BInterface->expects($this->once())->method('method');
    $CInterface->expects($this->once())->method('method');
    $D->expects($this->once())->method('method');

    $sut = new ClassOne($D);

    $sut->methodOne();
}

given a class with properly defined and set up DocBlocks like

class ClassOne extends tad_TestableObject {

    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();
    }
}

But I do not want/can extend crappy tad_TestableObject class

Totally legit and me too I find myself in the same situation (leaning on the “can”) side though, and that’s why the package also packs the tad_DependencyMocker class.
Given the class ClassOne defined above but not extending the tad_TestableObject 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();
    }
}

I will be able to mock its dependencies nonetheless

// file ClassOneTest.php

public function test_methodOne_will_call_methods(){

    // $dm = new tad_DependencyMocker('ClassOne')
    // would do the same
    extract(tad_DependencyMocker::on('ClassOne')
        ->forMethods(array('__construct', 'methodOne'))
        ->getMocksArray());

    $A->expects($this->once())->method('method');
    $BInterface->expects($this->once())->method('method');
    $CInterface->expects($this->once())->method('method');
    $D->expects($this->once())->method('method');

    $sut = new ClassOne($D);

    $sut->methodOne();
}

and use and configure them as usual.

I appreciate your input