From 7d014aab9d13d4cd57b1ae9ce5033439b6f96c8d Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Mon, 9 Nov 2015 13:27:23 +0100 Subject: [PATCH] Replace fake async client with the utils one --- composer.json | 3 +- spec/CookiePluginSpec.php | 4 +-- spec/ErrorPluginSpec.php | 8 ++--- spec/RedirectPluginSpec.php | 18 ++++------ spec/RetryPluginSpec.php | 15 ++++----- spec/StopwatchPluginSpec.php | 4 +-- src/FakeAsyncClient.php | 37 -------------------- src/FulfilledPromise.php | 65 ------------------------------------ src/PluginClient.php | 3 +- src/RejectedPromise.php | 60 --------------------------------- 10 files changed, 26 insertions(+), 191 deletions(-) delete mode 100644 src/FakeAsyncClient.php delete mode 100644 src/FulfilledPromise.php delete mode 100644 src/RejectedPromise.php diff --git a/composer.json b/composer.json index 9dbc2f5..a78c21d 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "require": { "php": ">=5.4", "php-http/httplug": "^1.0", - "php-http/httplug-async": "^0.1" + "php-http/httplug-async": "^0.1", + "php-http/utils": "^0.1@dev" }, "require-dev": { "phpspec/phpspec": "^2.2", diff --git a/spec/CookiePluginSpec.php b/spec/CookiePluginSpec.php index 4f87165..620862b 100644 --- a/spec/CookiePluginSpec.php +++ b/spec/CookiePluginSpec.php @@ -2,8 +2,8 @@ namespace spec\Http\Client\Plugin; -use Http\Client\Plugin\FulfilledPromise; use Http\Client\Promise; +use Http\Client\Utils\Promise\FulfilledPromise; use Http\Cookie\Cookie; use Http\Cookie\CookieJar; use PhpSpec\ObjectBehavior; @@ -153,7 +153,7 @@ function it_saves_cookie(CookieJar $cookieJar, RequestInterface $request, Respon $uri->getPath()->shouldBeCalled()->willReturn('/'); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Plugin\FulfilledPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise'); $response = $promise->getResponse(); $response->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); } diff --git a/spec/ErrorPluginSpec.php b/spec/ErrorPluginSpec.php index b7492be..da23d33 100644 --- a/spec/ErrorPluginSpec.php +++ b/spec/ErrorPluginSpec.php @@ -2,7 +2,7 @@ namespace spec\Http\Client\Plugin; -use Http\Client\Plugin\FulfilledPromise; +use Http\Client\Utils\Promise\FulfilledPromise; use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Psr\Http\Message\RequestInterface; @@ -26,7 +26,7 @@ function it_throw_request_exception_on_500_error(RequestInterface $request, Resp } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Plugin\RejectedPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); } function it_returns_response(RequestInterface $request, ResponseInterface $response) @@ -39,7 +39,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Plugin\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); } function it_throw_request_exception_on_custom_regex(RequestInterface $request, ResponseInterface $response) @@ -52,6 +52,6 @@ function it_throw_request_exception_on_custom_regex(RequestInterface $request, R } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Plugin\RejectedPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); } } diff --git a/spec/RedirectPluginSpec.php b/spec/RedirectPluginSpec.php index 02d29fb..29eade6 100644 --- a/spec/RedirectPluginSpec.php +++ b/spec/RedirectPluginSpec.php @@ -2,11 +2,7 @@ namespace spec\Http\Client\Plugin; -use Http\Client\HttpClient; -use Http\Client\Plugin\Exception\CircularRedirectionException; -use Http\Client\Plugin\Exception\RebootChainException; -use Http\Client\Plugin\FulfilledPromise; -use Http\Client\Plugin\PluginClient; +use Http\Client\Utils\Promise\FulfilledPromise; use Http\Client\Plugin\RedirectPlugin; use Http\Client\Promise; use PhpSpec\ObjectBehavior; @@ -66,7 +62,7 @@ function it_redirect_on_302( $promise->getResponse()->shouldBeCalled()->willReturn($finalResponse); $finalPromise = $this->handleRequest($request, $next, $first); - $finalPromise->shouldReturnAnInstanceOf('Http\Client\Plugin\FulfilledPromise'); + $finalPromise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); $finalPromise->getResponse()->shouldReturn($finalResponse); } @@ -200,7 +196,7 @@ function it_throws_http_exception_on_no_location(UriInterface $uri, RequestInter $responseRedirect->hasHeader('Location')->shouldBeCalled()->willReturn(false); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Plugin\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); $promise->getException()->shouldReturnAnInstanceOf('Http\Client\Exception\HttpException'); } @@ -220,7 +216,7 @@ function it_throws_http_exception_on_invalid_location(UriInterface $uri, Request $responseRedirect->hasHeader('Location')->shouldBeCalled()->willReturn(true); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Plugin\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); $promise->getException()->shouldReturnAnInstanceOf('Http\Client\Exception\HttpException'); } @@ -236,7 +232,7 @@ function it_throw_multi_redirect_exception_on_300(RequestInterface $request, Res $responseRedirect->getStatusCode()->shouldBeCalled()->willReturn('300'); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Plugin\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); $promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\MultipleRedirectionException'); } @@ -252,7 +248,7 @@ function it_throw_multi_redirect_exception_on_300_if_no_location(RequestInterfac $responseRedirect->hasHeader('Location')->shouldBeCalled()->willReturn(false); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Plugin\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); $promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\MultipleRedirectionException'); } @@ -383,7 +379,7 @@ function it_throws_circular_redirection_exception(UriInterface $uri, UriInterfac }; $promise = $this->handleRequest($request, $next, $first); - $promise->shouldReturnAnInstanceOf('Http\Client\Plugin\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); $promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\CircularRedirectionException'); } } diff --git a/spec/RetryPluginSpec.php b/spec/RetryPluginSpec.php index 43c1ee9..130cdcf 100644 --- a/spec/RetryPluginSpec.php +++ b/spec/RetryPluginSpec.php @@ -3,9 +3,8 @@ namespace spec\Http\Client\Plugin; use Http\Client\Exception; -use Http\Client\HttpClient; -use Http\Client\Plugin\FulfilledPromise; -use Http\Client\Plugin\RejectedPromise; +use Http\Client\Utils\Promise\FulfilledPromise; +use Http\Client\Utils\Promise\RejectedPromise; use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Psr\Http\Message\RequestInterface; @@ -27,7 +26,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Plugin\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); } function it_throws_exception_on_multiple_exceptions(RequestInterface $request) @@ -50,7 +49,7 @@ function it_throws_exception_on_multiple_exceptions(RequestInterface $request) }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Plugin\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); $promise->getException()->shouldReturn($exception2); } @@ -73,7 +72,7 @@ function it_returns_response_on_second_try(RequestInterface $request, ResponseIn }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Plugin\FulfilledPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); $promise->getResponse()->shouldReturn($response); } @@ -95,7 +94,7 @@ function it_does_not_keep_history_of_old_failure(RequestInterface $request, Resp } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Plugin\FulfilledPromise'); - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Plugin\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); } } diff --git a/spec/StopwatchPluginSpec.php b/spec/StopwatchPluginSpec.php index d70bce9..bd7112e 100644 --- a/spec/StopwatchPluginSpec.php +++ b/spec/StopwatchPluginSpec.php @@ -3,8 +3,8 @@ namespace spec\Http\Client\Plugin; use Http\Client\Exception\NetworkException; -use Http\Client\Plugin\FulfilledPromise; -use Http\Client\Plugin\RejectedPromise; +use Http\Client\Utils\Promise\FulfilledPromise; +use Http\Client\Utils\Promise\RejectedPromise; use PhpSpec\ObjectBehavior; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; diff --git a/src/FakeAsyncClient.php b/src/FakeAsyncClient.php deleted file mode 100644 index 644a52c..0000000 --- a/src/FakeAsyncClient.php +++ /dev/null @@ -1,37 +0,0 @@ - - */ -class FakeAsyncClient implements HttpAsyncClient -{ - /** @var \Http\Client\HttpClient */ - private $httpClient; - - public function __construct(HttpClient $httpClient) - { - $this->httpClient = $httpClient; - } - - /** - * {@inheritdoc} - */ - public function sendAsyncRequest(RequestInterface $request) - { - try { - return new FulfilledPromise($this->httpClient->sendRequest($request)); - } catch (Exception $e) { - return new RejectedPromise($e); - } - } -} - \ No newline at end of file diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php deleted file mode 100644 index 19f5b83..0000000 --- a/src/FulfilledPromise.php +++ /dev/null @@ -1,65 +0,0 @@ -response = $response; - } - - /** - * {@inheritdoc} - */ - public function then(callable $onFulfilled = null, callable $onRejected = null) - { - try { - return new FulfilledPromise($onFulfilled($this->response)); - } catch (Exception $e) { - return new RejectedPromise($e); - } - } - - /** - * {@inheritdoc} - */ - public function getState() - { - return Promise::FULFILLED; - } - - /** - * {@inheritdoc} - */ - public function getResponse() - { - return $this->response; - } - - /** - * {@inheritdoc} - */ - public function getException() - { - throw new \LogicException("Fulfilled promise, response not available"); - } - - /** - * {@inheritdoc} - */ - public function wait() - { - } -} - \ No newline at end of file diff --git a/src/PluginClient.php b/src/PluginClient.php index 30adf0c..8f94296 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -6,6 +6,7 @@ use Http\Client\HttpClient; use Http\Client\Plugin\Exception\RebootChainException; use Http\Client\Promise; +use Http\Client\Utils\EmulateAsyncClient; use Psr\Http\Message\RequestInterface; class PluginClient implements HttpClient, HttpAsyncClient @@ -31,7 +32,7 @@ public function __construct($client, array $plugins = array()) if ($client instanceof HttpAsyncClient) { $this->client = $client; } elseif ($client instanceof HttpClient) { - $this->client = new FakeAsyncClient($client); + $this->client = new EmulateAsyncClient($client); } else { throw new \RuntimeException("Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient"); } diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php deleted file mode 100644 index 148aedf..0000000 --- a/src/RejectedPromise.php +++ /dev/null @@ -1,60 +0,0 @@ -exception = $exception; - } - - /** - * {@inheritdoc} - */ - public function then(callable $onFulfilled = null, callable $onRejected = null) - { - try { - return new FulfilledPromise($onRejected($this->exception)); - } catch (Exception $e) { - return new RejectedPromise($e); - } - } - - /** - * {@inheritdoc} - */ - public function getState() - { - return Promise::REJECTED; - } - - /** - * {@inheritdoc} - */ - public function getResponse() - { - throw new \LogicException("Promise is rejected, no response available"); - } - - /** - * {@inheritdoc} - */ - public function getException() - { - return $this->exception; - } - - /** - * {@inheritdoc} - */ - public function wait() - { - } -}