Testable WordPress objects

I’ve added an abstract class to the tdd-helpers package to allow for easier method stubbing in tests.
I’ve added an abstract class to the I’ve added an abstract class to the tdd-helpers package to allow for easier method stubbing in WordPress objects testing using some reflection magic.

The problem

I’ve used two adapter classes, tad_FunctionsAdapter and tad_GlobalsAdapter in all of my WordPress projects to allow me to decouple my objects from the underlying globally defined functions and variables WordPress will set up in its run cycle.
In simple terms it means that in place of

apply_filters('some_filter', array($this, 'aMethod')); 

I’ve written

$this->f->apply_filters('some_filter', array($this, 'aMethod')); 

and for globals in place of

$name = $_GET['name']; 

I’ve written

$name = $this->g->get('name'); 

and while this ensures decoupling and isolated testing possibility it also makes stubbing all the calls an object method makes a chore: while that’s part of testing I’ve tried to batch some of the repeating tasks creating a parent abstract class.

A solution

The tad_TestableObject class defines some common methods I’ve been writing over and over in my objects

setFunctionsAdapter(tad_FunctionsAdapterInterface $f = null); setGlobalsAdapter(tad_GlobalsAdapterInterface $g = null); getFunctionsAdapter(); getGlobalsAdapter(); 

and two static methods aimed at making stubbing adapter methods a breeze provided some DocBlock love is in place.
Quoting the README file, given a class like

class MyTestableClass extends tad_TestableObject { /** * @f functionOne functionTwo * @g server */ public function methodOne() { ... $a = $this->f->functionOne(); ... $b = $this->f->functionTwo(); ... $c = $this->g->server('some'); } /** * @f functionThree functionFour * @g globals */ public function methodTwo() { ... $a = $this->f->functionThree(); ... $b = $this->f->functionFour(); ... $c = $this->g->globals('value'); } } 

the class will use the DocBlock to create ad hoc mocks during tests and it’s meant to be used inside a PHPUnit_Framework_TestCase class definition

// $mockF will define the stub methods // '__call' // 'functionOne' // 'functionTwo' // 'functionThree' // 'functionFour' $mockF = MyTestableClass::getMockFunctions($this); // $mockG will define the methods // '__call' // 'server' // 'globals' $mockG = MyTestableClass::getMockGlobals($this); 

those stubs will then be configurable as any test double produced using the PHPUnit_Framework_TestCase::getMock() method.
Specifying a method name, or an array of method names, will produce a mock stubbing adapter methods used in the specified methods alone

// $mockF will define the stub methods // '__call' // 'functionOne' // 'functionTwo' $mockF = MyTestableClass::getMockFunctions($this, 'methodOne'); // $mockG will define the methods // '__call' // 'server' $mockG = MyTestableClass::getMockGlobals($this, 'methodOne'); 

While I’ve tested the class a little I will put it to use and maybe come up with more sugar methods as the need arises.

I appreciate your input