Skip to content

Commit c2fdc8c

Browse files
committed
Improve flexible http client tests
1 parent b97d455 commit c2fdc8c

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

spec/FlexibleHttpClientSpec.php

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
use Http\Client\Common\FlexibleHttpClient;
88
use Http\Client\HttpAsyncClient;
99
use Http\Client\HttpClient;
10+
use Http\Promise\Promise;
11+
use Psr\Http\Message\RequestInterface;
12+
use Psr\Http\Message\ResponseInterface;
1013
use PhpSpec\ObjectBehavior;
1114
use Prophecy\Prophet;
1215

1316
class FlexibleHttpClientSpec extends ObjectBehavior
1417
{
1518
function let(HttpClient $httpClient)
1619
{
17-
$this->beAnInstanceOf(
18-
'spec\Http\Client\Common\FlexibleHttpClientStub', [
19-
$httpClient
20-
]
21-
);
20+
$this->beConstructedWith($httpClient);
2221
}
2322

2423
function it_is_initializable()
@@ -38,55 +37,58 @@ function it_is_an_async_http_client()
3837

3938
function it_throw_exception_if_invalid_client()
4039
{
41-
$httpClient = null;
42-
$this->beConstructedWith($httpClient);
40+
$this->beConstructedWith(null);
4341

4442
$this->shouldThrow('\LogicException')->duringInstantiation();
4543
}
4644

47-
function it_emulates_an_async_client(HttpClient $httpClient)
48-
{
45+
function it_emulates_an_async_client(
46+
HttpClient $httpClient,
47+
RequestInterface $syncRequest,
48+
ResponseInterface $syncResponse,
49+
RequestInterface $asyncRequest,
50+
ResponseInterface $asyncResponse
51+
) {
4952
$this->beConstructedWith($httpClient);
5053

51-
$this->getClient()->shouldImplement('Http\Client\HttpClient');
52-
$this->getAsyncClient()->shouldImplement('Http\Client\HttpAsyncClient');
53-
$this->getClient()->shouldNotImplement('Http\Client\Common\EmulatedHttpClient');
54-
$this->getAsyncClient()->shouldImplement('Http\Client\Common\EmulatedHttpAsyncClient');
55-
}
54+
$httpClient->sendRequest($syncRequest)->willReturn($syncResponse);
55+
$httpClient->sendRequest($asyncRequest)->willReturn($asyncResponse);
5656

57-
function it_emulates_a_client(HttpAsyncClient $httpAsyncClient)
58-
{
59-
$this->beConstructedWith($httpAsyncClient);
57+
$this->sendRequest($syncRequest)->shouldReturn($syncResponse);
58+
$promise = $this->sendAsyncRequest($asyncRequest);
6059

61-
$this->getClient()->shouldImplement('Http\Client\HttpClient');
62-
$this->getAsyncClient()->shouldImplement('Http\Client\HttpAsyncClient');
63-
$this->getClient()->shouldImplement('Http\Client\Common\EmulatedHttpClient');
64-
$this->getAsyncClient()->shouldNotImplement('Http\Client\EmulatedHttpAsyncClient');
60+
$promise->shouldHaveType('Http\Promise\Promise');
61+
$promise->wait()->shouldReturn($asyncResponse);
6562
}
6663

67-
function it_does_not_emulates_a_client()
68-
{
69-
$prophet = new Prophet();
70-
$httpClient = $prophet->prophesize();
71-
$httpClient->willImplement('Http\Client\HttpClient');
72-
$httpClient->willImplement('Http\Client\HttpAsyncClient');
64+
function it_emulates_a_client(
65+
HttpAsyncClient $httpAsyncClient,
66+
RequestInterface $asyncRequest,
67+
Promise $promise,
68+
RequestInterface $syncRequest,
69+
Promise $syncPromise,
70+
ResponseInterface $syncResponse
71+
) {
72+
$this->beConstructedWith($httpAsyncClient);
7373

74-
$this->beConstructedWith($httpClient);
74+
$httpAsyncClient->sendAsyncRequest($asyncRequest)->willReturn($promise);
75+
$httpAsyncClient->sendAsyncRequest($syncRequest)->willReturn($syncPromise);
76+
$syncPromise->wait()->willReturn($syncResponse);
7577

76-
$this->getClient()->shouldNotImplement('Http\Client\Common\EmulatedHttpClient');
77-
$this->getAsyncClient()->shouldNotImplement('Http\Client\Common\EmulatedHttpAsyncClient');
78+
$this->sendAsyncRequest($asyncRequest)->shouldReturn($promise);
79+
$this->sendRequest($syncRequest)->shouldReturn($syncResponse);
7880
}
79-
}
8081

81-
class FlexibleHttpClientStub extends FlexibleHttpClient
82-
{
83-
public function getClient()
82+
function it_does_not_emulate_a_client(FlexibleHttpClient $client, RequestInterface $syncRequest, RequestInterface $asyncRequest)
8483
{
85-
return $this->httpClient;
86-
}
84+
$client->sendRequest($syncRequest)->shouldBeCalled();
85+
$client->sendRequest($asyncRequest)->shouldNotBeCalled();
86+
$client->sendAsyncRequest($asyncRequest)->shouldBeCalled();
87+
$client->sendAsyncRequest($syncRequest)->shouldNotBeCalled();
8788

88-
public function getAsyncClient()
89-
{
90-
return $this->httpAsyncClient;
89+
$this->beConstructedWith($client);
90+
91+
$this->sendRequest($syncRequest);
92+
$this->sendAsyncRequest($asyncRequest);
9193
}
9294
}

0 commit comments

Comments
 (0)