Etsy sync WordPress plugin 06

Building a request for the Etsy WordPress plugin.

Requesting is the next step

The next step to move forward the development of the Etsy plugin is to build and send an actual request to the API to get some data back.
Sticking to TDD “rules” I’ve written a first test to be able to compile a request into the proper format.

use tad\FunctionMocker\FunctionMocker as Test; class ELH_RequestCompilerTest extends \PHPUnit_Framework_TestCase { protected function setUp() { Test::setUp(); } protected function tearDown() { Test::tearDown(); } /** * @test * it should be instantiatable */ public function it_should_be_instantiatable() { Test::assertInstanceOf( 'ELH_RequestCompiler', new ELH_RequestCompiler() ); } /** * @test * it should replace string parameters in the uri */ public function it_should_replace_string_parameters_in_the_uri() { $sut = new ELH_RequestCompiler(); $params = [ new ELH_ApiRequestParameter( 'foo', true, 'one', 'string' ), new ELH_ApiRequestParameter( 'baz', false, 10, 'int' ), new ELH_ApiRequestParameter( 'bar', false, 'none', 'string' ) ]; $request = Test::replace( 'ELH_ApiRequestInterface' ) ->method( 'parameters', $params ) ->method( 'uri', '/listings/:foo/shops' ) ->get(); $sut->set_request( $request ); $data = [ 'foo' => 'some_value', 'baz' => 'another_value', 'bar' => 'woo' ]; $out = $sut->get_compiled_request( $data ); $exp = '/listings/some_value/shops?baz=another_value&bar=woo'; Test::assertEquals( $exp, $out ); } } 

I’m testing little still but the two tests above did produce the class code below

class ELH_RequestCompiler { /** * @var ELH_ApiRequestInterface */ protected $request; public function set_request( ELH_ApiRequestInterface $request ) { $this->request = $request; } public function get_compiled_request( array $data = array() ) { $compiled = $this->request->uri(); $vars = array(); foreach ( $data as $key => $value ) { if ( strpos( $compiled, ':' . $key ) ) { $compiled = str_replace( ':' . $key, $value, $compiled ); } else { $vars[] = sprintf( '%s=%s', $key, $value ); } } $compiled .= '?' . implode( '&', $vars ); return $compiled; } } 

It’s a first step and it might seem like an overkill for the two simple requests I will be making (one for shops and the other for the listings) but the plugin might expand in functions in the future and the laid down structure will allow me the extension.

Next

I will complete the ELH_RequestCompiler class and methods while testing it and plug into the ELH_Synchronizer class.

I appreciate your input