Skip to content

Commit

Permalink
Adds traits for better abstraction, hopefully
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Jan 10, 2016
1 parent ce1492b commit 7a53ae8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
24 changes: 7 additions & 17 deletions tests/ClientTest.php
Expand Up @@ -3,18 +3,17 @@
namespace Shutterstock\Api;

use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit_Framework_TestCase;
use ReflectionClass;

class ClientTest extends PHPUnit_Framework_TestCase
{

use MockClientTrait, MockHandlerTrait, SetMockHandlerTrait;

public function testIsInstanceOfClient()
{
$client = $this->newClient();
$client = $this->getClient();

$this->assertInstanceOf(
'Shutterstock\Api\Client',
Expand All @@ -40,7 +39,7 @@ public function testConstructSetsGuzzle()

public function testGetImages()
{
$client = $this->newClient();
$client = $this->getClient();
$expectedImageResource = new Resource\Images($client);

$imageResource = $client->getImages();
Expand All @@ -57,13 +56,9 @@ public function testGetImages()
*/
public function testRequest($method, $uri, $options, Response $response, $compiledUri)
{
$mockHandler = new MockHandler([$response]);
$client = $this->newClient();

$reflectedClient = new ReflectionClass($client);
$reflectedProperty = $reflectedClient->getProperty('guzzle');
$reflectedProperty->setAccessible(true);
$reflectedProperty->setValue($client, new Guzzle(['handler' => HandlerStack::create($mockHandler)]));
$mockHandler = $this->getMockHandler($response);
$client = $this->getClient();
$this->setGuzzleWithMockHandler($client, $mockHandler);

$testResponse = $client->request($method, $uri, $options);
$lastRequest = $mockHandler->getLastRequest();
Expand All @@ -85,9 +80,4 @@ public function dataRequests()
],
];
}

protected function newClient()
{
return new Client('client_id', 'client_secret');
}
}
9 changes: 9 additions & 0 deletions tests/MockClientTrait.php
@@ -0,0 +1,9 @@
<?php

namespace Shutterstock\Api;

trait MockClientTrait {
protected function getClient() {
return new Client('client_id', 'client_secret');
}
}
15 changes: 15 additions & 0 deletions tests/MockHandlerTrait.php
@@ -0,0 +1,15 @@
<?php

namespace Shutterstock\Api;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;

trait MockHandlerTrait {
protected function getMockHandler(Response $response = null) {
if (is_null($response)) {
$response = new Response(200, [], json_encode([]));
}
return new MockHandler([$response]);
}
}
13 changes: 5 additions & 8 deletions tests/Resource/AbstractResourceTest.php
Expand Up @@ -4,11 +4,13 @@

use PHPUnit_Framework_TestCase;
use ReflectionClass;
use Shutterstock\Api\Client;
use Shutterstock\Api\MockClientTrait;

class AbstractResourceTest extends PHPUnit_Framework_TestCase
{

use MockClientTrait;

public function testIsInstanceOfResource()
{
$abstractResource = $this->mockAbstractResource();
Expand All @@ -21,7 +23,7 @@ public function testIsInstanceOfResource()

public function testConstructSetsClient()
{
$client = $this->newClient();
$client = $this->getClient();
$abstractResource = $this->mockAbstractResource();

$this->assertAttributeInstanceOf(
Expand Down Expand Up @@ -56,12 +58,7 @@ protected function mockAbstractResource()
{
return $this->getMockForAbstractClass(
'Shutterstock\Api\Resource\AbstractResource',
[$this->newClient()]
[$this->getClient()]
);
}

protected function newClient()
{
return new Client('client_id', 'client_secret');
}
}
21 changes: 21 additions & 0 deletions tests/SetMockHandlerTrait.php
@@ -0,0 +1,21 @@
<?php

namespace Shutterstock\Api;

use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use ReflectionClass;

trait SetMockHandlerTrait {
protected function setGuzzleWithMockHandler(Client $client, MockHandler $mockHandler) {
$options = [
'handler' => HandlerStack::create($mockHandler),
];
$reflectedClient = new ReflectionClass($client);
$reflectedProperty = $reflectedClient->getProperty('guzzle');
$reflectedProperty->setAccessible(true);
$reflectedProperty->setValue($client, new Guzzle($options));
return true;
}
}

0 comments on commit 7a53ae8

Please sign in to comment.