Skip to content

Commit 07dc655

Browse files
committed
Use http based promise in emulated trait and specs
1 parent 94e22b5 commit 07dc655

14 files changed

+71
-72
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.3.0 - Unreleased
44

5+
- Fix Emulated Trait to use Http based promise which respect the HttpAsyncClient interface
56
- Add HttpClientPool client to leverage load balancing and fallback mechanism [see the documentation](http://docs.php-http.org/en/latest/components/client-common.html) for more details
67

78
## 1.2.1 - 2016-07-26

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"require": {
1414
"php": ">=5.4",
15-
"php-http/httplug": "^1.0",
15+
"php-http/httplug": "^1.1",
1616
"php-http/message-factory": "^1.0",
1717
"php-http/message": "^1.2",
1818
"symfony/options-resolver": "^2.6 || ^3.0"

spec/EmulatedHttpAsyncClientSpec.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ function it_emulates_a_successful_request(
3636
) {
3737
$httpClient->sendRequest($request)->willReturn($response);
3838

39-
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise');
39+
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
4040
}
4141

4242
function it_emulates_a_failed_request(HttpClient $httpClient, RequestInterface $request)
4343
{
4444
$httpClient->sendRequest($request)->willThrow('Http\Client\Exception\TransferException');
4545

46-
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
46+
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
4747
}
4848

4949
function it_decorates_the_underlying_client(

spec/HttpClientPoolItemSpec.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use Http\Client\Exception\TransferException;
77
use Http\Client\HttpAsyncClient;
88
use Http\Client\HttpClient;
9+
use Http\Client\Promise\HttpRejectedPromise;
910
use Http\Promise\Promise;
10-
use Http\Promise\RejectedPromise;
1111
use PhpSpec\ObjectBehavior;
1212
use Prophecy\Argument;
1313
use Psr\Http\Message\RequestInterface;
@@ -60,10 +60,10 @@ public function it_disable_himself_on_send_async_request(HttpAsyncClient $httpAs
6060
{
6161
$this->beConstructedWith($httpAsyncClient);
6262

63-
$promise = new RejectedPromise(new TransferException());
63+
$promise = new HttpRejectedPromise(new TransferException());
6464
$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);
6565

66-
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
66+
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
6767
$this->isDisabled()->shouldReturn(true);
6868
$this->shouldThrow('Http\Client\Exception\RequestException')->duringSendAsyncRequest($request);
6969
}
@@ -84,12 +84,12 @@ public function it_reactivate_himself_on_send_async_request(HttpAsyncClient $htt
8484
{
8585
$this->beConstructedWith($httpAsyncClient, 0);
8686

87-
$promise = new RejectedPromise(new TransferException());
87+
$promise = new HttpRejectedPromise(new TransferException());
8888
$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);
8989

90-
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
90+
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
9191
$this->isDisabled()->shouldReturn(false);
92-
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
92+
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
9393
}
9494

9595
public function it_increments_request_count(HttpAsyncClient $httpAsyncClient, RequestInterface $request, ResponseInterface $response)

spec/Plugin/AddHostPluginSpec.php

-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
use Http\Message\StreamFactory;
66
use Http\Message\UriFactory;
7-
use Http\Promise\FulfilledPromise;
87
use Psr\Http\Message\RequestInterface;
9-
use Psr\Http\Message\ResponseInterface;
10-
use Psr\Http\Message\StreamInterface;
118
use Psr\Http\Message\UriInterface;
129
use PhpSpec\ObjectBehavior;
1310

spec/Plugin/CookiePluginSpec.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace spec\Http\Client\Common\Plugin;
44

5-
use Http\Promise\FulfilledPromise;
5+
use Http\Client\Promise\HttpFulfilledPromise;
66
use Http\Message\Cookie;
77
use Http\Message\CookieJar;
88
use Http\Promise\Promise;
@@ -141,7 +141,7 @@ function it_loads_cookie_when_cookie_is_secure(RequestInterface $request, UriInt
141141
function it_saves_cookie(RequestInterface $request, ResponseInterface $response, UriInterface $uri)
142142
{
143143
$next = function () use ($response) {
144-
return new FulfilledPromise($response->getWrappedObject());
144+
return new HttpFulfilledPromise($response->getWrappedObject());
145145
};
146146

147147
$response->hasHeader('Set-Cookie')->willReturn(true);
@@ -164,7 +164,7 @@ function it_throws_exception_on_invalid_expires_date(
164164
UriInterface $uri
165165
) {
166166
$next = function () use ($response) {
167-
return new FulfilledPromise($response->getWrappedObject());
167+
return new HttpFulfilledPromise($response->getWrappedObject());
168168
};
169169

170170
$response->hasHeader('Set-Cookie')->willReturn(true);
@@ -177,7 +177,7 @@ function it_throws_exception_on_invalid_expires_date(
177177
$uri->getPath()->willReturn('/');
178178

179179
$promise = $this->handleRequest($request, $next, function () {});
180-
$promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
180+
$promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
181181
$promise->shouldThrow('Http\Client\Exception\TransferException')->duringWait();
182182
}
183183
}

spec/Plugin/DecoderPluginSpec.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace spec\Http\Client\Common\Plugin;
44

5-
use Http\Promise\FulfilledPromise;
5+
use Http\Client\Promise\HttpFulfilledPromise;
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88
use Psr\Http\Message\StreamInterface;
@@ -31,7 +31,7 @@ function it_decodes(RequestInterface $request, ResponseInterface $response, Stre
3131
$request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request);
3232
$request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request);
3333
$next = function () use($response) {
34-
return new FulfilledPromise($response->getWrappedObject());
34+
return new HttpFulfilledPromise($response->getWrappedObject());
3535
};
3636

3737
$response->hasHeader('Transfer-Encoding')->willReturn(true);
@@ -53,7 +53,7 @@ function it_decodes_gzip(RequestInterface $request, ResponseInterface $response,
5353
$request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request);
5454
$request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request);
5555
$next = function () use($response) {
56-
return new FulfilledPromise($response->getWrappedObject());
56+
return new HttpFulfilledPromise($response->getWrappedObject());
5757
};
5858

5959
$response->hasHeader('Transfer-Encoding')->willReturn(false);
@@ -75,7 +75,7 @@ function it_decodes_deflate(RequestInterface $request, ResponseInterface $respon
7575
$request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request);
7676
$request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request);
7777
$next = function () use($response) {
78-
return new FulfilledPromise($response->getWrappedObject());
78+
return new HttpFulfilledPromise($response->getWrappedObject());
7979
};
8080

8181
$response->hasHeader('Transfer-Encoding')->willReturn(false);
@@ -97,7 +97,7 @@ function it_decodes_inflate(RequestInterface $request, ResponseInterface $respon
9797
$request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request);
9898
$request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request);
9999
$next = function () use($response) {
100-
return new FulfilledPromise($response->getWrappedObject());
100+
return new HttpFulfilledPromise($response->getWrappedObject());
101101
};
102102

103103
$response->hasHeader('Transfer-Encoding')->willReturn(false);
@@ -121,7 +121,7 @@ function it_does_not_decode_with_content_encoding(RequestInterface $request, Res
121121
$request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request);
122122
$request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldNotBeCalled();
123123
$next = function () use($response) {
124-
return new FulfilledPromise($response->getWrappedObject());
124+
return new HttpFulfilledPromise($response->getWrappedObject());
125125
};
126126

127127
$response->hasHeader('Transfer-Encoding')->willReturn(false);

spec/Plugin/ErrorPluginSpec.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace spec\Http\Client\Common\Plugin;
44

5-
use Http\Promise\FulfilledPromise;
5+
use Http\Client\Promise\HttpFulfilledPromise;
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88
use PhpSpec\ObjectBehavior;
@@ -27,12 +27,12 @@ function it_throw_client_error_exception_on_4xx_error(RequestInterface $request,
2727

2828
$next = function (RequestInterface $receivedRequest) use($request, $response) {
2929
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
30-
return new FulfilledPromise($response->getWrappedObject());
30+
return new HttpFulfilledPromise($response->getWrappedObject());
3131
}
3232
};
3333

3434
$promise = $this->handleRequest($request, $next, function () {});
35-
$promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
35+
$promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
3636
$promise->shouldThrow('Http\Client\Common\Exception\ClientErrorException')->duringWait();
3737
}
3838

