diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c268e8..c6a01d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 2.0 (unreleased) +### Changed +- Abstract method `HttpClientPool::chooseHttpClient()` has now an explicit return type (`Http\Client\Common\HttpClientPoolItem`) +- Interface method `Plugin::handleRequest(...)` has now an explicit return type (`Http\Promise\Promise`) + ### Removed - Deprecated option `debug_plugins` has been removed from `PluginClient` diff --git a/composer.json b/composer.json index 882758b..0b32bc2 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,11 @@ "Http\\Client\\Common\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "spec\\Http\\Client\\Common\\": "spec/" + } + }, "scripts": { "cs-check": "vendor/bin/php-cs-fixer fix --dry-run", "cs-fix": "vendor/bin/php-cs-fixer fix", diff --git a/spec/BatchClientSpec.php b/spec/BatchClientSpec.php index 8b7e96d..86c6c8b 100644 --- a/spec/BatchClientSpec.php +++ b/spec/BatchClientSpec.php @@ -6,12 +6,16 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; +use Http\Client\Common\BatchClient; +use Http\Client\Common\BatchResult; +use Http\Client\Exception\HttpException; +use Http\Client\Common\Exception\BatchException; class BatchClientSpec extends ObjectBehavior { public function let(HttpClient $client) { - $this->beAnInstanceOf('Http\Client\Common\BatchClient', [$client]); + $this->beAnInstanceOf(BatchClient::class, [$client]); } public function it_send_multiple_request_using_send_request(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response1, ResponseInterface $response2) @@ -19,14 +23,14 @@ public function it_send_multiple_request_using_send_request(HttpClient $client, $client->sendRequest($request1)->willReturn($response1); $client->sendRequest($request2)->willReturn($response2); - $this->sendRequests([$request1, $request2])->shouldReturnAnInstanceOf('Http\Client\Common\BatchResult'); + $this->sendRequests([$request1, $request2])->shouldReturnAnInstanceOf(BatchResult::class); } public function it_throw_batch_exception_if_one_or_more_request_failed(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response) { $client->sendRequest($request1)->willReturn($response); - $client->sendRequest($request2)->willThrow('Http\Client\Exception\HttpException'); + $client->sendRequest($request2)->willThrow(HttpException::class); - $this->shouldThrow('Http\Client\Common\Exception\BatchException')->duringSendRequests([$request1, $request2]); + $this->shouldThrow(BatchException::class)->duringSendRequests([$request1, $request2]); } } diff --git a/spec/BatchResultSpec.php b/spec/BatchResultSpec.php index aa3bb14..90a80a5 100644 --- a/spec/BatchResultSpec.php +++ b/spec/BatchResultSpec.php @@ -6,12 +6,13 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; +use Http\Client\Common\BatchResult; class BatchResultSpec extends ObjectBehavior { public function it_is_initializable() { - $this->beAnInstanceOf('Http\Client\Common\BatchResult'); + $this->beAnInstanceOf(BatchResult::class); } public function it_is_immutable(RequestInterface $request, ResponseInterface $response) @@ -19,7 +20,7 @@ public function it_is_immutable(RequestInterface $request, ResponseInterface $re $new = $this->addResponse($request, $response); $this->getResponses()->shouldReturn([]); - $new->shouldHaveType('Http\Client\Common\BatchResult'); + $new->shouldHaveType(BatchResult::class); $new->getResponses()->shouldReturn([$response]); } @@ -37,7 +38,7 @@ public function it_has_a_response_for_a_request(RequestInterface $request, Respo { $new = $this->addResponse($request, $response); - $this->shouldThrow('UnexpectedValueException')->duringGetResponseFor($request); + $this->shouldThrow(\UnexpectedValueException::class)->duringGetResponseFor($request); $this->isSuccessful($request)->shouldReturn(false); $new->getResponseFor($request)->shouldReturn($response); $new->isSuccessful($request)->shouldReturn(true); diff --git a/spec/EmulatedHttpAsyncClientSpec.php b/spec/EmulatedHttpAsyncClientSpec.php index ca8a4d1..226008b 100644 --- a/spec/EmulatedHttpAsyncClientSpec.php +++ b/spec/EmulatedHttpAsyncClientSpec.php @@ -6,6 +6,11 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; +use Http\Client\Common\EmulatedHttpAsyncClient; +use Http\Client\HttpAsyncClient; +use Http\Client\Promise\HttpFulfilledPromise; +use Http\Client\Exception\TransferException; +use Http\Client\Promise\HttpRejectedPromise; class EmulatedHttpAsyncClientSpec extends ObjectBehavior { @@ -16,17 +21,17 @@ public function let(HttpClient $httpClient) public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\EmulatedHttpAsyncClient'); + $this->shouldHaveType(EmulatedHttpAsyncClient::class); } public function it_is_an_http_client() { - $this->shouldImplement('Http\Client\HttpClient'); + $this->shouldImplement(HttpClient::class); } public function it_is_an_async_http_client() { - $this->shouldImplement('Http\Client\HttpAsyncClient'); + $this->shouldImplement(HttpAsyncClient::class); } public function it_emulates_a_successful_request( @@ -36,14 +41,14 @@ public function it_emulates_a_successful_request( ) { $httpClient->sendRequest($request)->willReturn($response); - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf(HttpFulfilledPromise::class); } public function it_emulates_a_failed_request(HttpClient $httpClient, RequestInterface $request) { - $httpClient->sendRequest($request)->willThrow('Http\Client\Exception\TransferException'); + $httpClient->sendRequest($request)->willThrow(TransferException::class); - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf(HttpRejectedPromise::class); } public function it_decorates_the_underlying_client( diff --git a/spec/EmulatedHttpClientSpec.php b/spec/EmulatedHttpClientSpec.php index cf6e076..adf206f 100644 --- a/spec/EmulatedHttpClientSpec.php +++ b/spec/EmulatedHttpClientSpec.php @@ -4,10 +4,13 @@ use Http\Client\Exception\TransferException; use Http\Client\HttpAsyncClient; +use Http\Client\HttpClient; use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; +use Http\Client\Common\EmulatedHttpClient; +use Http\Client\Exception; class EmulatedHttpClientSpec extends ObjectBehavior { @@ -18,17 +21,17 @@ public function let(HttpAsyncClient $httpAsyncClient) public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\EmulatedHttpClient'); + $this->shouldHaveType(EmulatedHttpClient::class); } public function it_is_an_http_client() { - $this->shouldImplement('Http\Client\HttpClient'); + $this->shouldImplement(HttpClient::class); } public function it_is_an_async_http_client() { - $this->shouldImplement('Http\Client\HttpAsyncClient'); + $this->shouldImplement(HttpAsyncClient::class); } public function it_emulates_a_successful_request( @@ -54,7 +57,7 @@ public function it_emulates_a_failed_request(HttpAsyncClient $httpAsyncClient, R $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); - $this->shouldThrow('Http\Client\Exception')->duringSendRequest($request); + $this->shouldThrow(Exception::class)->duringSendRequest($request); } public function it_decorates_the_underlying_client( diff --git a/spec/Exception/BatchExceptionSpec.php b/spec/Exception/BatchExceptionSpec.php index 0a32607..0205843 100644 --- a/spec/Exception/BatchExceptionSpec.php +++ b/spec/Exception/BatchExceptionSpec.php @@ -3,7 +3,9 @@ namespace spec\Http\Client\Common\Exception; use Http\Client\Common\BatchResult; +use Http\Client\Exception; use PhpSpec\ObjectBehavior; +use Http\Client\Common\Exception\BatchException; class BatchExceptionSpec extends ObjectBehavior { @@ -15,21 +17,21 @@ public function let() public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\Exception\BatchException'); + $this->shouldHaveType(BatchException::class); } public function it_is_a_runtime_exception() { - $this->shouldHaveType('RuntimeException'); + $this->shouldHaveType(\RuntimeException::class); } public function it_is_an_exception() { - $this->shouldImplement('Http\Client\Exception'); + $this->shouldImplement(Exception::class); } public function it_has_a_batch_result() { - $this->getResult()->shouldHaveType('Http\Client\Common\BatchResult'); + $this->getResult()->shouldHaveType(BatchResult::class); } } diff --git a/spec/FlexibleHttpClientSpec.php b/spec/FlexibleHttpClientSpec.php index 769b4c3..b63aa3d 100644 --- a/spec/FlexibleHttpClientSpec.php +++ b/spec/FlexibleHttpClientSpec.php @@ -8,6 +8,7 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; +use Http\Client\Common\FlexibleHttpClient; class FlexibleHttpClientSpec extends ObjectBehavior { @@ -18,24 +19,24 @@ public function let(HttpClient $httpClient) public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\FlexibleHttpClient'); + $this->shouldHaveType(FlexibleHttpClient::class); } public function it_is_an_http_client() { - $this->shouldImplement('Http\Client\HttpClient'); + $this->shouldImplement(HttpClient::class); } public function it_is_an_async_http_client() { - $this->shouldImplement('Http\Client\HttpAsyncClient'); + $this->shouldImplement(HttpAsyncClient::class); } public function it_throw_exception_if_invalid_client() { $this->beConstructedWith(null); - $this->shouldThrow('\LogicException')->duringInstantiation(); + $this->shouldThrow(\LogicException::class)->duringInstantiation(); } public function it_emulates_an_async_client( @@ -53,7 +54,7 @@ public function it_emulates_an_async_client( $this->sendRequest($syncRequest)->shouldReturn($syncResponse); $promise = $this->sendAsyncRequest($asyncRequest); - $promise->shouldHaveType('Http\Promise\Promise'); + $promise->shouldHaveType(Promise::class); $promise->wait()->shouldReturn($asyncResponse); } @@ -77,8 +78,8 @@ public function it_emulates_a_client( public function it_does_not_emulate_a_client($client, RequestInterface $syncRequest, RequestInterface $asyncRequest) { - $client->implement('Http\Client\HttpClient'); - $client->implement('Http\Client\HttpAsyncClient'); + $client->implement(HttpClient::class); + $client->implement(HttpAsyncClient::class); $client->sendRequest($syncRequest)->shouldBeCalled(); $client->sendRequest($asyncRequest)->shouldNotBeCalled(); diff --git a/spec/HttpClientPool/LeastUsedClientPoolSpec.php b/spec/HttpClientPool/LeastUsedClientPoolSpec.php index 8642c31..9c02e47 100644 --- a/spec/HttpClientPool/LeastUsedClientPoolSpec.php +++ b/spec/HttpClientPool/LeastUsedClientPoolSpec.php @@ -10,28 +10,31 @@ use Prophecy\Argument; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Http\Client\Common\HttpClientPool\LeastUsedClientPool; +use Http\Client\Common\Exception\HttpClientNotFoundException; +use Http\Client\Exception\HttpException; class LeastUsedClientPoolSpec extends ObjectBehavior { public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\HttpClientPool\LeastUsedClientPool'); + $this->shouldHaveType(LeastUsedClientPool::class); } public function it_is_an_http_client() { - $this->shouldImplement('Http\Client\HttpClient'); + $this->shouldImplement(HttpClient::class); } public function it_is_an_async_http_client() { - $this->shouldImplement('Http\Client\HttpAsyncClient'); + $this->shouldImplement(HttpAsyncClient::class); } public function it_throw_exception_with_no_client(RequestInterface $request) { - $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request); - $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendAsyncRequest($request); + $this->shouldThrow(HttpClientNotFoundException::class)->duringSendRequest($request); + $this->shouldThrow(HttpClientNotFoundException::class)->duringSendAsyncRequest($request); } public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response) @@ -54,19 +57,19 @@ public function it_sends_async_request(HttpAsyncClient $httpAsyncClient, Request public function it_throw_exception_if_no_more_enable_client(HttpClient $client, RequestInterface $request) { $this->addHttpClient($client); - $client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException'); + $client->sendRequest($request)->willThrow(HttpException::class); - $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); - $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request); + $this->shouldThrow(HttpException::class)->duringSendRequest($request); + $this->shouldThrow(HttpClientNotFoundException::class)->duringSendRequest($request); } public function it_reenable_client(HttpClient $client, RequestInterface $request) { $this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0)); - $client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException'); + $client->sendRequest($request)->willThrow(HttpException::class); - $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); - $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); + $this->shouldThrow(HttpException::class)->duringSendRequest($request); + $this->shouldThrow(HttpException::class)->duringSendRequest($request); } public function it_uses_the_lowest_request_client(HttpClientPoolItem $client1, HttpClientPoolItem $client2, RequestInterface $request, ResponseInterface $response) diff --git a/spec/HttpClientPool/RandomClientPoolSpec.php b/spec/HttpClientPool/RandomClientPoolSpec.php index 4054d82..fb0e5ad 100644 --- a/spec/HttpClientPool/RandomClientPoolSpec.php +++ b/spec/HttpClientPool/RandomClientPoolSpec.php @@ -10,28 +10,31 @@ use Prophecy\Argument; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Http\Client\Common\HttpClientPool\RandomClientPool; +use Http\Client\Common\Exception\HttpClientNotFoundException; +use Http\Client\Exception\HttpException; class RandomClientPoolSpec extends ObjectBehavior { public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\HttpClientPool\RandomClientPool'); + $this->shouldHaveType(RandomClientPool::class); } public function it_is_an_http_client() { - $this->shouldImplement('Http\Client\HttpClient'); + $this->shouldImplement(HttpClient::class); } public function it_is_an_async_http_client() { - $this->shouldImplement('Http\Client\HttpAsyncClient'); + $this->shouldImplement(HttpAsyncClient::class); } public function it_throw_exception_with_no_client(RequestInterface $request) { - $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request); - $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendAsyncRequest($request); + $this->shouldThrow(HttpClientNotFoundException::class)->duringSendRequest($request); + $this->shouldThrow(HttpClientNotFoundException::class)->duringSendAsyncRequest($request); } public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response) @@ -54,18 +57,18 @@ public function it_sends_async_request(HttpAsyncClient $httpAsyncClient, Request public function it_throw_exception_if_no_more_enable_client(HttpClient $client, RequestInterface $request) { $this->addHttpClient($client); - $client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException'); + $client->sendRequest($request)->willThrow(HttpException::class); - $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); - $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request); + $this->shouldThrow(HttpException::class)->duringSendRequest($request); + $this->shouldThrow(HttpClientNotFoundException::class)->duringSendRequest($request); } public function it_reenable_client(HttpClient $client, RequestInterface $request) { $this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0)); - $client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException'); + $client->sendRequest($request)->willThrow(HttpException::class); - $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); - $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); + $this->shouldThrow(HttpException::class)->duringSendRequest($request); + $this->shouldThrow(HttpException::class)->duringSendRequest($request); } } diff --git a/spec/HttpClientPool/RoundRobinClientPoolSpec.php b/spec/HttpClientPool/RoundRobinClientPoolSpec.php index 48c2d01..925f548 100644 --- a/spec/HttpClientPool/RoundRobinClientPoolSpec.php +++ b/spec/HttpClientPool/RoundRobinClientPoolSpec.php @@ -10,28 +10,31 @@ use Prophecy\Argument; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Http\Client\Common\HttpClientPool\RoundRobinClientPool; +use Http\Client\Common\Exception\HttpClientNotFoundException; +use Http\Client\Exception\HttpException; class RoundRobinClientPoolSpec extends ObjectBehavior { public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\HttpClientPool\RoundRobinClientPool'); + $this->shouldHaveType(RoundRobinClientPool::class); } public function it_is_an_http_client() { - $this->shouldImplement('Http\Client\HttpClient'); + $this->shouldImplement(HttpClient::class); } public function it_is_an_async_http_client() { - $this->shouldImplement('Http\Client\HttpAsyncClient'); + $this->shouldImplement(HttpAsyncClient::class); } public function it_throw_exception_with_no_client(RequestInterface $request) { - $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request); - $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendAsyncRequest($request); + $this->shouldThrow(HttpClientNotFoundException::class)->duringSendRequest($request); + $this->shouldThrow(HttpClientNotFoundException::class)->duringSendAsyncRequest($request); } public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response) @@ -54,19 +57,19 @@ public function it_sends_async_request(HttpAsyncClient $httpAsyncClient, Request public function it_throw_exception_if_no_more_enable_client(HttpClient $client, RequestInterface $request) { $this->addHttpClient($client); - $client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException'); + $client->sendRequest($request)->willThrow(HttpException::class); - $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); - $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request); + $this->shouldThrow(HttpException::class)->duringSendRequest($request); + $this->shouldThrow(HttpClientNotFoundException::class)->duringSendRequest($request); } public function it_reenable_client(HttpClient $client, RequestInterface $request) { $this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0)); - $client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException'); + $client->sendRequest($request)->willThrow(HttpException::class); - $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); - $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); + $this->shouldThrow(HttpException::class)->duringSendRequest($request); + $this->shouldThrow(HttpException::class)->duringSendRequest($request); } public function it_round_between_clients(HttpClient $client1, HttpClient $client2, RequestInterface $request, ResponseInterface $response) diff --git a/spec/HttpClientPoolItemSpec.php b/spec/HttpClientPoolItemSpec.php index cba68d8..f44351b 100644 --- a/spec/HttpClientPoolItemSpec.php +++ b/spec/HttpClientPoolItemSpec.php @@ -12,6 +12,7 @@ use Prophecy\Argument; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Http\Client\Exception\RequestException; class HttpClientPoolItemSpec extends ObjectBehavior { @@ -22,12 +23,12 @@ public function let(HttpClient $httpClient) public function it_is_an_http_client() { - $this->shouldImplement('Http\Client\HttpClient'); + $this->shouldImplement(HttpClient::class); } public function it_is_an_async_http_client() { - $this->shouldImplement('Http\Client\HttpAsyncClient'); + $this->shouldImplement(HttpAsyncClient::class); } public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response) @@ -53,7 +54,7 @@ public function it_disable_himself_on_send_request(HttpClient $httpClient, Reque $httpClient->sendRequest($request)->willThrow($exception); $this->shouldThrow($exception)->duringSendRequest($request); $this->isDisabled()->shouldReturn(true); - $this->shouldThrow('Http\Client\Exception\RequestException')->duringSendRequest($request); + $this->shouldThrow(RequestException::class)->duringSendRequest($request); } public function it_disable_himself_on_send_async_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request) @@ -63,9 +64,9 @@ public function it_disable_himself_on_send_async_request(HttpAsyncClient $httpAs $promise = new HttpRejectedPromise(new TransferException()); $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf(HttpRejectedPromise::class); $this->isDisabled()->shouldReturn(true); - $this->shouldThrow('Http\Client\Exception\RequestException')->duringSendAsyncRequest($request); + $this->shouldThrow(RequestException::class)->duringSendAsyncRequest($request); } public function it_reactivate_himself_on_send_request(HttpClient $httpClient, RequestInterface $request) @@ -87,9 +88,9 @@ public function it_reactivate_himself_on_send_async_request(HttpAsyncClient $htt $promise = new HttpRejectedPromise(new TransferException()); $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf(HttpRejectedPromise::class); $this->isDisabled()->shouldReturn(false); - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf(HttpRejectedPromise::class); } public function it_increments_request_count(HttpAsyncClient $httpAsyncClient, RequestInterface $request, ResponseInterface $response) diff --git a/spec/HttpClientRouterSpec.php b/spec/HttpClientRouterSpec.php index e43acea..68446a4 100644 --- a/spec/HttpClientRouterSpec.php +++ b/spec/HttpClientRouterSpec.php @@ -9,22 +9,24 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; +use Http\Client\Common\HttpClientRouter; +use Http\Client\Exception\RequestException; class HttpClientRouterSpec extends ObjectBehavior { public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\HttpClientRouter'); + $this->shouldHaveType(HttpClientRouter::class); } public function it_is_an_http_client() { - $this->shouldImplement('Http\Client\HttpClient'); + $this->shouldImplement(HttpClient::class); } public function it_is_an_async_http_client() { - $this->shouldImplement('Http\Client\HttpAsyncClient'); + $this->shouldImplement(HttpAsyncClient::class); } public function it_send_request(RequestMatcher $matcher, HttpClient $client, RequestInterface $request, ResponseInterface $response) @@ -50,7 +52,7 @@ public function it_throw_exception_on_send_request(RequestMatcher $matcher, Http $this->addClient($client, $matcher); $matcher->matches($request)->willReturn(false); - $this->shouldThrow('Http\Client\Exception\RequestException')->duringSendRequest($request); + $this->shouldThrow(RequestException::class)->duringSendRequest($request); } public function it_throw_exception_on_send_async_request(RequestMatcher $matcher, HttpAsyncClient $client, RequestInterface $request) @@ -58,6 +60,6 @@ public function it_throw_exception_on_send_async_request(RequestMatcher $matcher $this->addClient($client, $matcher); $matcher->matches($request)->willReturn(false); - $this->shouldThrow('Http\Client\Exception\RequestException')->duringSendAsyncRequest($request); + $this->shouldThrow(RequestException::class)->duringSendAsyncRequest($request); } } diff --git a/spec/HttpMethodsClientSpec.php b/spec/HttpMethodsClientSpec.php index b68c8c4..74e0810 100644 --- a/spec/HttpMethodsClientSpec.php +++ b/spec/HttpMethodsClientSpec.php @@ -2,19 +2,20 @@ namespace spec\Http\Client\Common; -use Http\Client\HttpClient; +use GuzzleHttp\Psr7\Response; use Http\Client\Common\HttpMethodsClient; +use Http\Client\HttpClient; use Http\Message\MessageFactory; +use PhpSpec\ObjectBehavior; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use PhpSpec\ObjectBehavior; class HttpMethodsClientSpec extends ObjectBehavior { public function let(HttpClient $client, MessageFactory $messageFactory) { $this->beAnInstanceOf( - 'spec\Http\Client\Common\HttpMethodsClientStub', [ + HttpMethodsClientStub::class, [ $client, $messageFactory, ] @@ -25,56 +26,56 @@ public function it_sends_a_get_request() { $data = HttpMethodsClientStub::$requestData; - $this->get($data['uri'], $data['headers'])->shouldReturn(true); + $this->get($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class); } public function it_sends_a_head_request() { $data = HttpMethodsClientStub::$requestData; - $this->head($data['uri'], $data['headers'])->shouldReturn(true); + $this->head($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class); } public function it_sends_a_trace_request() { $data = HttpMethodsClientStub::$requestData; - $this->trace($data['uri'], $data['headers'])->shouldReturn(true); + $this->trace($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class); } public function it_sends_a_post_request() { $data = HttpMethodsClientStub::$requestData; - $this->post($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); + $this->post($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); } public function it_sends_a_put_request() { $data = HttpMethodsClientStub::$requestData; - $this->put($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); + $this->put($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); } public function it_sends_a_patch_request() { $data = HttpMethodsClientStub::$requestData; - $this->patch($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); + $this->patch($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); } public function it_sends_a_delete_request() { $data = HttpMethodsClientStub::$requestData; - $this->delete($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); + $this->delete($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); } public function it_sends_a_options_request() { $data = HttpMethodsClientStub::$requestData; - $this->options($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); + $this->options($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); } public function it_sends_request_with_underlying_client(HttpClient $client, MessageFactory $messageFactory, RequestInterface $request, ResponseInterface $response) @@ -99,17 +100,37 @@ class HttpMethodsClientStub extends HttpMethodsClient /** * {@inheritdoc} */ - public function send($method, $uri, array $headers = [], $body = null) + public function send($method, $uri, array $headers = [], $body = null): ResponseInterface { - if (in_array($method, ['GET', 'HEAD', 'TRACE'])) { - return $uri === self::$requestData['uri'] && - $headers === self::$requestData['headers'] && - is_null($body); + if ($uri !== self::$requestData['uri']) { + throw new \InvalidArgumentException('Invalid URI: '.$uri); } - return in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']) && - $uri === self::$requestData['uri'] && - $headers === self::$requestData['headers'] && - $body === self::$requestData['body']; + if ($headers !== self::$requestData['headers']) { + throw new \InvalidArgumentException('Invalid headers: '.print_r($headers, true)); + } + + switch ($method) { + case 'GET': + case 'HEAD': + case 'TRACE': + if (null !== $body) { + throw new \InvalidArgumentException('Non-empty body'); + } + + return new Response(); + case 'POST': + case 'PUT': + case 'PATCH': + case 'DELETE': + case 'OPTIONS': + if ($body !== self::$requestData['body']) { + throw new \InvalidArgumentException('Invalid body: '.print_r($body, true)); + } + + return new Response(); + default: + throw new \InvalidArgumentException('Invalid method: '.$method); + } } } diff --git a/spec/Plugin/AddHostPluginSpec.php b/spec/Plugin/AddHostPluginSpec.php index a6630bd..2691f10 100644 --- a/spec/Plugin/AddHostPluginSpec.php +++ b/spec/Plugin/AddHostPluginSpec.php @@ -5,6 +5,8 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\UriInterface; use PhpSpec\ObjectBehavior; +use Http\Client\Common\Plugin\AddHostPlugin; +use Http\Client\Common\Plugin; class AddHostPluginSpec extends ObjectBehavior { @@ -17,14 +19,14 @@ public function it_is_initializable(UriInterface $uri) { $uri->getHost()->shouldBeCalled()->willReturn('example.com'); - $this->shouldHaveType('Http\Client\Common\Plugin\AddHostPlugin'); + $this->shouldHaveType(AddHostPlugin::class); } public function it_is_a_plugin(UriInterface $uri) { $uri->getHost()->shouldBeCalled()->willReturn('example.com'); - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_adds_domain( @@ -45,7 +47,7 @@ public function it_adds_domain( $uri->getHost()->shouldBeCalled()->willReturn(''); $this->beConstructedWith($host); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_replaces_domain( @@ -65,7 +67,7 @@ public function it_replaces_domain( $uri->withPort(8000)->shouldBeCalled()->willReturn($uri); $this->beConstructedWith($host, ['replace' => true]); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_nothing_when_domain_exists( @@ -77,6 +79,6 @@ public function it_does_nothing_when_domain_exists( $uri->getHost()->shouldBeCalled()->willReturn('default.com'); $this->beConstructedWith($host); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } } diff --git a/spec/Plugin/AddPathPluginSpec.php b/spec/Plugin/AddPathPluginSpec.php index c95301c..a76eb16 100644 --- a/spec/Plugin/AddPathPluginSpec.php +++ b/spec/Plugin/AddPathPluginSpec.php @@ -5,6 +5,8 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\UriInterface; use PhpSpec\ObjectBehavior; +use Http\Client\Common\Plugin\AddPathPlugin; +use Http\Client\Common\Plugin; class AddPathPluginSpec extends ObjectBehavior { @@ -17,14 +19,14 @@ public function it_is_initializable(UriInterface $uri) { $uri->getPath()->shouldBeCalled()->willReturn('/api'); - $this->shouldHaveType('Http\Client\Common\Plugin\AddPathPlugin'); + $this->shouldHaveType(AddPathPlugin::class); } public function it_is_a_plugin(UriInterface $uri) { $uri->getPath()->shouldBeCalled()->willReturn('/api'); - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_adds_path( @@ -41,7 +43,7 @@ public function it_adds_path( $uri->getPath()->shouldBeCalled()->willReturn('/users'); $this->beConstructedWith($host); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_throws_exception_on_trailing_slash(UriInterface $host) @@ -49,7 +51,7 @@ public function it_throws_exception_on_trailing_slash(UriInterface $host) $host->getPath()->shouldBeCalled()->willReturn('/api/'); $this->beConstructedWith($host); - $this->shouldThrow('\LogicException')->duringInstantiation(); + $this->shouldThrow(\LogicException::class)->duringInstantiation(); } public function it_throws_exception_on_empty_path(UriInterface $host) @@ -57,6 +59,6 @@ public function it_throws_exception_on_empty_path(UriInterface $host) $host->getPath()->shouldBeCalled()->willReturn(''); $this->beConstructedWith($host); - $this->shouldThrow('\LogicException')->duringInstantiation(); + $this->shouldThrow(\LogicException::class)->duringInstantiation(); } } diff --git a/spec/Plugin/AuthenticationPluginSpec.php b/spec/Plugin/AuthenticationPluginSpec.php index 549b507..c86cb3a 100644 --- a/spec/Plugin/AuthenticationPluginSpec.php +++ b/spec/Plugin/AuthenticationPluginSpec.php @@ -7,6 +7,8 @@ use Psr\Http\Message\RequestInterface; use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use Http\Client\Common\Plugin\AuthenticationPlugin; +use Http\Client\Common\Plugin; class AuthenticationPluginSpec extends ObjectBehavior { @@ -17,12 +19,12 @@ public function let(Authentication $authentication) public function it_is_initializable(Authentication $authentication) { - $this->shouldHaveType('Http\Client\Common\Plugin\AuthenticationPlugin'); + $this->shouldHaveType(AuthenticationPlugin::class); } public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_sends_an_authenticated_request(Authentication $authentication, RequestInterface $notAuthedRequest, RequestInterface $authedRequest, Promise $promise) diff --git a/spec/Plugin/BaseUriPluginSpec.php b/spec/Plugin/BaseUriPluginSpec.php index e2f8c46..92f5074 100644 --- a/spec/Plugin/BaseUriPluginSpec.php +++ b/spec/Plugin/BaseUriPluginSpec.php @@ -5,6 +5,8 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\UriInterface; use PhpSpec\ObjectBehavior; +use Http\Client\Common\Plugin\BaseUriPlugin; +use Http\Client\Common\Plugin; class BaseUriPluginSpec extends ObjectBehavior { @@ -18,7 +20,7 @@ public function it_is_initializable(UriInterface $uri) $uri->getHost()->shouldBeCalled()->willReturn('example.com'); $uri->getPath()->shouldBeCalled()->willReturn('/api'); - $this->shouldHaveType('Http\Client\Common\Plugin\BaseUriPlugin'); + $this->shouldHaveType(BaseUriPlugin::class); } public function it_is_a_plugin(UriInterface $uri) @@ -26,7 +28,7 @@ public function it_is_a_plugin(UriInterface $uri) $uri->getHost()->shouldBeCalled()->willReturn('example.com'); $uri->getPath()->shouldBeCalled()->willReturn('/api'); - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_adds_domain_and_path( @@ -50,7 +52,7 @@ public function it_adds_domain_and_path( $uri->getPath()->shouldBeCalled()->willReturn('/users'); $this->beConstructedWith($host); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_adds_domain( @@ -72,7 +74,7 @@ public function it_adds_domain( $uri->getHost()->shouldBeCalled()->willReturn(''); $this->beConstructedWith($host); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_replaces_domain_and_adds_path( @@ -95,6 +97,6 @@ public function it_replaces_domain_and_adds_path( $uri->getPath()->shouldBeCalled()->willReturn('/users'); $this->beConstructedWith($host, ['replace' => true]); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } } diff --git a/spec/Plugin/ContentLengthPluginSpec.php b/spec/Plugin/ContentLengthPluginSpec.php index 6624f6c..a945924 100644 --- a/spec/Plugin/ContentLengthPluginSpec.php +++ b/spec/Plugin/ContentLengthPluginSpec.php @@ -7,17 +7,19 @@ use Psr\Http\Message\StreamInterface; use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use Http\Client\Common\Plugin\ContentLengthPlugin; +use Http\Client\Common\Plugin; class ContentLengthPluginSpec extends ObjectBehavior { public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\Plugin\ContentLengthPlugin'); + $this->shouldHaveType(ContentLengthPlugin::class); } public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_adds_content_length_header(RequestInterface $request, StreamInterface $stream) @@ -27,7 +29,7 @@ public function it_adds_content_length_header(RequestInterface $request, StreamI $stream->getSize()->shouldBeCalled()->willReturn(100); $request->withHeader('Content-Length', '100')->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_streams_chunked_if_no_size(RequestInterface $request, StreamInterface $stream) @@ -43,6 +45,6 @@ public function it_streams_chunked_if_no_size(RequestInterface $request, StreamI $request->withBody(Argument::type('Http\Message\Encoding\ChunkStream'))->shouldBeCalled()->willReturn($request); $request->withAddedHeader('Transfer-Encoding', 'chunked')->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } } diff --git a/spec/Plugin/ContentTypePluginSpec.php b/spec/Plugin/ContentTypePluginSpec.php index d2a6ffa..a27d32a 100644 --- a/spec/Plugin/ContentTypePluginSpec.php +++ b/spec/Plugin/ContentTypePluginSpec.php @@ -2,19 +2,21 @@ namespace spec\Http\Client\Common\Plugin; -use Psr\Http\Message\RequestInterface; +use Http\Client\Common\Plugin; +use Http\Client\Common\Plugin\ContentTypePlugin; use PhpSpec\ObjectBehavior; +use Psr\Http\Message\RequestInterface; class ContentTypePluginSpec extends ObjectBehavior { public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\Plugin\ContentTypePlugin'); + $this->shouldHaveType(ContentTypePlugin::class); } public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_adds_json_content_type_header(RequestInterface $request) @@ -23,7 +25,7 @@ public function it_adds_json_content_type_header(RequestInterface $request) $request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for(json_encode(['foo' => 'bar']))); $request->withHeader('Content-Type', 'application/json')->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_adds_xml_content_type_header(RequestInterface $request) @@ -32,7 +34,7 @@ public function it_adds_xml_content_type_header(RequestInterface $request) $request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('bar')); $request->withHeader('Content-Type', 'application/xml')->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_set_content_type_header(RequestInterface $request) @@ -41,7 +43,7 @@ public function it_does_not_set_content_type_header(RequestInterface $request) $request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('foo')); $request->withHeader('Content-Type', null)->shouldNotBeCalled(); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_set_content_type_header_if_already_one(RequestInterface $request) @@ -50,7 +52,7 @@ public function it_does_not_set_content_type_header_if_already_one(RequestInterf $request->getBody()->shouldNotBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('foo')); $request->withHeader('Content-Type', null)->shouldNotBeCalled(); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_set_content_type_header_if_size_0_or_unknown(RequestInterface $request) @@ -59,7 +61,7 @@ public function it_does_not_set_content_type_header_if_size_0_or_unknown(Request $request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for()); $request->withHeader('Content-Type', null)->shouldNotBeCalled(); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_adds_xml_content_type_header_if_size_limit_is_not_reached_using_default_value(RequestInterface $request) @@ -72,7 +74,7 @@ public function it_adds_xml_content_type_header_if_size_limit_is_not_reached_usi $request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('bar')); $request->withHeader('Content-Type', 'application/xml')->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_adds_xml_content_type_header_if_size_limit_is_not_reached(RequestInterface $request) @@ -86,7 +88,7 @@ public function it_adds_xml_content_type_header_if_size_limit_is_not_reached(Req $request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('bar')); $request->withHeader('Content-Type', 'application/xml')->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_set_content_type_header_if_size_limit_is_reached(RequestInterface $request) @@ -100,6 +102,6 @@ public function it_does_not_set_content_type_header_if_size_limit_is_reached(Req $request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('bar')); $request->withHeader('Content-Type', null)->shouldNotBeCalled(); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } } diff --git a/spec/Plugin/CookiePluginSpec.php b/spec/Plugin/CookiePluginSpec.php index 1f021b1..7bb188c 100644 --- a/spec/Plugin/CookiePluginSpec.php +++ b/spec/Plugin/CookiePluginSpec.php @@ -10,7 +10,10 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; use PhpSpec\ObjectBehavior; -use Prophecy\Argument; +use Http\Client\Common\Plugin\CookiePlugin; +use Http\Client\Common\Plugin; +use Http\Client\Promise\HttpRejectedPromise; +use Http\Client\Exception\TransferException; class CookiePluginSpec extends ObjectBehavior { @@ -25,12 +28,12 @@ public function let() public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\Plugin\CookiePlugin'); + $this->shouldHaveType(CookiePlugin::class); } public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_loads_cookie(RequestInterface $request, UriInterface $uri, Promise $promise) @@ -44,11 +47,7 @@ public function it_loads_cookie(RequestInterface $request, UriInterface $uri, Pr $request->withAddedHeader('Cookie', 'name=value')->willReturn($request); - $this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) { - if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) { - return $promise->getWrappedObject(); - } - }, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_combines_multiple_cookies_into_one_header(RequestInterface $request, UriInterface $uri, Promise $promise) @@ -65,11 +64,7 @@ public function it_combines_multiple_cookies_into_one_header(RequestInterface $r $request->withAddedHeader('Cookie', 'name=value; name2=value2')->willReturn($request); - $this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) { - if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) { - return $promise->getWrappedObject(); - } - }, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_load_cookie_if_expired(RequestInterface $request, UriInterface $uri, Promise $promise) @@ -79,11 +74,7 @@ public function it_does_not_load_cookie_if_expired(RequestInterface $request, Ur $request->withAddedHeader('Cookie', 'name=value')->shouldNotBeCalled(); - $this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) { - if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) { - return $promise->getWrappedObject(); - } - }, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_load_cookie_if_domain_does_not_match(RequestInterface $request, UriInterface $uri, Promise $promise) @@ -96,11 +87,7 @@ public function it_does_not_load_cookie_if_domain_does_not_match(RequestInterfac $request->withAddedHeader('Cookie', 'name=value')->shouldNotBeCalled(); - $this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) { - if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) { - return $promise->getWrappedObject(); - } - }, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_load_cookie_on_hackish_domains(RequestInterface $request, UriInterface $uri, Promise $promise) @@ -118,11 +105,7 @@ public function it_does_not_load_cookie_on_hackish_domains(RequestInterface $req $request->withAddedHeader('Cookie', 'name=value')->shouldNotBeCalled(); - $this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) { - if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) { - return $promise->getWrappedObject(); - } - }, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } } @@ -137,11 +120,7 @@ public function it_loads_cookie_on_subdomains(RequestInterface $request, UriInte $request->withAddedHeader('Cookie', 'name=value')->willReturn($request); - $this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) { - if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) { - return $promise->getWrappedObject(); - } - }, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_load_cookie_if_path_does_not_match(RequestInterface $request, UriInterface $uri, Promise $promise) @@ -155,11 +134,7 @@ public function it_does_not_load_cookie_if_path_does_not_match(RequestInterface $request->withAddedHeader('Cookie', 'name=value')->shouldNotBeCalled(); - $this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) { - if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) { - return $promise->getWrappedObject(); - } - }, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_load_cookie_when_cookie_is_secure(RequestInterface $request, UriInterface $uri, Promise $promise) @@ -174,11 +149,7 @@ public function it_does_not_load_cookie_when_cookie_is_secure(RequestInterface $ $request->withAddedHeader('Cookie', 'name=value')->shouldNotBeCalled(); - $this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) { - if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) { - return $promise->getWrappedObject(); - } - }, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_loads_cookie_when_cookie_is_secure(RequestInterface $request, UriInterface $uri, Promise $promise) @@ -193,11 +164,7 @@ public function it_loads_cookie_when_cookie_is_secure(RequestInterface $request, $request->withAddedHeader('Cookie', 'name=value')->willReturn($request); - $this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) { - if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) { - return $promise->getWrappedObject(); - } - }, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_saves_cookie(RequestInterface $request, ResponseInterface $response, UriInterface $uri) @@ -216,8 +183,8 @@ public function it_saves_cookie(RequestInterface $request, ResponseInterface $re $uri->getPath()->willReturn('/'); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldHaveType('Http\Promise\Promise'); - $promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); + $promise->shouldHaveType(Promise::class); + $promise->wait()->shouldReturnAnInstanceOf(ResponseInterface::class); } public function it_throws_exception_on_invalid_expires_date( @@ -239,7 +206,7 @@ public function it_throws_exception_on_invalid_expires_date( $uri->getPath()->willReturn('/'); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); - $promise->shouldThrow('Http\Client\Exception\TransferException')->duringWait(); + $promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class); + $promise->shouldThrow(TransferException::class)->duringWait(); } } diff --git a/spec/Plugin/DecoderPluginSpec.php b/spec/Plugin/DecoderPluginSpec.php index 4de938c..1316a90 100644 --- a/spec/Plugin/DecoderPluginSpec.php +++ b/spec/Plugin/DecoderPluginSpec.php @@ -9,17 +9,21 @@ use PhpSpec\Exception\Example\SkippingException; use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use Http\Client\Common\Plugin\DecoderPlugin; +use Http\Client\Common\Plugin; +use Http\Message\Encoding\GzipDecodeStream; +use Http\Message\Encoding\DecompressStream; class DecoderPluginSpec extends ObjectBehavior { public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\Plugin\DecoderPlugin'); + $this->shouldHaveType(DecoderPlugin::class); } public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_decodes(RequestInterface $request, ResponseInterface $response, StreamInterface $stream) @@ -60,7 +64,7 @@ public function it_decodes_gzip(RequestInterface $request, ResponseInterface $re $response->hasHeader('Content-Encoding')->willReturn(true); $response->getHeader('Content-Encoding')->willReturn(['gzip']); $response->getBody()->willReturn($stream); - $response->withBody(Argument::type('Http\Message\Encoding\GzipDecodeStream'))->willReturn($response); + $response->withBody(Argument::type(GzipDecodeStream::class))->willReturn($response); $response->withoutHeader('Content-Encoding')->willReturn($response); $stream->isReadable()->willReturn(true); @@ -82,7 +86,7 @@ public function it_decodes_deflate(RequestInterface $request, ResponseInterface $response->hasHeader('Content-Encoding')->willReturn(true); $response->getHeader('Content-Encoding')->willReturn(['deflate']); $response->getBody()->willReturn($stream); - $response->withBody(Argument::type('Http\Message\Encoding\DecompressStream'))->willReturn($response); + $response->withBody(Argument::type(DecompressStream::class))->willReturn($response); $response->withoutHeader('Content-Encoding')->willReturn($response); $stream->isReadable()->willReturn(true); diff --git a/spec/Plugin/ErrorPluginSpec.php b/spec/Plugin/ErrorPluginSpec.php index 5a9e90a..67e5c7e 100644 --- a/spec/Plugin/ErrorPluginSpec.php +++ b/spec/Plugin/ErrorPluginSpec.php @@ -7,17 +7,22 @@ use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use Http\Client\Common\Plugin\ErrorPlugin; +use Http\Client\Common\Plugin; +use Http\Client\Promise\HttpRejectedPromise; +use Http\Client\Common\Exception\ClientErrorException; +use Http\Client\Common\Exception\ServerErrorException; class ErrorPluginSpec extends ObjectBehavior { public function it_is_initializable() { - $this->beAnInstanceOf('Http\Client\Common\Plugin\ErrorPlugin'); + $this->beAnInstanceOf(ErrorPlugin::class); } public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_throw_client_error_exception_on_4xx_error(RequestInterface $request, ResponseInterface $response) @@ -32,8 +37,8 @@ public function it_throw_client_error_exception_on_4xx_error(RequestInterface $r }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); - $promise->shouldThrow('Http\Client\Common\Exception\ClientErrorException')->duringWait(); + $promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class); + $promise->shouldThrow(ClientErrorException::class)->duringWait(); } public function it_does_not_throw_client_error_exception_on_4xx_error_if_only_server_exception(RequestInterface $request, ResponseInterface $response) @@ -49,7 +54,7 @@ public function it_does_not_throw_client_error_exception_on_4xx_error_if_only_se } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf(HttpFulfilledPromise::class); } public function it_throw_server_error_exception_on_5xx_error(RequestInterface $request, ResponseInterface $response) @@ -64,8 +69,8 @@ public function it_throw_server_error_exception_on_5xx_error(RequestInterface $r }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); - $promise->shouldThrow('Http\Client\Common\Exception\ServerErrorException')->duringWait(); + $promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class); + $promise->shouldThrow(ServerErrorException::class)->duringWait(); } public function it_returns_response(RequestInterface $request, ResponseInterface $response) @@ -78,6 +83,6 @@ public function it_returns_response(RequestInterface $request, ResponseInterface } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf(HttpFulfilledPromise::class); } } diff --git a/spec/Plugin/HeaderAppendPluginSpec.php b/spec/Plugin/HeaderAppendPluginSpec.php index bb10de1..9325069 100644 --- a/spec/Plugin/HeaderAppendPluginSpec.php +++ b/spec/Plugin/HeaderAppendPluginSpec.php @@ -2,21 +2,23 @@ namespace spec\Http\Client\Common\Plugin; -use Psr\Http\Message\RequestInterface; +use Http\Client\Common\Plugin; +use Http\Client\Common\Plugin\HeaderAppendPlugin; use PhpSpec\ObjectBehavior; +use Psr\Http\Message\RequestInterface; class HeaderAppendPluginSpec extends ObjectBehavior { public function it_is_initializable() { $this->beConstructedWith([]); - $this->shouldHaveType('Http\Client\Common\Plugin\HeaderAppendPlugin'); + $this->shouldHaveType(HeaderAppendPlugin::class); } public function it_is_a_plugin() { $this->beConstructedWith([]); - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_appends_the_header(RequestInterface $request) @@ -29,6 +31,6 @@ public function it_appends_the_header(RequestInterface $request) $request->withAddedHeader('foo', 'bar')->shouldBeCalled()->willReturn($request); $request->withAddedHeader('baz', 'qux')->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } } diff --git a/spec/Plugin/HeaderDefaultsPluginSpec.php b/spec/Plugin/HeaderDefaultsPluginSpec.php index cd407e3..5a50a9c 100644 --- a/spec/Plugin/HeaderDefaultsPluginSpec.php +++ b/spec/Plugin/HeaderDefaultsPluginSpec.php @@ -2,21 +2,23 @@ namespace spec\Http\Client\Common\Plugin; -use Psr\Http\Message\RequestInterface; +use Http\Client\Common\Plugin; +use Http\Client\Common\Plugin\HeaderDefaultsPlugin; use PhpSpec\ObjectBehavior; +use Psr\Http\Message\RequestInterface; class HeaderDefaultsPluginSpec extends ObjectBehavior { public function it_is_initializable() { $this->beConstructedWith([]); - $this->shouldHaveType('Http\Client\Common\Plugin\HeaderDefaultsPlugin'); + $this->shouldHaveType(HeaderDefaultsPlugin::class); } public function it_is_a_plugin() { $this->beConstructedWith([]); - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_sets_the_default_header(RequestInterface $request) @@ -30,6 +32,6 @@ public function it_sets_the_default_header(RequestInterface $request) $request->withHeader('foo', 'bar')->shouldBeCalled()->willReturn($request); $request->hasHeader('baz')->shouldBeCalled()->willReturn(true); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } } diff --git a/spec/Plugin/HeaderRemovePluginSpec.php b/spec/Plugin/HeaderRemovePluginSpec.php index ec6b069..3f60359 100644 --- a/spec/Plugin/HeaderRemovePluginSpec.php +++ b/spec/Plugin/HeaderRemovePluginSpec.php @@ -2,21 +2,23 @@ namespace spec\Http\Client\Common\Plugin; -use Psr\Http\Message\RequestInterface; +use Http\Client\Common\Plugin; +use Http\Client\Common\Plugin\HeaderRemovePlugin; use PhpSpec\ObjectBehavior; +use Psr\Http\Message\RequestInterface; class HeaderRemovePluginSpec extends ObjectBehavior { public function it_is_initializable() { $this->beConstructedWith([]); - $this->shouldHaveType('Http\Client\Common\Plugin\HeaderRemovePlugin'); + $this->shouldHaveType(HeaderRemovePlugin::class); } public function it_is_a_plugin() { $this->beConstructedWith([]); - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_removes_the_header(RequestInterface $request) @@ -31,6 +33,6 @@ public function it_removes_the_header(RequestInterface $request) $request->hasHeader('baz')->shouldBeCalled()->willReturn(true); $request->withoutHeader('baz')->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } } diff --git a/spec/Plugin/HeaderSetPluginSpec.php b/spec/Plugin/HeaderSetPluginSpec.php index 4e4efd0..b152567 100644 --- a/spec/Plugin/HeaderSetPluginSpec.php +++ b/spec/Plugin/HeaderSetPluginSpec.php @@ -2,21 +2,23 @@ namespace spec\Http\Client\Common\Plugin; -use Psr\Http\Message\RequestInterface; +use Http\Client\Common\Plugin; +use Http\Client\Common\Plugin\HeaderSetPlugin; use PhpSpec\ObjectBehavior; +use Psr\Http\Message\RequestInterface; class HeaderSetPluginSpec extends ObjectBehavior { public function it_is_initializable() { $this->beConstructedWith([]); - $this->shouldHaveType('Http\Client\Common\Plugin\HeaderSetPlugin'); + $this->shouldHaveType(HeaderSetPlugin::class); } public function it_is_a_plugin() { $this->beConstructedWith([]); - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_set_the_header(RequestInterface $request) @@ -29,6 +31,6 @@ public function it_set_the_header(RequestInterface $request) $request->withHeader('foo', 'bar')->shouldBeCalled()->willReturn($request); $request->withHeader('baz', 'qux')->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } } diff --git a/spec/Plugin/HistoryPluginSpec.php b/spec/Plugin/HistoryPluginSpec.php index 77682a7..495e5d5 100644 --- a/spec/Plugin/HistoryPluginSpec.php +++ b/spec/Plugin/HistoryPluginSpec.php @@ -10,6 +10,7 @@ use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use Http\Client\Common\Plugin; class HistoryPluginSpec extends ObjectBehavior { @@ -25,7 +26,7 @@ public function it_is_initializable() public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_records_success(Journal $journal, RequestInterface $request, ResponseInterface $response) diff --git a/spec/Plugin/PluginStub.php b/spec/Plugin/PluginStub.php new file mode 100644 index 0000000..ead2a57 --- /dev/null +++ b/spec/Plugin/PluginStub.php @@ -0,0 +1,25 @@ +shouldHaveType('Http\Client\Common\Plugin\QueryDefaultsPlugin'); + $this->shouldHaveType(QueryDefaultsPlugin::class); } public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_sets_the_default_header(RequestInterface $request, UriInterface $uri) @@ -34,9 +36,7 @@ public function it_sets_the_default_header(RequestInterface $request, UriInterfa $uri->withQuery('test=true&foo=bar')->shouldBeCalled()->willReturn($uri); $request->withUri($uri)->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () { - }, function () { - }); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_replace_existing_request_value(RequestInterface $request, UriInterface $uri) @@ -51,8 +51,6 @@ public function it_does_not_replace_existing_request_value(RequestInterface $req $uri->withQuery('foo=new&bar=barDefault')->shouldBeCalled()->willReturn($uri); $request->withUri($uri)->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () { - }, function () { - }); + $this->handleRequest($request, PluginStub::next(), function () {}); } } diff --git a/spec/Plugin/RedirectPluginSpec.php b/spec/Plugin/RedirectPluginSpec.php index 283bfb5..e24b6b1 100644 --- a/spec/Plugin/RedirectPluginSpec.php +++ b/spec/Plugin/RedirectPluginSpec.php @@ -2,25 +2,30 @@ namespace spec\Http\Client\Common\Plugin; +use Http\Client\Common\Exception\CircularRedirectionException; +use Http\Client\Common\Exception\MultipleRedirectionException; +use Http\Client\Common\Plugin; use Http\Client\Common\Plugin\RedirectPlugin; +use Http\Client\Exception\HttpException; use Http\Client\Promise\HttpFulfilledPromise; +use Http\Client\Promise\HttpRejectedPromise; use Http\Promise\Promise; +use PhpSpec\ObjectBehavior; +use Prophecy\Argument; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; -use PhpSpec\ObjectBehavior; -use Prophecy\Argument; class RedirectPluginSpec extends ObjectBehavior { public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\Plugin\RedirectPlugin'); + $this->shouldHaveType(RedirectPlugin::class); } public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_redirects_on_302( @@ -64,13 +69,13 @@ public function it_redirects_on_302( $promise->wait()->shouldBeCalled()->willReturn($finalResponse); $finalPromise = $this->handleRequest($request, $next, $first); - $finalPromise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); + $finalPromise->shouldReturnAnInstanceOf(HttpFulfilledPromise::class); $finalPromise->wait()->shouldReturn($finalResponse); } public function it_use_storage_on_301(UriInterface $uri, UriInterface $uriRedirect, RequestInterface $request, RequestInterface $modifiedRequest) { - $this->beAnInstanceOf('spec\Http\Client\Common\Plugin\RedirectPluginStub'); + $this->beAnInstanceOf(RedirectPluginStub::class); $this->beConstructedWith($uriRedirect, '/original', '301'); $next = function () { @@ -86,7 +91,7 @@ public function it_use_storage_on_301(UriInterface $uri, UriInterface $uriRedire $uriRedirect->__toString()->willReturn('/redirect'); - $this->handleRequest($request, $next, function () {}); + $this->handleRequest($request, $next, PluginStub::first()); } public function it_stores_a_301( @@ -98,7 +103,7 @@ public function it_stores_a_301( ResponseInterface $finalResponse, Promise $promise ) { - $this->beAnInstanceOf('spec\Http\Client\Common\Plugin\RedirectPluginStub'); + $this->beAnInstanceOf(RedirectPluginStub::class); $this->beConstructedWith($uriRedirect, '', '301'); $request->getUri()->willReturn($uri); @@ -201,8 +206,8 @@ public function it_throws_http_exception_on_no_location(RequestInterface $reques $responseRedirect->hasHeader('Location')->willReturn(false); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); - $promise->shouldThrow('Http\Client\Exception\HttpException')->duringWait(); + $promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class); + $promise->shouldThrow(HttpException::class)->duringWait(); } public function it_throws_http_exception_on_invalid_location(RequestInterface $request, UriInterface $uri, ResponseInterface $responseRedirect) @@ -221,8 +226,8 @@ public function it_throws_http_exception_on_invalid_location(RequestInterface $r $responseRedirect->hasHeader('Location')->willReturn(true); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); - $promise->shouldThrow('Http\Client\Exception\HttpException')->duringWait(); + $promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class); + $promise->shouldThrow(HttpException::class)->duringWait(); } public function it_throw_multi_redirect_exception_on_300(RequestInterface $request, ResponseInterface $responseRedirect) @@ -237,8 +242,8 @@ public function it_throw_multi_redirect_exception_on_300(RequestInterface $reque $responseRedirect->getStatusCode()->willReturn('300'); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); - $promise->shouldThrow('Http\Client\Common\Exception\MultipleRedirectionException')->duringWait(); + $promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class); + $promise->shouldThrow(MultipleRedirectionException::class)->duringWait(); } public function it_throw_multi_redirect_exception_on_300_if_no_location(RequestInterface $request, ResponseInterface $responseRedirect) @@ -253,8 +258,8 @@ public function it_throw_multi_redirect_exception_on_300_if_no_location(RequestI $responseRedirect->hasHeader('Location')->willReturn(false); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); - $promise->shouldThrow('Http\Client\Common\Exception\MultipleRedirectionException')->duringWait(); + $promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class); + $promise->shouldThrow(MultipleRedirectionException::class)->duringWait(); } public function it_switch_method_for_302( @@ -358,7 +363,7 @@ public function it_throws_circular_redirection_exception(UriInterface $uri, UriI { $first = function () {}; - $this->beAnInstanceOf('spec\Http\Client\Common\Plugin\RedirectPluginStubCircular'); + $this->beAnInstanceOf(RedirectPluginStubCircular::class); $this->beConstructedWith(spl_object_hash((object) $first)); $request->getUri()->willReturn($uri); @@ -384,8 +389,8 @@ public function it_throws_circular_redirection_exception(UriInterface $uri, UriI }; $promise = $this->handleRequest($request, $next, $first); - $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); - $promise->shouldThrow('Http\Client\Common\Exception\CircularRedirectionException')->duringWait(); + $promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class); + $promise->shouldThrow(CircularRedirectionException::class)->duringWait(); } public function it_redirects_http_to_https( @@ -431,7 +436,7 @@ public function it_redirects_http_to_https( $promise->wait()->shouldBeCalled()->willReturn($finalResponse); $finalPromise = $this->handleRequest($request, $next, $first); - $finalPromise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); + $finalPromise->shouldReturnAnInstanceOf(HttpFulfilledPromise::class); $finalPromise->wait()->shouldReturn($finalResponse); } } diff --git a/spec/Plugin/RequestMatcherPluginSpec.php b/spec/Plugin/RequestMatcherPluginSpec.php index bd4e7e4..de7cae6 100644 --- a/spec/Plugin/RequestMatcherPluginSpec.php +++ b/spec/Plugin/RequestMatcherPluginSpec.php @@ -8,6 +8,7 @@ use Psr\Http\Message\RequestInterface; use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use Http\Client\Common\Plugin\RequestMatcherPlugin; class RequestMatcherPluginSpec extends ObjectBehavior { @@ -18,12 +19,12 @@ public function let(RequestMatcher $requestMatcher, Plugin $plugin) public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\Plugin\RequestMatcherPlugin'); + $this->shouldHaveType(RequestMatcherPlugin::class); } public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_matches_a_request_and_delegates_to_plugin( @@ -34,7 +35,7 @@ public function it_matches_a_request_and_delegates_to_plugin( $requestMatcher->matches($request)->willReturn(true); $plugin->handleRequest($request, Argument::type('callable'), Argument::type('callable'))->shouldBeCalled(); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, PluginStub::next(), function () {}); } public function it_does_not_match_a_request( diff --git a/spec/Plugin/RetryPluginSpec.php b/spec/Plugin/RetryPluginSpec.php index c439cad..682b892 100644 --- a/spec/Plugin/RetryPluginSpec.php +++ b/spec/Plugin/RetryPluginSpec.php @@ -9,17 +9,19 @@ use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; use Prophecy\Argument; +use Http\Client\Common\Plugin\RetryPlugin; +use Http\Client\Common\Plugin; class RetryPluginSpec extends ObjectBehavior { public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\Plugin\RetryPlugin'); + $this->shouldHaveType(RetryPlugin::class); } public function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_returns_response(RequestInterface $request, ResponseInterface $response) @@ -30,7 +32,7 @@ public function it_returns_response(RequestInterface $request, ResponseInterface } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf(HttpFulfilledPromise::class); } public function it_throws_exception_on_multiple_exceptions(RequestInterface $request) @@ -53,7 +55,7 @@ public function it_throws_exception_on_multiple_exceptions(RequestInterface $req }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); + $promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class); $promise->shouldThrow($exception2)->duringWait(); } @@ -76,7 +78,7 @@ public function it_returns_response_on_second_try(RequestInterface $request, Res }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); + $promise->shouldReturnAnInstanceOf(HttpFulfilledPromise::class); $promise->wait()->shouldReturn($response); } @@ -98,8 +100,8 @@ public function it_does_not_keep_history_of_old_failure(RequestInterface $reques } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf(HttpFulfilledPromise::class); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf(HttpFulfilledPromise::class); } public function it_has_an_exponential_default_delay(RequestInterface $request, Exception\HttpException $exception) diff --git a/spec/PluginClientFactorySpec.php b/spec/PluginClientFactorySpec.php index fe7e076..d7a3acb 100644 --- a/spec/PluginClientFactorySpec.php +++ b/spec/PluginClientFactorySpec.php @@ -4,19 +4,21 @@ use Http\Client\HttpClient; use PhpSpec\ObjectBehavior; +use Http\Client\Common\PluginClientFactory; +use Http\Client\Common\PluginClient; class PluginClientFactorySpec extends ObjectBehavior { public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\PluginClientFactory'); + $this->shouldHaveType(PluginClientFactory::class); } public function it_returns_a_plugin_client(HttpClient $httpClient) { $client = $this->createClient($httpClient); - $client->shouldHaveType('Http\Client\Common\PluginClient'); + $client->shouldHaveType(PluginClient::class); } public function it_does_not_construct_plugin_client_with_client_name_option(HttpClient $httpClient) diff --git a/spec/PluginClientSpec.php b/spec/PluginClientSpec.php index 50df636..c5c2932 100644 --- a/spec/PluginClientSpec.php +++ b/spec/PluginClientSpec.php @@ -10,6 +10,8 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; +use Http\Client\Common\Exception\LoopException; +use Http\Client\Common\PluginClient; class PluginClientSpec extends ObjectBehavior { @@ -20,17 +22,17 @@ public function let(HttpClient $httpClient) public function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\PluginClient'); + $this->shouldHaveType(PluginClient::class); } public function it_is_an_http_client() { - $this->shouldImplement('Http\Client\HttpClient'); + $this->shouldImplement(HttpClient::class); } public function it_is_an_http_async_client() { - $this->shouldImplement('Http\Client\HttpAsyncClient'); + $this->shouldImplement(HttpAsyncClient::class); } public function it_sends_request_with_underlying_client(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response) @@ -59,8 +61,8 @@ public function it_sends_async_request_if_no_send_request(HttpAsyncClient $httpA public function it_prefers_send_request($client, RequestInterface $request, ResponseInterface $response) { - $client->implement('Http\Client\HttpClient'); - $client->implement('Http\Client\HttpAsyncClient'); + $client->implement(HttpClient::class); + $client->implement(HttpAsyncClient::class); $client->sendRequest($request)->willReturn($response); @@ -84,6 +86,6 @@ public function it_throws_loop_exception(HttpClient $httpClient, RequestInterfac $this->beConstructedWith($httpClient, [$plugin]); - $this->shouldThrow('Http\Client\Common\Exception\LoopException')->duringSendRequest($request); + $this->shouldThrow(LoopException::class)->duringSendRequest($request); } } diff --git a/src/BatchClient.php b/src/BatchClient.php index 7ddba4e..8dfeb81 100644 --- a/src/BatchClient.php +++ b/src/BatchClient.php @@ -49,7 +49,7 @@ public function sendRequest(RequestInterface $request): ResponseInterface * BatchResult with a map of request to result for success, request to * exception for failures */ - public function sendRequests(array $requests) + public function sendRequests(array $requests): BatchResult { $batchResult = new BatchResult(); diff --git a/src/BatchResult.php b/src/BatchResult.php index 3f8a2f0..94ff51a 100644 --- a/src/BatchResult.php +++ b/src/BatchResult.php @@ -31,10 +31,8 @@ public function __construct() /** * Checks if there are any successful responses at all. - * - * @return bool */ - public function hasResponses() + public function hasResponses(): bool { return $this->responses->count() > 0; } @@ -44,7 +42,7 @@ public function hasResponses() * * @return ResponseInterface[] */ - public function getResponses() + public function getResponses(): array { $responses = []; @@ -57,10 +55,8 @@ public function getResponses() /** * Checks if there is a successful response for a request. - * - * @return bool */ - public function isSuccessful(RequestInterface $request) + public function isSuccessful(RequestInterface $request): bool { return $this->responses->contains($request); } @@ -68,11 +64,10 @@ public function isSuccessful(RequestInterface $request) /** * Returns the response for a successful request. * - * @return ResponseInterface * * @throws \UnexpectedValueException If request was not part of the batch or failed */ - public function getResponseFor(RequestInterface $request) + public function getResponseFor(RequestInterface $request): ResponseInterface { try { return $this->responses[$request]; @@ -86,7 +81,7 @@ public function getResponseFor(RequestInterface $request) * * @return BatchResult the new BatchResult with this request-response pair added to it */ - public function addResponse(RequestInterface $request, ResponseInterface $response) + public function addResponse(RequestInterface $request, ResponseInterface $response): self { $new = clone $this; $new->responses->attach($request, $response); @@ -96,10 +91,8 @@ public function addResponse(RequestInterface $request, ResponseInterface $respon /** * Checks if there are any unsuccessful requests at all. - * - * @return bool */ - public function hasExceptions() + public function hasExceptions(): bool { return $this->exceptions->count() > 0; } @@ -109,7 +102,7 @@ public function hasExceptions() * * @return Exception[] */ - public function getExceptions() + public function getExceptions(): array { $exceptions = []; @@ -122,10 +115,8 @@ public function getExceptions() /** * Checks if there is an exception for a request, meaning the request failed. - * - * @return bool */ - public function isFailed(RequestInterface $request) + public function isFailed(RequestInterface $request): bool { return $this->exceptions->contains($request); } @@ -133,11 +124,10 @@ public function isFailed(RequestInterface $request) /** * Returns the exception for a failed request. * - * @return Exception * * @throws \UnexpectedValueException If request was not part of the batch or was successful */ - public function getExceptionFor(RequestInterface $request) + public function getExceptionFor(RequestInterface $request): Exception { try { return $this->exceptions[$request]; @@ -151,7 +141,7 @@ public function getExceptionFor(RequestInterface $request) * * @return BatchResult the new BatchResult with this request-exception pair added to it */ - public function addException(RequestInterface $request, Exception $exception) + public function addException(RequestInterface $request, Exception $exception): self { $new = clone $this; $new->exceptions->attach($request, $exception); diff --git a/src/Deferred.php b/src/Deferred.php index 075a30e..7413451 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -34,7 +34,7 @@ public function __construct(callable $waitCallback) /** * {@inheritdoc} */ - public function then(callable $onFulfilled = null, callable $onRejected = null) + public function then(callable $onFulfilled = null, callable $onRejected = null): Promise { $deferred = new self($this->waitCallback); @@ -69,7 +69,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null) /** * {@inheritdoc} */ - public function getState() + public function getState(): string { return $this->state; } diff --git a/src/HttpClientPool.php b/src/HttpClientPool.php index 6f4597f..90a8464 100644 --- a/src/HttpClientPool.php +++ b/src/HttpClientPool.php @@ -40,7 +40,7 @@ public function addHttpClient($client) * * @return HttpClientPoolItem Return a http client that can do both sync or async */ - abstract protected function chooseHttpClient(); + abstract protected function chooseHttpClient(): HttpClientPoolItem; /** * {@inheritdoc} diff --git a/src/HttpClientPool/LeastUsedClientPool.php b/src/HttpClientPool/LeastUsedClientPool.php index 6299cce..61eb670 100644 --- a/src/HttpClientPool/LeastUsedClientPool.php +++ b/src/HttpClientPool/LeastUsedClientPool.php @@ -18,7 +18,7 @@ final class LeastUsedClientPool extends HttpClientPool /** * {@inheritdoc} */ - protected function chooseHttpClient() + protected function chooseHttpClient(): HttpClientPoolItem { $clientPool = array_filter($this->clientPool, function (HttpClientPoolItem $clientPoolItem) { return !$clientPoolItem->isDisabled(); diff --git a/src/HttpClientPool/RandomClientPool.php b/src/HttpClientPool/RandomClientPool.php index 3255f86..2889ed7 100644 --- a/src/HttpClientPool/RandomClientPool.php +++ b/src/HttpClientPool/RandomClientPool.php @@ -16,7 +16,7 @@ final class RandomClientPool extends HttpClientPool /** * {@inheritdoc} */ - protected function chooseHttpClient() + protected function chooseHttpClient(): HttpClientPoolItem { $clientPool = array_filter($this->clientPool, function (HttpClientPoolItem $clientPoolItem) { return !$clientPoolItem->isDisabled(); diff --git a/src/HttpClientPool/RoundRobinClientPool.php b/src/HttpClientPool/RoundRobinClientPool.php index 8d8e40a..3315b51 100644 --- a/src/HttpClientPool/RoundRobinClientPool.php +++ b/src/HttpClientPool/RoundRobinClientPool.php @@ -4,6 +4,7 @@ use Http\Client\Common\Exception\HttpClientNotFoundException; use Http\Client\Common\HttpClientPool; +use Http\Client\Common\HttpClientPoolItem; /** * RoundRobinClientPool will choose the next client in the pool. @@ -15,7 +16,7 @@ final class RoundRobinClientPool extends HttpClientPool /** * {@inheritdoc} */ - protected function chooseHttpClient() + protected function chooseHttpClient(): HttpClientPoolItem { $last = current($this->clientPool); diff --git a/src/HttpMethodsClient.php b/src/HttpMethodsClient.php index a1ef2c8..bc02ef8 100644 --- a/src/HttpMethodsClient.php +++ b/src/HttpMethodsClient.php @@ -52,10 +52,8 @@ public function __construct(HttpClient $httpClient, RequestFactory $requestFacto * @param string|UriInterface $uri * * @throws Exception - * - * @return ResponseInterface */ - public function get($uri, array $headers = []) + public function get($uri, array $headers = []): ResponseInterface { return $this->send('GET', $uri, $headers, null); } @@ -66,10 +64,8 @@ public function get($uri, array $headers = []) * @param string|UriInterface $uri * * @throws Exception - * - * @return ResponseInterface */ - public function head($uri, array $headers = []) + public function head($uri, array $headers = []): ResponseInterface { return $this->send('HEAD', $uri, $headers, null); } @@ -80,10 +76,8 @@ public function head($uri, array $headers = []) * @param string|UriInterface $uri * * @throws Exception - * - * @return ResponseInterface */ - public function trace($uri, array $headers = []) + public function trace($uri, array $headers = []): ResponseInterface { return $this->send('TRACE', $uri, $headers, null); } @@ -95,10 +89,8 @@ public function trace($uri, array $headers = []) * @param string|StreamInterface|null $body * * @throws Exception - * - * @return ResponseInterface */ - public function post($uri, array $headers = [], $body = null) + public function post($uri, array $headers = [], $body = null): ResponseInterface { return $this->send('POST', $uri, $headers, $body); } @@ -110,10 +102,8 @@ public function post($uri, array $headers = [], $body = null) * @param string|StreamInterface|null $body * * @throws Exception - * - * @return ResponseInterface */ - public function put($uri, array $headers = [], $body = null) + public function put($uri, array $headers = [], $body = null): ResponseInterface { return $this->send('PUT', $uri, $headers, $body); } @@ -125,10 +115,8 @@ public function put($uri, array $headers = [], $body = null) * @param string|StreamInterface|null $body * * @throws Exception - * - * @return ResponseInterface */ - public function patch($uri, array $headers = [], $body = null) + public function patch($uri, array $headers = [], $body = null): ResponseInterface { return $this->send('PATCH', $uri, $headers, $body); } @@ -140,10 +128,8 @@ public function patch($uri, array $headers = [], $body = null) * @param string|StreamInterface|null $body * * @throws Exception - * - * @return ResponseInterface */ - public function delete($uri, array $headers = [], $body = null) + public function delete($uri, array $headers = [], $body = null): ResponseInterface { return $this->send('DELETE', $uri, $headers, $body); } @@ -155,10 +141,8 @@ public function delete($uri, array $headers = [], $body = null) * @param string|StreamInterface|null $body * * @throws Exception - * - * @return ResponseInterface */ - public function options($uri, array $headers = [], $body = null) + public function options($uri, array $headers = [], $body = null): ResponseInterface { return $this->send('OPTIONS', $uri, $headers, $body); } @@ -171,10 +155,8 @@ public function options($uri, array $headers = [], $body = null) * @param string|StreamInterface|null $body * * @throws Exception - * - * @return ResponseInterface */ - public function send($method, $uri, array $headers = [], $body = null) + public function send($method, $uri, array $headers = [], $body = null): ResponseInterface { return $this->sendRequest($this->requestFactory->createRequest( $method, diff --git a/src/Plugin.php b/src/Plugin.php index bfb2668..ab7e4a1 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -27,5 +27,5 @@ interface Plugin * * @return Promise Resolves a PSR-7 Response or fails with an Http\Client\Exception (The same as HttpAsyncClient) */ - public function handleRequest(RequestInterface $request, callable $next, callable $first); + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise; } diff --git a/src/Plugin/AddHostPlugin.php b/src/Plugin/AddHostPlugin.php index 050feb1..492667e 100644 --- a/src/Plugin/AddHostPlugin.php +++ b/src/Plugin/AddHostPlugin.php @@ -3,6 +3,7 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\UriInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -48,7 +49,7 @@ public function __construct(UriInterface $host, array $config = []) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { if ($this->replace || '' === $request->getUri()->getHost()) { $uri = $request->getUri() diff --git a/src/Plugin/AddPathPlugin.php b/src/Plugin/AddPathPlugin.php index 1675088..ae5d97d 100644 --- a/src/Plugin/AddPathPlugin.php +++ b/src/Plugin/AddPathPlugin.php @@ -3,6 +3,7 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\UriInterface; @@ -41,7 +42,7 @@ public function __construct(UriInterface $uri) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $identifier = spl_object_hash((object) $first); diff --git a/src/Plugin/AuthenticationPlugin.php b/src/Plugin/AuthenticationPlugin.php index ff33f9b..5b6c447 100644 --- a/src/Plugin/AuthenticationPlugin.php +++ b/src/Plugin/AuthenticationPlugin.php @@ -4,6 +4,7 @@ use Http\Client\Common\Plugin; use Http\Message\Authentication; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; /** @@ -26,7 +27,7 @@ public function __construct(Authentication $authentication) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $request = $this->authentication->authenticate($request); diff --git a/src/Plugin/BaseUriPlugin.php b/src/Plugin/BaseUriPlugin.php index 669c308..ee9e075 100644 --- a/src/Plugin/BaseUriPlugin.php +++ b/src/Plugin/BaseUriPlugin.php @@ -3,6 +3,7 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\UriInterface; @@ -39,7 +40,7 @@ public function __construct(UriInterface $uri, array $hostConfig = []) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $addHostNext = function (RequestInterface $request) use ($next, $first) { return $this->addHostPlugin->handleRequest($request, $next, $first); diff --git a/src/Plugin/ContentLengthPlugin.php b/src/Plugin/ContentLengthPlugin.php index 0f7aafa..dff2ca8 100644 --- a/src/Plugin/ContentLengthPlugin.php +++ b/src/Plugin/ContentLengthPlugin.php @@ -4,6 +4,7 @@ use Http\Client\Common\Plugin; use Http\Message\Encoding\ChunkStream; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; /** @@ -16,7 +17,7 @@ final class ContentLengthPlugin implements Plugin /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { if (!$request->hasHeader('Content-Length')) { $stream = $request->getBody(); diff --git a/src/Plugin/ContentTypePlugin.php b/src/Plugin/ContentTypePlugin.php index d0bfa8d..190f125 100644 --- a/src/Plugin/ContentTypePlugin.php +++ b/src/Plugin/ContentTypePlugin.php @@ -3,6 +3,7 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\StreamInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -57,7 +58,7 @@ public function __construct(array $config = []) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { if (!$request->hasHeader('Content-Type')) { $stream = $request->getBody(); @@ -93,10 +94,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl /** * @param $stream StreamInterface - * - * @return bool */ - private function isJson($stream) + private function isJson($stream): bool { $stream->rewind(); diff --git a/src/Plugin/CookiePlugin.php b/src/Plugin/CookiePlugin.php index 3ab5388..84669cf 100644 --- a/src/Plugin/CookiePlugin.php +++ b/src/Plugin/CookiePlugin.php @@ -8,6 +8,7 @@ use Http\Message\CookieJar; use Http\Message\CookieUtil; use Http\Message\Exception\UnexpectedValueException; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -33,7 +34,7 @@ public function __construct(CookieJar $cookieJar) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $cookies = []; foreach ($this->cookieJar->getCookies() as $cookie) { @@ -99,7 +100,7 @@ private function createCookie(RequestInterface $request, $setCookie) $parts = array_map('trim', explode(';', $setCookie)); if (empty($parts) || !strpos($parts[0], '=')) { - return; + return null; } list($name, $cookieValue) = $this->createValueKey(array_shift($parts)); diff --git a/src/Plugin/DecoderPlugin.php b/src/Plugin/DecoderPlugin.php index 3a28f5b..0930779 100644 --- a/src/Plugin/DecoderPlugin.php +++ b/src/Plugin/DecoderPlugin.php @@ -4,6 +4,7 @@ use Http\Client\Common\Plugin; use Http\Message\Encoding; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; @@ -48,7 +49,7 @@ public function __construct(array $config = []) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $encodings = extension_loaded('zlib') ? ['gzip', 'deflate'] : ['identity']; diff --git a/src/Plugin/ErrorPlugin.php b/src/Plugin/ErrorPlugin.php index 4fd7201..1d25fb7 100644 --- a/src/Plugin/ErrorPlugin.php +++ b/src/Plugin/ErrorPlugin.php @@ -5,6 +5,7 @@ use Http\Client\Common\Exception\ClientErrorException; use Http\Client\Common\Exception\ServerErrorException; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -46,7 +47,7 @@ public function __construct(array $config = []) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $promise = $next($request); @@ -66,7 +67,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl * * @return ResponseInterface If status code is not in 4xx or 5xx return response */ - private function transformResponseToException(RequestInterface $request, ResponseInterface $response) + private function transformResponseToException(RequestInterface $request, ResponseInterface $response): ResponseInterface { if (!$this->onlyServerException && $response->getStatusCode() >= 400 && $response->getStatusCode() < 500) { throw new ClientErrorException($response->getReasonPhrase(), $request, $response); diff --git a/src/Plugin/HeaderAppendPlugin.php b/src/Plugin/HeaderAppendPlugin.php index 26fd813..ca44836 100644 --- a/src/Plugin/HeaderAppendPlugin.php +++ b/src/Plugin/HeaderAppendPlugin.php @@ -3,6 +3,7 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; /** @@ -21,7 +22,7 @@ final class HeaderAppendPlugin implements Plugin /** * @var array */ - private $headers = []; + private $headers; /** * @param array $headers Hashmap of header name to header value @@ -34,7 +35,7 @@ public function __construct(array $headers) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { foreach ($this->headers as $header => $headerValue) { $request = $request->withAddedHeader($header, $headerValue); diff --git a/src/Plugin/HeaderDefaultsPlugin.php b/src/Plugin/HeaderDefaultsPlugin.php index 6dfc111..77ffe8c 100644 --- a/src/Plugin/HeaderDefaultsPlugin.php +++ b/src/Plugin/HeaderDefaultsPlugin.php @@ -3,6 +3,7 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; /** @@ -30,7 +31,7 @@ public function __construct(array $headers) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { foreach ($this->headers as $header => $headerValue) { if (!$request->hasHeader($header)) { diff --git a/src/Plugin/HeaderRemovePlugin.php b/src/Plugin/HeaderRemovePlugin.php index fc9c19d..0a34248 100644 --- a/src/Plugin/HeaderRemovePlugin.php +++ b/src/Plugin/HeaderRemovePlugin.php @@ -3,6 +3,7 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; /** @@ -28,7 +29,7 @@ public function __construct(array $headers) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { foreach ($this->headers as $header) { if ($request->hasHeader($header)) { diff --git a/src/Plugin/HeaderSetPlugin.php b/src/Plugin/HeaderSetPlugin.php index 75f11d4..210bb42 100644 --- a/src/Plugin/HeaderSetPlugin.php +++ b/src/Plugin/HeaderSetPlugin.php @@ -3,6 +3,7 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; /** @@ -17,7 +18,7 @@ final class HeaderSetPlugin implements Plugin /** * @var array */ - private $headers = []; + private $headers; /** * @param array $headers Hashmap of header name to header value @@ -30,7 +31,7 @@ public function __construct(array $headers) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { foreach ($this->headers as $header => $headerValue) { $request = $request->withHeader($header, $headerValue); diff --git a/src/Plugin/HistoryPlugin.php b/src/Plugin/HistoryPlugin.php index 0c59a2a..7b14485 100644 --- a/src/Plugin/HistoryPlugin.php +++ b/src/Plugin/HistoryPlugin.php @@ -4,6 +4,7 @@ use Http\Client\Common\Plugin; use Http\Client\Exception; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -29,7 +30,7 @@ public function __construct(Journal $journal) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $journal = $this->journal; diff --git a/src/Plugin/QueryDefaultsPlugin.php b/src/Plugin/QueryDefaultsPlugin.php index d9c06d6..2693507 100644 --- a/src/Plugin/QueryDefaultsPlugin.php +++ b/src/Plugin/QueryDefaultsPlugin.php @@ -3,6 +3,7 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; /** @@ -31,7 +32,7 @@ public function __construct(array $queryParams) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $uri = $request->getUri(); diff --git a/src/Plugin/RedirectPlugin.php b/src/Plugin/RedirectPlugin.php index d2f442e..80c5251 100644 --- a/src/Plugin/RedirectPlugin.php +++ b/src/Plugin/RedirectPlugin.php @@ -6,6 +6,7 @@ use Http\Client\Common\Exception\MultipleRedirectionException; use Http\Client\Common\Plugin; use Http\Client\Exception\HttpException; +use Http\Promise\Promise; use Psr\Http\Message\MessageInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -131,7 +132,7 @@ public function __construct(array $config = []) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { // Check in storage if (array_key_exists((string) $request->getUri(), $this->redirectStorage)) { @@ -215,10 +216,8 @@ protected function buildRedirectRequest(RequestInterface $request, UriInterface * * @throws HttpException If location header is not usable (missing or incorrect) * @throws MultipleRedirectionException If a 300 status code is received and default location cannot be resolved (doesn't use the location header or not present) - * - * @return UriInterface */ - private function createUri(ResponseInterface $response, RequestInterface $request) + private function createUri(ResponseInterface $response, RequestInterface $request): UriInterface { if ($this->redirectCodes[$response->getStatusCode()]['multiple'] && (!$this->useDefaultForMultiple || !$response->hasHeader('Location'))) { throw new MultipleRedirectionException('Cannot choose a redirection', $request, $response); diff --git a/src/Plugin/RequestMatcherPlugin.php b/src/Plugin/RequestMatcherPlugin.php index dd97b3c..d7d3f3d 100644 --- a/src/Plugin/RequestMatcherPlugin.php +++ b/src/Plugin/RequestMatcherPlugin.php @@ -4,6 +4,7 @@ use Http\Client\Common\Plugin; use Http\Message\RequestMatcher; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; /** @@ -32,7 +33,7 @@ public function __construct(RequestMatcher $requestMatcher, Plugin $delegatedPlu /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { if ($this->requestMatcher->matches($request)) { return $this->delegatedPlugin->handleRequest($request, $next, $first); diff --git a/src/Plugin/RetryPlugin.php b/src/Plugin/RetryPlugin.php index 96cfbb2..c10bce4 100644 --- a/src/Plugin/RetryPlugin.php +++ b/src/Plugin/RetryPlugin.php @@ -4,6 +4,7 @@ use Http\Client\Common\Plugin; use Http\Client\Exception; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -72,7 +73,7 @@ public function __construct(array $config = []) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $chainIdentifier = spl_object_hash((object) $first); diff --git a/src/PluginClient.php b/src/PluginClient.php index 6881c93..264c2fe 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -101,10 +101,8 @@ public function sendAsyncRequest(RequestInterface $request) /** * Configure the plugin client. - * - * @return array */ - private function configure(array $options = []) + private function configure(array $options = []): array { $resolver = new OptionsResolver(); $resolver->setDefaults([ @@ -121,10 +119,8 @@ private function configure(array $options = []) * * @param Plugin[] $pluginList A list of plugins * @param callable $clientCallable Callable making the HTTP call - * - * @return callable */ - private function createPluginChain($pluginList, callable $clientCallable) + private function createPluginChain(array $pluginList, callable $clientCallable): callable { $firstCallable = $lastCallable = $clientCallable; diff --git a/src/PluginClientFactory.php b/src/PluginClientFactory.php index a262f80..77642ea 100644 --- a/src/PluginClientFactory.php +++ b/src/PluginClientFactory.php @@ -42,10 +42,8 @@ public static function setFactory(callable $factory) * } * * @see PluginClient constructor for PluginClient specific $options. - * - * @return PluginClient */ - public function createClient($client, array $plugins = [], array $options = []) + public function createClient($client, array $plugins = [], array $options = []): PluginClient { if (static::$factory) { $factory = static::$factory;