Skip to content

[2.0] Add types #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 22, 2018
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 8 additions & 4 deletions spec/BatchClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,31 @@
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)
{
$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]);
}
}
7 changes: 4 additions & 3 deletions spec/BatchResultSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
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)
{
$new = $this->addResponse($request, $response);

$this->getResponses()->shouldReturn([]);
$new->shouldHaveType('Http\Client\Common\BatchResult');
$new->shouldHaveType(BatchResult::class);
$new->getResponses()->shouldReturn([$response]);
}

Expand All @@ -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);
Expand Down
17 changes: 11 additions & 6 deletions spec/EmulatedHttpAsyncClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(
Expand All @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions spec/EmulatedHttpClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(
Expand All @@ -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(
Expand Down
10 changes: 6 additions & 4 deletions spec/Exception/BatchExceptionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
15 changes: 8 additions & 7 deletions spec/FlexibleHttpClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(
Expand All @@ -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);
}

Expand All @@ -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();
Expand Down
25 changes: 14 additions & 11 deletions spec/HttpClientPool/LeastUsedClientPoolSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading