Skip to content

Commit d71e015

Browse files
committed
Add emulated clients
1 parent 04fa8ec commit d71e015

9 files changed

+144
-279
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Change Log
22

33

4+
## Unreleased
5+
6+
### Added
7+
8+
- Emulated clients
9+
10+
411
## 0.1.0 - 2015-12-25
512

613
### Added

spec/EmulatedHttpAsyncClientSpec.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Common;
4+
5+
use Http\Client\HttpClient;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
use PhpSpec\ObjectBehavior;
9+
10+
class EmulatedHttpAsyncClientSpec extends ObjectBehavior
11+
{
12+
function let(HttpClient $httpClient)
13+
{
14+
$this->beConstructedWith($httpClient);
15+
}
16+
17+
function it_is_initializable()
18+
{
19+
$this->shouldHaveType('Http\Client\Common\EmulatedHttpAsyncClient');
20+
}
21+
22+
function it_is_an_http_client()
23+
{
24+
$this->shouldImplement('Http\Client\HttpClient');
25+
}
26+
27+
function it_is_an_async_http_client()
28+
{
29+
$this->shouldImplement('Http\Client\HttpAsyncClient');
30+
}
31+
32+
function it_emulates_a_successful_request(
33+
HttpClient $httpClient,
34+
RequestInterface $request,
35+
ResponseInterface $response
36+
) {
37+
$httpClient->sendRequest($request)->willReturn($response);
38+
39+
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise');
40+
}
41+
42+
function it_emulates_a_failed_request(HttpClient $httpClient, RequestInterface $request)
43+
{
44+
$httpClient->sendRequest($request)->willThrow('Http\Client\Exception\TransferException');
45+
46+
$this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise');
47+
}
48+
49+
function it_decorates_the_underlying_client(
50+
HttpClient $httpClient,
51+
RequestInterface $request,
52+
ResponseInterface $response
53+
) {
54+
$httpClient->sendRequest($request)->willReturn($response);
55+
56+
$this->sendRequest($request)->shouldReturn($response);
57+
}
58+
}

spec/HttpClientEmulatorSpec.php renamed to spec/EmulatedHttpClientSpec.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,39 @@
55
use Http\Client\Exception\TransferException;
66
use Http\Client\HttpClient;
77
use Http\Client\HttpAsyncClient;
8-
use Http\Client\Common\HttpClientEmulator;
9-
use Http\Client\Common\HttpAsyncClientDecorator;
108
use Http\Promise\Promise;
119
use Psr\Http\Message\RequestInterface;
1210
use Psr\Http\Message\ResponseInterface;
1311
use PhpSpec\ObjectBehavior;
1412

15-
class HttpClientEmulatorSpec extends ObjectBehavior
13+
class EmulatedHttpClientSpec extends ObjectBehavior
1614
{
1715
function let(HttpAsyncClient $httpAsyncClient)
1816
{
19-
$this->beAnInstanceOf('spec\Http\Client\Common\HttpClientEmulatorStub', [$httpAsyncClient]);
17+
$this->beConstructedWith($httpAsyncClient);
2018
}
2119

2220
function it_is_initializable()
2321
{
24-
$this->shouldHaveType('spec\Http\Client\Common\HttpClientEmulatorStub');
22+
$this->shouldHaveType('Http\Client\Common\EmulatedHttpClient');
2523
}
2624

27-
function it_emulates_a_successful_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise, ResponseInterface $response)
25+
function it_is_an_http_client()
2826
{
27+
$this->shouldImplement('Http\Client\HttpClient');
28+
}
29+
30+
function it_is_an_async_http_client()
31+
{
32+
$this->shouldImplement('Http\Client\HttpAsyncClient');
33+
}
34+
35+
function it_emulates_a_successful_request(
36+
HttpAsyncClient $httpAsyncClient,
37+
RequestInterface $request,
38+
Promise $promise,
39+
ResponseInterface $response
40+
) {
2941
$promise->wait()->shouldBeCalled();
3042
$promise->getState()->willReturn(Promise::FULFILLED);
3143
$promise->wait()->willReturn($response);
@@ -45,18 +57,14 @@ function it_emulates_a_failed_request(HttpAsyncClient $httpAsyncClient, RequestI
4557

4658
$this->shouldThrow('Http\Client\Exception')->duringSendRequest($request);
4759
}
48-
}
4960

50-
class HttpClientEmulatorStub implements HttpAsyncClient, HttpClient
51-
{
52-
use HttpAsyncClientDecorator;
53-
use HttpClientEmulator;
61+
function it_decorates_the_underlying_client(
62+
HttpAsyncClient $httpAsyncClient,
63+
RequestInterface $request,
64+
Promise $promise
65+
) {
66+
$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);
5467

55-
/**
56-
* @param HttpAsyncClient $httpAsyncClient
57-
*/
58-
public function __construct(HttpAsyncClient $httpAsyncClient)
59-
{
60-
$this->httpAsyncClient = $httpAsyncClient;
68+
$this->sendAsyncRequest($request)->shouldReturn($promise);
6169
}
6270
}

spec/HttpAsyncClientDecoratorSpec.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

spec/HttpAsyncClientEmulatorSpec.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

spec/HttpClientDecoratorSpec.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)