From 98f65f50a69fca3a816fefd293f236acdd205769 Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Mon, 11 Sep 2017 14:10:11 +0100 Subject: [PATCH] Upgrade guzzle --- .travis.yml | 3 - composer.json | 9 +-- src/Abstracts/AbstractApi.php | 14 ++-- src/Abstracts/AbstractApiFormat.php | 2 +- src/Abstracts/AbstractApiResponse.php | 2 +- src/Abstracts/AbstractEndpoint.php | 9 +-- src/Interfaces/ApiFormatInterface.php | 2 +- src/Response/ResponseBuilder.php | 13 ++-- tests/ApiClientTest.php | 103 +++++++++++++++++--------- tests/Support/MockApi.php | 17 +++++ tests/ValidationTest.php | 3 +- 11 files changed, 108 insertions(+), 69 deletions(-) diff --git a/.travis.yml b/.travis.yml index a3175bd..d02a808 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/composer.json b/composer.json index 9e604a5..0c2b649 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Abstracts/AbstractApi.php b/src/Abstracts/AbstractApi.php index 0204604..ced9f10 100644 --- a/src/Abstracts/AbstractApi.php +++ b/src/Abstracts/AbstractApi.php @@ -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 @@ -38,7 +38,7 @@ protected function _getClient() { if($this->_client === null) { - $this->_client = new Client($this->_guzzleConfig); + $this->_client = new Client(); } return $this->_client; @@ -68,7 +68,7 @@ public function processRequest(ApiRequestInterface $request) $time = microtime(true); try { - $response = $this->_getClient()->send($apiRequest); + $response = $apiRequest->wait(); } catch(ClientException $e) { @@ -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(), @@ -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; diff --git a/src/Abstracts/AbstractApiFormat.php b/src/Abstracts/AbstractApiFormat.php index e541f34..f732697 100644 --- a/src/Abstracts/AbstractApiFormat.php +++ b/src/Abstracts/AbstractApiFormat.php @@ -1,11 +1,11 @@ hydrate($data); return $response; } diff --git a/src/Abstracts/AbstractEndpoint.php b/src/Abstracts/AbstractEndpoint.php index 420b311..cb8d357 100644 --- a/src/Abstracts/AbstractEndpoint.php +++ b/src/Abstracts/AbstractEndpoint.php @@ -1,12 +1,12 @@ toArray()) ); return str_replace($find, $payload->toArray(), $path); diff --git a/src/Interfaces/ApiFormatInterface.php b/src/Interfaces/ApiFormatInterface.php index 2bdff67..4217e9a 100644 --- a/src/Interfaces/ApiFormatInterface.php +++ b/src/Interfaces/ApiFormatInterface.php @@ -1,7 +1,7 @@ $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); } @@ -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()); } @@ -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; @@ -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()); } @@ -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'] @@ -113,7 +145,7 @@ function ($request) * @var MockHeaderResponse $apiResult */ $this->assertInstanceOf( - '\Packaged\Api\Tests\Support\MockHeaderResponse', + MockHeaderResponse::class, $apiResult ); $this->assertArrayHasKey('header1', $apiResult->toArray()); @@ -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 { diff --git a/tests/Support/MockApi.php b/tests/Support/MockApi.php index c29ccbe..e1f3ab9 100644 --- a/tests/Support/MockApi.php +++ b/tests/Support/MockApi.php @@ -1,11 +1,13 @@ _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 * diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index 1e1c097..eff7957 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -2,8 +2,9 @@ namespace Packaged\Api\Tests; use Packaged\Api\Tests\Support\MockPayload; +use PHPUnit\Framework\TestCase; -class ValidationTest extends \PHPUnit_Framework_TestCase +class ValidationTest extends TestCase { /** * @expectedException \Packaged\Api\Exceptions\PayloadValidationException