Skip to content

Commit

Permalink
Upgrade guzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Sep 11, 2017
1 parent 62bd492 commit 98f65f5
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 69 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,5 @@ script:
- mkdir -p build/logs
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_script:
- vendor/bin/coveralls -v

matrix:
fast_finish: true
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
}
],
"require": {
"php": ">=5.4.0",
"guzzlehttp/guzzle": ">= 4.1 < 6",
"symfony/serializer": "~2.1",
"php": ">=7.0",
"guzzlehttp/guzzle": "~6.3",
"symfony/serializer": "~3.3",
"packaged/helpers": "~1.0",
"packaged/docblock": "~0",
"packaged/config": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.5",
"satooshi/php-coveralls": "~1.0"
"phpunit/phpunit": "~6.3"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 7 additions & 7 deletions src/Abstracts/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Promise\PromiseInterface;
use Packaged\Api\Format\JsonFormat;
use Packaged\Api\HttpVerb;
use Packaged\Api\Interfaces\ApiAwareInterface;
use Packaged\Api\Interfaces\ApiInterface;
use Packaged\Api\Interfaces\ApiRequestInterface;
use Packaged\Api\Interfaces\EndpointInterface;
use Packaged\Api\HttpVerb;
use Packaged\Helpers\Path;

abstract class AbstractApi extends AbstractDefinable implements ApiInterface
{
protected $_client;
protected $_guzzleConfig = [];

/**
* Bind this API to an instance
Expand All @@ -38,7 +38,7 @@ protected function _getClient()
{
if($this->_client === null)
{
$this->_client = new Client($this->_guzzleConfig);
$this->_client = new Client();
}

return $this->_client;
Expand Down Expand Up @@ -68,7 +68,7 @@ public function processRequest(ApiRequestInterface $request)
$time = microtime(true);
try
{
$response = $this->_getClient()->send($apiRequest);
$response = $apiRequest->wait();
}
catch(ClientException $e)
{
Expand Down Expand Up @@ -98,11 +98,11 @@ protected function _processResponse($response)
/**
* @param ApiRequestInterface $request
*
* @return \GuzzleHttp\Message\RequestInterface
* @return PromiseInterface
*/
protected function _createRequest(ApiRequestInterface $request)
{
return $this->_getClient()->createRequest(
return $this->_getClient()->requestAsync(
$request->getVerb(),
Path::buildUnix($this->getUrl(), $request->getPath())
. $request->getQueryString(),
Expand All @@ -121,7 +121,7 @@ protected function _makeOptions(ApiRequestInterface $request)

if($request->getVerb() == HttpVerb::POST)
{
$options['body'] = $request->getPostData();
$options['form_params'] = $request->getPostData();
}

return $options;
Expand Down
2 changes: 1 addition & 1 deletion src/Abstracts/AbstractApiFormat.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace Packaged\Api\Abstracts;

use GuzzleHttp\Message\ResponseInterface;
use Packaged\Api\Exceptions\InvalidApiResponseException;
use Packaged\Api\Interfaces\ApiFormatInterface;
use Packaged\Api\Response\ApiCallData;
use Packaged\Api\Response\ResponseBuilder;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;

Expand Down
2 changes: 1 addition & 1 deletion src/Abstracts/AbstractApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function hydrate($data)
*/
public static function make($data)
{
$response = new static;
$response = new static();
$response->hydrate($data);
return $response;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Abstracts/AbstractEndpoint.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace Packaged\Api\Abstracts;

use Packaged\Api\ApiRequest;
use Packaged\Api\HttpVerb;
use Packaged\Api\Interfaces\ApiAwareInterface;
use Packaged\Api\Interfaces\ApiInterface;
use Packaged\Api\Interfaces\ApiPayloadInterface;
use Packaged\Api\ApiRequest;
use Packaged\Api\Interfaces\EndpointInterface;
use Packaged\Api\HttpVerb;
use Packaged\Api\Traits\ApiAwareTrait;
use Packaged\Helpers\Objects;
use Packaged\Helpers\Path;
Expand Down Expand Up @@ -60,10 +60,7 @@ protected function _buildPath($path, ApiPayloadInterface $payload = null)
if(stristr($path, ':') && $payload !== null)
{
$find = array_map(
function ($value)
{
return ':' . $value;
},
function ($value) { return ':' . $value; },
array_keys($payload->toArray())
);
return str_replace($find, $payload->toArray(), $path);
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/ApiFormatInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Packaged\Api\Interfaces;

use GuzzleHttp\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface;

interface ApiFormatInterface
{
Expand Down
13 changes: 5 additions & 8 deletions src/Response/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public static function create(ApiCallData $data)
}

$interfaces = class_implements($type);
if($type == '\Exception'
|| $type === '\Packaged\Api\Exceptions\ApiException'
|| in_array('\Packaged\Api\Exceptions\ApiException', $interfaces)
if($type == \Exception::class
|| $type === ApiException::class
|| in_array(ApiException::class, $interfaces)
|| array_key_exists('Exception', class_parents($type))
)
{
Expand All @@ -45,12 +45,9 @@ public static function create(ApiCallData $data)
}
throw $exception;
}
else if(in_array(
'Packaged\Api\Interfaces\ApiResponseInterface',
$interfaces
))
else if(in_array(ApiResponseInterface::class, $interfaces))
{
$class = new $type;
$class = new $type();
/**
* @var $class \Packaged\Api\Interfaces\ApiResponseInterface
*/
Expand Down
103 changes: 67 additions & 36 deletions tests/ApiClientTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace Packaged\Api\Tests;

use GuzzleHttp\Post\PostBody;
use GuzzleHttp\Ring\Client\MockHandler;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use Packaged\Api\ApiRequest;
use Packaged\Api\Format\JsonFormat;
use Packaged\Api\HttpVerb;
Expand All @@ -12,8 +12,11 @@
use Packaged\Api\Tests\Support\MockHeaderResponse;
use Packaged\Api\Tests\Support\MockPayload;
use Packaged\Api\Tests\Support\MockResponse;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use function GuzzleHttp\Psr7\parse_query;

class ApiClientTest extends \PHPUnit_Framework_TestCase
class ApiClientTest extends TestCase
{
protected function _encode(
$result, $statusCode = 200, $statusMessage = '', $type = null
Expand All @@ -29,19 +32,51 @@ protected function _encode(

protected function _getApi($handlerResult, array $headers = null)
{
if(!is_callable($handlerResult))
if(is_callable($handlerResult))
{
$handlerResult = [
'body' => $this->_encode($handlerResult),
'status' => 200
];
$handler = new MockHandler(
[
function ($request) use ($handlerResult, $headers)
{
/** @var RequestInterface $request */
if(is_array($headers))
{
foreach($headers as $k => $v)
{
$request = $request->withAddedHeader($k, $v);
}
}
$resp = $handlerResult($request);
return new Response(
$resp['status'],
$headers ?: [],
$resp['body']
);
},
]
);
}
$handler = new MockHandler($handlerResult);
$config = ['handler' => $handler];
if($headers)
else
{
$config['defaults']['headers'] = $headers;
if(is_object($handlerResult))
{
$handlerResult = [
'body' => $this->_encode($handlerResult),
'status' => 200,
];
}
$handler = new MockHandler(
[
new Response(
$handlerResult['status'],
$headers ?: [],
$handlerResult['body']
),
]
);
}

$config = ['handler' => $handler];
return new MockApi('http://www.test.com', $config);
}

Expand All @@ -59,10 +94,7 @@ public function testGet()

$endpoint = $this->_getEndpoint($response);
$apiResult = $endpoint->getRequest()->get();
$this->assertInstanceOf(
'\Packaged\Api\Tests\Support\MockResponse',
$apiResult
);
$this->assertInstanceOf(MockResponse::class, $apiResult);
$this->assertEquals($response->toArray(), $apiResult->toArray());
}

Expand All @@ -71,12 +103,14 @@ public function testPost()
$endpoint = $this->_getEndpoint(
function ($request)
{
$body = $request['body'];
if($body instanceof PostBody)
/** @var RequestInterface $request */
$body = $request->getBody()->getContents();
$params = parse_query($body);
if(!empty($params))
{
return [
'body' => $this->_encode(MockResponse::make($body->getFields())),
'status' => 200
'body' => $this->_encode(MockResponse::make($params)),
'status' => 200,
];
}
return null;
Expand All @@ -87,10 +121,7 @@ function ($request)
$payload->key1 = 'value';
$payload->key2 = 'vals';
$apiResult = $endpoint->getRequest($payload, '/', HttpVerb::POST)->get();
$this->assertInstanceOf(
'\Packaged\Api\Tests\Support\MockResponse',
$apiResult
);
$this->assertInstanceOf(MockResponse::class, $apiResult);
$this->assertEquals($payload->toArray(), $apiResult->toArray());
}

Expand All @@ -99,11 +130,12 @@ public function testGlobalHeaders()
$endpoint = $this->_getEndpoint(
function ($request)
{
/** @var RequestInterface $request */
return [
'body' => (new JsonFormat())->encode(
MockHeaderResponse::make($request['headers'])
MockHeaderResponse::make($request->getHeaders())
),
'status' => 200
'status' => 200,
];
},
['header1' => 'val', 'head2' => 'val2']
Expand All @@ -113,7 +145,7 @@ function ($request)
* @var MockHeaderResponse $apiResult
*/
$this->assertInstanceOf(
'\Packaged\Api\Tests\Support\MockHeaderResponse',
MockHeaderResponse::class,
$apiResult
);
$this->assertArrayHasKey('header1', $apiResult->toArray());
Expand All @@ -137,17 +169,16 @@ public function testException()
$toThrow = new MockException('Oops', 1050);
$toThrow->errorValue = 'test value';
$endpoint = $this->_getEndpoint(
function () use ($toThrow)
{
return [
'body' => $toThrow->getFormatted(new JsonFormat()),
'status' => 200
];
}
[
'body' => $toThrow->getFormatted(new JsonFormat()),
'status' => 200,
]
);
$payload = new MockPayload();
$payload->key1 = 'value';
$payload->key2 = 'vals';
$payload->hydrate(['key1' => 'value']);
$key2 = new \stdClass();
$key2->key2 = 'vals';
$payload->hydrate($key2);
$e = null;
try
{
Expand Down
17 changes: 17 additions & 0 deletions tests/Support/MockApi.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
<?php
namespace Packaged\Api\Tests\Support;

use GuzzleHttp\Client;
use Packaged\Api\Abstracts\AbstractApi;

class MockApi extends AbstractApi
{
protected $_url;
protected $_guzzleConfig = [];

function __construct($url, array $guzzleConfig = [])
{
$this->_url = $url;
$this->_guzzleConfig = $guzzleConfig;
}

/**
* Retrieve the Guzzle HTTP Client
*
* @return Client
*/
protected function _getClient()
{
if($this->_client === null)
{
$this->_client = new Client($this->_guzzleConfig);
}

return $this->_client;
}

/**
* Retrieve the url for the API
*
Expand Down
Loading

0 comments on commit 98f65f5

Please sign in to comment.