Ad hoc adapter classes 05

I’m trying to create a sustainable and effective way to develop WordPress plugins and themes in a TDD fashion using generated adapter classes to make test in isolation possible.

The story so far

Test-driven development is not the fastest programming technique around and it can become a chore when tools and coding styles do not align. To streamline my job as much as possible I’d like to be able to generate adapter classes wrapping only the methods a class will use in an attempt to make adapter usage as painless as possible in execution terms. I had drafted a usage scenario like this:

use tad\Generators\Adapter\AdapterClassGenerator as Generator;
use tad\Generators\Adapter\Utility\FunctionCallsCollector as CollectorAdapter;

class ClassUsingAdaptersTest extends \PHPUnit_Framework_TestCase{

    protected static $jsonFilePath = 'path/to/file.json';
    protected static $adapterFilePath = 'path/to/AdHocAdapter.php';

    /**
     * Will run once before any test runs
     * @beforeClass
     */
    public static function removePreviousAdapter(){
        if(file_exists(self::$adapterFilePath)){
            unlink(self::$adapterFilePath);
        }
    }

    public function setUp(){
        // the CollectorAdapter implements the `tad_Adapters_IFunction`
        // interface
        $this->collectorAdapter = new CollectorAdpater();
        $this->collectorAdapter->_setJsonFilePath(self::$jsonFilePath);
        $this->collectorAdapter->_shouldAppend(true);

        // either in the constructor or with an accessor method the 
        // class allows for the injection of an object implementing
        // the `tad_Adapters_IFunction` interface
        $sut = new ClassUsingAdapters();
        $sut->setAdapter($this->collectorAdapter);
    }

    public testOne(){
        // will call methods wrapping globally defined functions on the
        // adapter
        $this->sut->methodOne();
        ...
    }

    public testTwo(){
        // will call methods wrapping globally defined functions on the
        // adapter
        $this->sut->methodTwo();
        ...
    }

    ... more tests on more methods of ClassUsingAdapter

    /**
     * Will be called after all tests ran
     * @afterClass
     */
    public static function generateAdapter(){
        Generator::constructFromJson(self::$jsonFilePath)
            ->addMagicCall()
            ->setOutputFile(self::$adapterFilePath)
            ->generate();
    }
}

where the generation was contained in a test case. While this implementation works I’d like to make things easier.

Possible CLI workflow

I’d like to be able to run a command like this at the terminal:

twp adapter:wrap ClassOneUsingAdapter  ClassTwoUsingAdapter ClassThreeUsingAdapter -var wpf

In plain English

create an adapter class wrapping all the calls to globally defined functions the three classes make, the variable the classes will use to invoke the adapter is $this->wpf->...

I will venture next in the world of code parsing and will try to come up with a working implementation of the target interaction above.