Skip to content

Commit 543b2a2

Browse files
Jean85sagikazarmark
authored andcommitted
[2.0] Add types (#117)
* Add types where possible * Add spec to autoload-dev * Fix signatures * Use ::class anywhere * Add `: Promise` return type to Plugin::handleRequest
1 parent bb98f24 commit 543b2a2

File tree

64 files changed

+415
-344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+415
-344
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 2.0 (unreleased)
44

5+
### Changed
6+
- Abstract method `HttpClientPool::chooseHttpClient()` has now an explicit return type (`Http\Client\Common\HttpClientPoolItem`)
7+
- Interface method `Plugin::handleRequest(...)` has now an explicit return type (`Http\Promise\Promise`)
8+
59
### Removed
610
- Deprecated option `debug_plugins` has been removed from `PluginClient`
711

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
"Http\\Client\\Common\\": "src/"
3636
}
3737
},
38+
"autoload-dev": {
39+
"psr-4": {
40+
"spec\\Http\\Client\\Common\\": "spec/"
41+
}
42+
},
3843
"scripts": {
3944
"cs-check": "vendor/bin/php-cs-fixer fix --dry-run",
4045
"cs-fix": "vendor/bin/php-cs-fixer fix",

spec/BatchClientSpec.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,31 @@
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88
use PhpSpec\ObjectBehavior;
9+
use Http\Client\Common\BatchClient;
10+
use Http\Client\Common\BatchResult;
11+
use Http\Client\Exception\HttpException;
12+
use Http\Client\Common\Exception\BatchException;
913

1014
class BatchClientSpec extends ObjectBehavior
1115
{
1216
public function let(HttpClient $client)
1317
{
14-
$this->beAnInstanceOf('Http\Client\Common\BatchClient', [$client]);
18+
$this->beAnInstanceOf(BatchClient::class, [$client]);
1519
}
1620

1721
public function it_send_multiple_request_using_send_request(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response1, ResponseInterface $response2)
1822
{
1923
$client->sendRequest($request1)->willReturn($response1);
2024
$client->sendRequest($request2)->willReturn($response2);
2125

22-
$this->sendRequests([$request1, $request2])->shouldReturnAnInstanceOf('Http\Client\Common\BatchResult');
26+
$this->sendRequests([$request1, $request2])->shouldReturnAnInstanceOf(BatchResult::class);
2327
}
2428

2529
public function it_throw_batch_exception_if_one_or_more_request_failed(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response)
2630
{
2731
$client->sendRequest($request1)->willReturn($response);
28-
$client->sendRequest($request2)->willThrow('Http\Client\Exception\HttpException');
32+
$client->sendRequest($request2)->willThrow(HttpException::class);
2933

30-
$this->shouldThrow('Http\Client\Common\Exception\BatchException')->duringSendRequests([$request1, $request2]);
34+
$this->shouldThrow(BatchException::class)->duringSendRequests([$request1, $request2]);
3135
}
3236
}

spec/BatchResultSpec.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88
use PhpSpec\ObjectBehavior;
9+
use Http\Client\Common\BatchResult;
910

1011
class BatchResultSpec extends ObjectBehavior
1112
{
1213
public function it_is_initializable()
1314
{
14-
$this->beAnInstanceOf('Http\Client\Common\BatchResult');
15+
$this->beAnInstanceOf(BatchResult::class);
1516
}
1617

1718
public function it_is_immutable(RequestInterface $request, ResponseInterface $response)
1819
{
1920
$new = $this->addResponse($request, $response);
2021

2122
$this->getResponses()->shouldReturn([]);
22-
$new->shouldHaveType('Http\Client\Common\BatchResult');
23+
$new->shouldHaveType(BatchResult::class);
2324
$new->getResponses()->shouldReturn([$response]);
2425
}
2526

@@ -37,7 +38,7 @@ public function it_has_a_response_for_a_request(RequestInterface $request, Respo
3738
{
3839
$new = $this->addResponse($request, $response);
3940

40-
$this->shouldThrow('UnexpectedValueException')->duringGetResponseFor($request);
41+
$this->shouldThrow(\UnexpectedValueException::class)->duringGetResponseFor($request);
4142
$this->isSuccessful($request)->shouldReturn(false);
4243
$new->getResponseFor($request)->shouldReturn($response);
4344
$new->isSuccessful($request)->shouldReturn(true);

spec/EmulatedHttpAsyncClientSpec.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88
use PhpSpec\ObjectBehavior;
9+
use Http\Client\Common\EmulatedHttpAsyncClient;
10+
use Http\Client\HttpAsyncClient;
11+
use Http\Client\Promise\HttpFulfilledPromise;
12+
use Http\Client\Exception\TransferException;
13+
use Http\Client\Promise\HttpRejectedPromise;
914

1015
class EmulatedHttpAsyncClientSpec extends ObjectBehavior
1116
{
@@ -16,17 +21,17 @@ public function let(HttpClient $httpClient)
1621

1722
public function it_is_initializable()
1823
{
19-
$this->shouldHaveType('Http\Client\Common\EmulatedHttpAsyncClient');
24+
$this->shouldHaveType(EmulatedHttpAsyncClient::class);
2025
}
2126

2227
public function it_is_an_http_client()
2328
{
24-
$this->shouldImplement('Http\Client\HttpClient');
29+
$this->shouldImplement(HttpClient::class);
2530
}
2631

2732
public function it_is_an_async_http_client()
2833
{
29-
$this->shouldImplement('Http\Client\HttpAsyncClient');
34+
$this->shouldImplement(HttpAsyncClient::class);
3035
}
3136

3237
public function it_emulates_a_successful_request(
@@ -36,14 +41,14 @@ public function it_emulates_a_successful_request(
3641
) {
3742
$httpClient->sendRequest($request)->willReturn($response);
3843

39-
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
44+
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf(HttpFulfilledPromise::class);
4045
}
4146

4247
public function it_emulates_a_failed_request(HttpClient $httpClient, RequestInterface $request)
4348
{
44-
$httpClient->sendRequest($request)->willThrow('Http\Client\Exception\TransferException');
49+
$httpClient->sendRequest($request)->willThrow(TransferException::class);
4550

46-
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
51+
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf(HttpRejectedPromise::class);
4752
}
4853

4954
public function it_decorates_the_underlying_client(

spec/EmulatedHttpClientSpec.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
use Http\Client\Exception\TransferException;
66
use Http\Client\HttpAsyncClient;
7+
use Http\Client\HttpClient;
78
use Http\Promise\Promise;
89
use Psr\Http\Message\RequestInterface;
910
use Psr\Http\Message\ResponseInterface;
1011
use PhpSpec\ObjectBehavior;
12+
use Http\Client\Common\EmulatedHttpClient;
13+
use Http\Client\Exception;
1114

1215
class EmulatedHttpClientSpec extends ObjectBehavior
1316
{
@@ -18,17 +21,17 @@ public function let(HttpAsyncClient $httpAsyncClient)
1821

1922
public function it_is_initializable()
2023
{
21-
$this->shouldHaveType('Http\Client\Common\EmulatedHttpClient');
24+
$this->shouldHaveType(EmulatedHttpClient::class);
2225
}
2326

2427
public function it_is_an_http_client()
2528
{
26-
$this->shouldImplement('Http\Client\HttpClient');
29+
$this->shouldImplement(HttpClient::class);
2730
}
2831

2932
public function it_is_an_async_http_client()
3033
{
31-
$this->shouldImplement('Http\Client\HttpAsyncClient');
34+
$this->shouldImplement(HttpAsyncClient::class);
3235
}
3336

3437
public function it_emulates_a_successful_request(
@@ -54,7 +57,7 @@ public function it_emulates_a_failed_request(HttpAsyncClient $httpAsyncClient, R
5457

5558
$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);
5659

57-
$this->shouldThrow('Http\Client\Exception')->duringSendRequest($request);
60+
$this->shouldThrow(Exception::class)->duringSendRequest($request);
5861
}
5962

6063
public function it_decorates_the_underlying_client(

spec/Exception/BatchExceptionSpec.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace spec\Http\Client\Common\Exception;
44

55
use Http\Client\Common\BatchResult;
6+
use Http\Client\Exception;
67
use PhpSpec\ObjectBehavior;
8+
use Http\Client\Common\Exception\BatchException;
79

810
class BatchExceptionSpec extends ObjectBehavior
911
{
@@ -15,21 +17,21 @@ public function let()
1517

1618
public function it_is_initializable()
1719
{
18-
$this->shouldHaveType('Http\Client\Common\Exception\BatchException');
20+
$this->shouldHaveType(BatchException::class);
1921
}
2022

2123
public function it_is_a_runtime_exception()
2224
{
23-
$this->shouldHaveType('RuntimeException');
25+
$this->shouldHaveType(\RuntimeException::class);
2426
}
2527

2628
public function it_is_an_exception()
2729
{
28-
$this->shouldImplement('Http\Client\Exception');
30+
$this->shouldImplement(Exception::class);
2931
}
3032

3133
public function it_has_a_batch_result()
3234
{
33-
$this->getResult()->shouldHaveType('Http\Client\Common\BatchResult');
35+
$this->getResult()->shouldHaveType(BatchResult::class);
3436
}
3537
}

spec/FlexibleHttpClientSpec.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
1010
use PhpSpec\ObjectBehavior;
11+
use Http\Client\Common\FlexibleHttpClient;
1112

1213
class FlexibleHttpClientSpec extends ObjectBehavior
1314
{
@@ -18,24 +19,24 @@ public function let(HttpClient $httpClient)
1819

1920
public function it_is_initializable()
2021
{
21-
$this->shouldHaveType('Http\Client\Common\FlexibleHttpClient');
22+
$this->shouldHaveType(FlexibleHttpClient::class);
2223
}
2324

2425
public function it_is_an_http_client()
2526
{
26-
$this->shouldImplement('Http\Client\HttpClient');
27+
$this->shouldImplement(HttpClient::class);
2728
}
2829

2930
public function it_is_an_async_http_client()
3031
{
31-
$this->shouldImplement('Http\Client\HttpAsyncClient');
32+
$this->shouldImplement(HttpAsyncClient::class);
3233
}
3334

3435
public function it_throw_exception_if_invalid_client()
3536
{
3637
$this->beConstructedWith(null);
3738

38-
$this->shouldThrow('\LogicException')->duringInstantiation();
39+
$this->shouldThrow(\LogicException::class)->duringInstantiation();
3940
}
4041

4142
public function it_emulates_an_async_client(
@@ -53,7 +54,7 @@ public function it_emulates_an_async_client(
5354
$this->sendRequest($syncRequest)->shouldReturn($syncResponse);
5455
$promise = $this->sendAsyncRequest($asyncRequest);
5556

56-
$promise->shouldHaveType('Http\Promise\Promise');
57+
$promise->shouldHaveType(Promise::class);
5758
$promise->wait()->shouldReturn($asyncResponse);
5859
}
5960

@@ -77,8 +78,8 @@ public function it_emulates_a_client(
7778

7879
public function it_does_not_emulate_a_client($client, RequestInterface $syncRequest, RequestInterface $asyncRequest)
7980
{
80-
$client->implement('Http\Client\HttpClient');
81-
$client->implement('Http\Client\HttpAsyncClient');
81+
$client->implement(HttpClient::class);
82+
$client->implement(HttpAsyncClient::class);
8283

8384
$client->sendRequest($syncRequest)->shouldBeCalled();
8485
$client->sendRequest($asyncRequest)->shouldNotBeCalled();

spec/HttpClientPool/LeastUsedClientPoolSpec.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,31 @@
1010
use Prophecy\Argument;
1111
use Psr\Http\Message\RequestInterface;
1212
use Psr\Http\Message\ResponseInterface;
13+
use Http\Client\Common\HttpClientPool\LeastUsedClientPool;
14+
use Http\Client\Common\Exception\HttpClientNotFoundException;
15+
use Http\Client\Exception\HttpException;
1316

1417
class LeastUsedClientPoolSpec extends ObjectBehavior
1518
{
1619
public function it_is_initializable()
1720
{
18-
$this->shouldHaveType('Http\Client\Common\HttpClientPool\LeastUsedClientPool');
21+
$this->shouldHaveType(LeastUsedClientPool::class);
1922
}
2023

2124
public function it_is_an_http_client()
2225
{
23-
$this->shouldImplement('Http\Client\HttpClient');
26+
$this->shouldImplement(HttpClient::class);
2427
}
2528

2629
public function it_is_an_async_http_client()
2730
{
28-
$this->shouldImplement('Http\Client\HttpAsyncClient');
31+
$this->shouldImplement(HttpAsyncClient::class);
2932
}
3033

3134
public function it_throw_exception_with_no_client(RequestInterface $request)
3235
{
33-
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
34-
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendAsyncRequest($request);
36+
$this->shouldThrow(HttpClientNotFoundException::class)->duringSendRequest($request);
37+
$this->shouldThrow(HttpClientNotFoundException::class)->duringSendAsyncRequest($request);
3538
}
3639

3740
public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response)
@@ -54,19 +57,19 @@ public function it_sends_async_request(HttpAsyncClient $httpAsyncClient, Request
5457
public function it_throw_exception_if_no_more_enable_client(HttpClient $client, RequestInterface $request)
5558
{
5659
$this->addHttpClient($client);
57-
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');
60+
$client->sendRequest($request)->willThrow(HttpException::class);
5861

59-
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
60-
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
62+
$this->shouldThrow(HttpException::class)->duringSendRequest($request);
63+
$this->shouldThrow(HttpClientNotFoundException::class)->duringSendRequest($request);
6164
}
6265

6366
public function it_reenable_client(HttpClient $client, RequestInterface $request)
6467
{
6568
$this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0));
66-
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');
69+
$client->sendRequest($request)->willThrow(HttpException::class);
6770

68-
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
69-
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
71+
$this->shouldThrow(HttpException::class)->duringSendRequest($request);
72+
$this->shouldThrow(HttpException::class)->duringSendRequest($request);
7073
}
7174

7275
public function it_uses_the_lowest_request_client(HttpClientPoolItem $client1, HttpClientPoolItem $client2, RequestInterface $request, ResponseInterface $response)

0 commit comments

Comments
 (0)