From 2b7632a03e67e7ba678cfc34f741a79c4adba584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sun, 3 May 2015 17:36:47 +0200 Subject: [PATCH 01/12] Initial import --- composer.json | 12 +- phpunit.xml.dist | 3 + src/Guzzle5HttpAdapter.php | 171 ++++++++++++++++++++++++++ tests/Guzzle5HttpAdapterTest.php | 43 +++++++ tests/Guzzle5MultiHttpAdapterTest.php | 28 +++++ 5 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 src/Guzzle5HttpAdapter.php create mode 100644 tests/Guzzle5HttpAdapterTest.php create mode 100644 tests/Guzzle5MultiHttpAdapterTest.php diff --git a/composer.json b/composer.json index a6044fa..dfac6eb 100644 --- a/composer.json +++ b/composer.json @@ -12,10 +12,13 @@ ], "require": { "php": ">=5.4", - "php-http/adapter-core": "dev-master" + "ext-curl": "*", + "php-http/adapter-core": "dev-initial_import", + "guzzlehttp/guzzle": "~5.0", + "guzzlehttp/ringphp": "^1.0.8@dev" }, "require-dev": { - "phpunit/phpunit": "~4.4" + "php-http/adapter-integration-tests": "dev-master" }, "provide": { "php-http/adapter-implementation": "0.1" @@ -25,6 +28,11 @@ "Http\\Adapter\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Http\\Adapter\\Tests\\": "tests/" + } + }, "extra": { "branch-alias": { "dev-master": "0.1-dev" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fd71e68..fe68fd5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,6 +5,9 @@ tests/ + + + src/ diff --git a/src/Guzzle5HttpAdapter.php b/src/Guzzle5HttpAdapter.php new file mode 100644 index 0000000..ece431e --- /dev/null +++ b/src/Guzzle5HttpAdapter.php @@ -0,0 +1,171 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Http\Adapter; + +use GuzzleHttp\Client; +use GuzzleHttp\ClientInterface; +use GuzzleHttp\Event\CompleteEvent; +use GuzzleHttp\Event\ErrorEvent; +use GuzzleHttp\Message\RequestInterface; +use GuzzleHttp\Pool; +use Http\Adapter\Message\InternalRequestInterface; +use Http\Adapter\Normalizer\BodyNormalizer; + +/** + * @author GeLo + */ +class Guzzle5HttpAdapter extends CurlHttpAdapter +{ + /** + * @var ClientInterface + */ + private $client; + + /** + * + * @param ClientInterface|null $client + * @param ConfigurationInterface|null $configuration + */ + public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null) + { + parent::__construct($configuration); + + $this->client = $client ?: new Client(); + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'guzzle5'; + } + + /** + * {@inheritdoc} + */ + protected function sendInternalRequest(InternalRequestInterface $internalRequest) + { + try { + $response = $this->client->send($this->createRequest($internalRequest)); + } catch (\Exception $e) { + var_dump($e); exit; + throw HttpAdapterException::cannotFetchUri( + $e->getRequest()->getUrl(), + $this->getName(), + $e->getMessage() + ); + } + + return $this->getConfiguration()->getMessageFactory()->createResponse( + (integer) $response->getStatusCode(), + $response->getProtocolVersion(), + $response->getHeaders(), + BodyNormalizer::normalize( + function () use ($response) { + return $response->getBody()->detach(); + }, + $internalRequest->getMethod() + ) + ); + } + + /** + * {@inheritdoc} + */ + protected function sendInternalRequests(array $internalRequests, $success, $error) + { + $requests = []; + foreach ($internalRequests as $internalRequest) { + $requests[] = $this->createRequest($internalRequest, $success, $error); + } + + Pool::batch($this->client, $requests); + } + + /** + * {@inheritdoc} + */ + protected function createFile($file) + { + return fopen($file, 'r'); + } + + /** + * Creates a request. + * + * @param InternalRequestInterface $internalRequest + * @param callable|null $success + * @param callable|null $error + * + * @return RequestInterface + */ + private function createRequest(InternalRequestInterface $internalRequest, callable $success = null, callable $error = null) + { + $request = $this->client->createRequest( + $internalRequest->getMethod(), + (string) $internalRequest->getUri(), + [ + 'exceptions' => false, + 'allow_redirects' => false, + 'timeout' => $this->getConfiguration()->getTimeout(), + 'connect_timeout' => $this->getConfiguration()->getTimeout(), + 'version' => $internalRequest->getProtocolVersion(), + 'headers' => $this->prepareHeaders($internalRequest), + 'body' => $this->prepareContent($internalRequest), + ] + ); + + if (isset($success)) { + $messageFactory = $this->getConfiguration()->getMessageFactory(); + + $request->getEmitter()->on( + 'complete', + function (CompleteEvent $event) use ($success, $internalRequest, $messageFactory) { + $response = $messageFactory->createResponse( + (integer) $event->getResponse()->getStatusCode(), + $event->getResponse()->getProtocolVersion(), + $event->getResponse()->getHeaders(), + BodyNormalizer::normalize( + function () use ($event) { + return $event->getResponse()->getBody()->detach(); + }, + $internalRequest->getMethod() + ) + ); + + $response = $response->withParameter('request', $internalRequest); + call_user_func($success, $response); + } + ); + } + + if (isset($error)) { + $httpAdapterName = $this->getName(); + + $request->getEmitter()->on( + 'error', + function (ErrorEvent $event) use ($error, $internalRequest, $httpAdapterName) { + $exception = HttpAdapterException::cannotFetchUri( + $event->getException()->getRequest()->getUrl(), + $httpAdapterName, + $event->getException()->getMessage() + ); + $exception->setRequest($internalRequest); + call_user_func($error, $exception); + } + ); + } + + return $request; + } +} diff --git a/tests/Guzzle5HttpAdapterTest.php b/tests/Guzzle5HttpAdapterTest.php new file mode 100644 index 0000000..209e44b --- /dev/null +++ b/tests/Guzzle5HttpAdapterTest.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Http\Adapter\Tests; + +use GuzzleHttp\Client; +use GuzzleHttp\Ring\Client\CurlHandler; +use Http\Adapter\Guzzle5HttpAdapter; + +/** + * @author GeLo + */ +class Guzzle5HttpAdapterTest extends HttpAdapterTest +{ + public function testGetName() + { + $this->assertSame('guzzle5', $this->httpAdapter->getName()); + } + + /** + * {@inheritdoc} + */ + protected function createHttpAdapter() + { + return new Guzzle5HttpAdapter(new Client(['handler' => $this->createHandler()])); + } + + /** + * Returns a handler for the client + */ + protected function createHandler() + { + return new CurlHandler(); + } +} diff --git a/tests/Guzzle5MultiHttpAdapterTest.php b/tests/Guzzle5MultiHttpAdapterTest.php new file mode 100644 index 0000000..ef0fd85 --- /dev/null +++ b/tests/Guzzle5MultiHttpAdapterTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Http\Adapter\Tests; + +use GuzzleHttp\Ring\Client\CurlMultiHandler; + +/** + * @author GeLo + */ +class Guzzle5MultiHttpAdapterTest extends Guzzle5HttpAdapterTest +{ + /** + * Returns a handler for the client + */ + protected function createHandler() + { + return new CurlMultiHandler(); + } +} From 2b3b65dd3dfe92666c821b034a7f4f4fe63b412b Mon Sep 17 00:00:00 2001 From: Eric GELOEN Date: Sun, 3 May 2015 21:41:25 +0200 Subject: [PATCH 02/12] Make tests full and green --- .travis.yml | 1 + src/Guzzle5HttpAdapter.php | 1 - tests/Guzzle5CurlHttpAdapterTest.php | 28 +++++++++++++++++++ tests/Guzzle5HttpAdapterTest.php | 10 +++---- ...hp => Guzzle5MultiCurlHttpAdapterTest.php} | 4 +-- tests/Guzzle5StreamHttpAdapterTest.php | 28 +++++++++++++++++++ 6 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 tests/Guzzle5CurlHttpAdapterTest.php rename tests/{Guzzle5MultiHttpAdapterTest.php => Guzzle5MultiCurlHttpAdapterTest.php} (81%) create mode 100644 tests/Guzzle5StreamHttpAdapterTest.php diff --git a/.travis.yml b/.travis.yml index 89e2828..fdf421b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,7 @@ matrix: install: - travis_retry composer self-update - travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction + - php -S 127.0.0.1:10000 -t vendor/php-http/adapter-integration-tests/fixture script: vendor/bin/phpunit ${PHPUNIT_FLAGS} diff --git a/src/Guzzle5HttpAdapter.php b/src/Guzzle5HttpAdapter.php index ece431e..cd1bba2 100644 --- a/src/Guzzle5HttpAdapter.php +++ b/src/Guzzle5HttpAdapter.php @@ -58,7 +58,6 @@ protected function sendInternalRequest(InternalRequestInterface $internalRequest try { $response = $this->client->send($this->createRequest($internalRequest)); } catch (\Exception $e) { - var_dump($e); exit; throw HttpAdapterException::cannotFetchUri( $e->getRequest()->getUrl(), $this->getName(), diff --git a/tests/Guzzle5CurlHttpAdapterTest.php b/tests/Guzzle5CurlHttpAdapterTest.php new file mode 100644 index 0000000..929fde2 --- /dev/null +++ b/tests/Guzzle5CurlHttpAdapterTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Http\Adapter\Tests; + +use GuzzleHttp\Ring\Client\CurlHandler; + +/** + * @author GeLo + */ +class Guzzle5CurlHttpAdapterTest extends Guzzle5HttpAdapterTest +{ + /** + * {@inheritdoc} + */ + protected function createHandler() + { + return new CurlHandler(); + } +} diff --git a/tests/Guzzle5HttpAdapterTest.php b/tests/Guzzle5HttpAdapterTest.php index 209e44b..b1bb62c 100644 --- a/tests/Guzzle5HttpAdapterTest.php +++ b/tests/Guzzle5HttpAdapterTest.php @@ -12,13 +12,12 @@ namespace Http\Adapter\Tests; use GuzzleHttp\Client; -use GuzzleHttp\Ring\Client\CurlHandler; use Http\Adapter\Guzzle5HttpAdapter; /** * @author GeLo */ -class Guzzle5HttpAdapterTest extends HttpAdapterTest +abstract class Guzzle5HttpAdapterTest extends HttpAdapterTest { public function testGetName() { @@ -35,9 +34,8 @@ protected function createHttpAdapter() /** * Returns a handler for the client + * + * @return object */ - protected function createHandler() - { - return new CurlHandler(); - } + abstract protected function createHandler(); } diff --git a/tests/Guzzle5MultiHttpAdapterTest.php b/tests/Guzzle5MultiCurlHttpAdapterTest.php similarity index 81% rename from tests/Guzzle5MultiHttpAdapterTest.php rename to tests/Guzzle5MultiCurlHttpAdapterTest.php index ef0fd85..2083cab 100644 --- a/tests/Guzzle5MultiHttpAdapterTest.php +++ b/tests/Guzzle5MultiCurlHttpAdapterTest.php @@ -16,10 +16,10 @@ /** * @author GeLo */ -class Guzzle5MultiHttpAdapterTest extends Guzzle5HttpAdapterTest +class Guzzle5MultiCurlHttpAdapterTest extends Guzzle5HttpAdapterTest { /** - * Returns a handler for the client + * {@inheritdoc} */ protected function createHandler() { diff --git a/tests/Guzzle5StreamHttpAdapterTest.php b/tests/Guzzle5StreamHttpAdapterTest.php new file mode 100644 index 0000000..21bc528 --- /dev/null +++ b/tests/Guzzle5StreamHttpAdapterTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Http\Adapter\Tests; + +use GuzzleHttp\Ring\Client\StreamHandler; + +/** + * @author GeLo + */ +class Guzzle5StreamHttpAdapterTest extends Guzzle5HttpAdapterTest +{ + /** + * {@inheritdoc} + */ + protected function createHandler() + { + return new StreamHandler(); + } +} From 60668b6fdcd25e3301909e3188f0e3d1e515e4ee Mon Sep 17 00:00:00 2001 From: Eric GELOEN Date: Sun, 3 May 2015 21:46:14 +0200 Subject: [PATCH 03/12] Fix PHP web server run --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index fdf421b..b6d6275 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ matrix: install: - travis_retry composer self-update - travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction - - php -S 127.0.0.1:10000 -t vendor/php-http/adapter-integration-tests/fixture + - php -S 127.0.0.1:10000 -t vendor/php-http/adapter-integration-tests/fixture > /dev/null 2>&1 & script: vendor/bin/phpunit ${PHPUNIT_FLAGS} From eb6aaf11ca1c5ecf28d88841ff91f15552e84ade Mon Sep 17 00:00:00 2001 From: Eric GELOEN Date: Sun, 3 May 2015 22:07:41 +0200 Subject: [PATCH 04/12] CurlHandler is only testable with PHP >= 5.5 --- tests/Guzzle5CurlHttpAdapterTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Guzzle5CurlHttpAdapterTest.php b/tests/Guzzle5CurlHttpAdapterTest.php index 929fde2..d6a9065 100644 --- a/tests/Guzzle5CurlHttpAdapterTest.php +++ b/tests/Guzzle5CurlHttpAdapterTest.php @@ -18,6 +18,18 @@ */ class Guzzle5CurlHttpAdapterTest extends Guzzle5HttpAdapterTest { + /** + * {@inheritdoc} + */ + protected function setUp() + { + if (PHP_VERSION_ID < 50500) { + $this->markTestSkipped(); + } + + parent::setUp(); + } + /** * {@inheritdoc} */ From d0c78bc7165b551656d48899144498e4ecb31962 Mon Sep 17 00:00:00 2001 From: Eric GELOEN Date: Sun, 3 May 2015 22:26:43 +0200 Subject: [PATCH 05/12] Refine Guzzle version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index dfac6eb..0317e1e 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "php": ">=5.4", "ext-curl": "*", "php-http/adapter-core": "dev-initial_import", - "guzzlehttp/guzzle": "~5.0", + "guzzlehttp/guzzle": "^5.0.1", "guzzlehttp/ringphp": "^1.0.8@dev" }, "require-dev": { From 8a6b261f2b436cb7070525e3cf7b2a1cae8f1606 Mon Sep 17 00:00:00 2001 From: Eric GELOEN Date: Sun, 3 May 2015 22:37:11 +0200 Subject: [PATCH 06/12] Refine guzzle version (this time its good) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0317e1e..52c1731 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "php": ">=5.4", "ext-curl": "*", "php-http/adapter-core": "dev-initial_import", - "guzzlehttp/guzzle": "^5.0.1", + "guzzlehttp/guzzle": "~5.1", "guzzlehttp/ringphp": "^1.0.8@dev" }, "require-dev": { From f3b04ec9b66e3f9c3a9c013e8460584a5b00eeb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Mon, 4 May 2015 14:48:38 +0200 Subject: [PATCH 07/12] Use short server command --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b6d6275..79418fe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,9 @@ matrix: install: - travis_retry composer self-update - travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction - - php -S 127.0.0.1:10000 -t vendor/php-http/adapter-integration-tests/fixture > /dev/null 2>&1 & + +before_script: + - vendor/bin/http_test_server > /dev/null 2>&1 & script: vendor/bin/phpunit ${PHPUNIT_FLAGS} From 8fdeef63ffc60efacce264c8de6cc01e506f6719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Mon, 4 May 2015 14:58:04 +0200 Subject: [PATCH 08/12] One of the tests PHP 5.5 --- tests/Guzzle5CurlHttpAdapterTest.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/tests/Guzzle5CurlHttpAdapterTest.php b/tests/Guzzle5CurlHttpAdapterTest.php index d6a9065..d11f226 100644 --- a/tests/Guzzle5CurlHttpAdapterTest.php +++ b/tests/Guzzle5CurlHttpAdapterTest.php @@ -14,22 +14,12 @@ use GuzzleHttp\Ring\Client\CurlHandler; /** + * @requires PHP 5.5 + * * @author GeLo */ class Guzzle5CurlHttpAdapterTest extends Guzzle5HttpAdapterTest { - /** - * {@inheritdoc} - */ - protected function setUp() - { - if (PHP_VERSION_ID < 50500) { - $this->markTestSkipped(); - } - - parent::setUp(); - } - /** * {@inheritdoc} */ From ef8f05ae62c9c1d0b581f7d62b35840abf212ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Thu, 14 May 2015 01:36:01 +0200 Subject: [PATCH 09/12] Updates git configs --- .gitattributes | 16 ++++++++-------- .gitignore | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.gitattributes b/.gitattributes index f8173ef..eecf37d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,8 +1,8 @@ -/tests export-ignore -/.editorconfig export-ignore -/.gitattributes export-ignore -/.gitignore export-ignore -/.scrutinizer.yml export-ignore -/.travis.yml export-ignore -/CONTRIBUTING.md export-ignore -/phpunit.xml.dist export-ignore +tests/ export-ignore +.editorconfig export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.scrutinizer.yml export-ignore +.travis.yml export-ignore +CONTRIBUTING.md export-ignore +phpunit.xml.dist export-ignore diff --git a/.gitignore b/.gitignore index bf5f069..e45d856 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/build -/vendor -/composer.lock -/phpunit.xml +build/ +vendor/ +composer.lock +phpunit.xml From abe8f856d79e630dac554a578503408e07a24072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Thu, 14 May 2015 01:39:06 +0200 Subject: [PATCH 10/12] Catches the proper exception --- src/Guzzle5HttpAdapter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Guzzle5HttpAdapter.php b/src/Guzzle5HttpAdapter.php index cd1bba2..db6f969 100644 --- a/src/Guzzle5HttpAdapter.php +++ b/src/Guzzle5HttpAdapter.php @@ -15,6 +15,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Event\CompleteEvent; use GuzzleHttp\Event\ErrorEvent; +use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Message\RequestInterface; use GuzzleHttp\Pool; use Http\Adapter\Message\InternalRequestInterface; @@ -57,7 +58,7 @@ protected function sendInternalRequest(InternalRequestInterface $internalRequest { try { $response = $this->client->send($this->createRequest($internalRequest)); - } catch (\Exception $e) { + } catch (RequestException $e) { throw HttpAdapterException::cannotFetchUri( $e->getRequest()->getUrl(), $this->getName(), From 1b4dd2beb7bb868c927d0153d72947bce1b44d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Thu, 14 May 2015 01:41:26 +0200 Subject: [PATCH 11/12] Updates dependencies --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 52c1731..d4c8111 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,12 @@ "require": { "php": ">=5.4", "ext-curl": "*", - "php-http/adapter-core": "dev-initial_import", + "php-http/adapter-core": "0.1.*@dev", "guzzlehttp/guzzle": "~5.1", "guzzlehttp/ringphp": "^1.0.8@dev" }, "require-dev": { - "php-http/adapter-integration-tests": "dev-master" + "php-http/adapter-integration-tests": "0.1.*@dev" }, "provide": { "php-http/adapter-implementation": "0.1" From a7313a2e1916f786d1a50215a70f02ad579a6993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sat, 16 May 2015 12:03:24 +0200 Subject: [PATCH 12/12] Moves curl dependency to require-dev --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d4c8111..ccc969b 100644 --- a/composer.json +++ b/composer.json @@ -12,12 +12,12 @@ ], "require": { "php": ">=5.4", - "ext-curl": "*", "php-http/adapter-core": "0.1.*@dev", "guzzlehttp/guzzle": "~5.1", "guzzlehttp/ringphp": "^1.0.8@dev" }, "require-dev": { + "ext-curl": "*", "php-http/adapter-integration-tests": "0.1.*@dev" }, "provide": {