@@ -43,12 +43,12 @@ function it_throw_server_error_exception_on_5xx_error(RequestInterface $request,
4343

4444
$next = function (RequestInterface $receivedRequest) use($request, $response) {
4545
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
46-
return new FulfilledPromise($response->getWrappedObject());
46+
return new HttpFulfilledPromise($response->getWrappedObject());
4747
}
4848
};
4949

5050
$promise = $this->handleRequest($request, $next, function () {});
51-
$promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
51+
$promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
5252
$promise->shouldThrow('Http\Client\Common\Exception\ServerErrorException')->duringWait();
5353
}
5454

@@ -58,10 +58,10 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo
5858

5959
$next = function (RequestInterface $receivedRequest) use($request, $response) {
6060
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
61-
return new FulfilledPromise($response->getWrappedObject());
61+
return new HttpFulfilledPromise($response->getWrappedObject());
6262
}
6363
};
6464

65-
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise');
65+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
6666
}
6767
}

spec/Plugin/HistoryPluginSpec.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use Http\Client\Exception\TransferException;
66
use Http\Client\Common\Plugin\Journal;
7-
use Http\Promise\FulfilledPromise;
8-
use Http\Promise\RejectedPromise;
7+
use Http\Client\Promise\HttpFulfilledPromise;
8+
use Http\Client\Promise\HttpRejectedPromise;
99
use Psr\Http\Message\RequestInterface;
1010
use Psr\Http\Message\ResponseInterface;
1111
use PhpSpec\ObjectBehavior;
@@ -32,7 +32,7 @@ function it_records_success(Journal $journal, RequestInterface $request, Respons
3232
{
3333
$next = function (RequestInterface $receivedRequest) use($request, $response) {
3434
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
35-
return new FulfilledPromise($response->getWrappedObject());
35+
return new HttpFulfilledPromise($response->getWrappedObject());
3636
}
3737
};
3838

@@ -46,7 +46,7 @@ function it_records_failure(Journal $journal, RequestInterface $request)
4646
$exception = new TransferException();
4747
$next = function (RequestInterface $receivedRequest) use($request, $exception) {
4848
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
49-
return new RejectedPromise($exception);
49+
return new HttpRejectedPromise($exception);
5050
}
5151
};
5252

0 commit comments

Comments
 (0)