Etsy sync WordPress plugin 03
April 9, 2015
Divide and conquer.
Stupid little objects
I've tried to delay the writing of the first test as much as possible.
The "game" is to avoid any logic and data manipulation until possible and that I can do breaking functionalities in as many little specialized classes as possible.
An example of such an object in the project is this one
class ELH_Api implements ELH_ApiInterface {
/**
* @var string
*/
protected $api_key = '';
public function get_api_key() {
return $this->api_key;
}
public function set_api_key( $api_key ) {
$this->api_key = $api_key;
}
}
it's just an information storage and that's meant to be set once and read later. On the same line a big part of the plugin is the ELH_Synchronizer
class
class ELH_Synchronizer implements ELH_SynchronizerInterface {
/**
* @var ELH_StepInterface
*/
protected $first_step;
/**
* @var ELH_StrategySelectorInterface
*/
protected $strategy_selector;
public static function instance( ELH_StrategySelectorInterface $strategy_selector ) {
$instance = new self;
$instance->set_strategy_selector( $strategy_selector );
return $instance;
}
public function set_first_step( ELH_StepInterface $step ) {
$this->first_step = $step;
return $this;
}
public function set_strategy_selector( ELH_StrategySelectorInterface $strategy_selector ) {
$this->strategy_selector = $strategy_selector;
}
public function sync() {
if ( empty( $this->first_step ) ) {
return;
}
try {
$status = new ELH_Status();
$this->first_step->set_status( $status );
$this->first_step->go();
} catch ( ELH_SyncException $e ) {
$strategy = $this->strategy_selector->get_strategy_for( $e, $status );
$strategy->set( 'exception', $e );
$strategy->set( 'status', $status );
$strategy->run();
}
}
}
that's delegating so much of its tasks and inner workings to look almost empty.
Testing the Synchronizer class
The small footprint of the Synchronizer
class will allow me to easily test it with little code and have its code covered by the tests below (sudo code, look here for the real test code)
<?php
use tad\FunctionMocker\FunctionMocker as Test;
class SynchronizerTest extends \PHPUnit_Framework_TestCase {
/**
* @test
* it should run the first step
*/
public function it_should_run_the_first_step();
/**
* @test
* it should set the status on the first step
*/
public function it_should_set_the_status_on_the_first_step();
/**
* @test
* it should capture sync exceptions and pass them to the strategy handler
*/
/**
* @test
* it should let the strategy selector handle the exception
*/
public function it_should_let_the_strategy_selector_handle_the_exception();
}
Next
I will TDD the classes the Synchronizer
is using and test it against some mock API responses.