Skip to content

Commit 28ec435

Browse files
committed
Merge pull request #11 from php-http/http-client-flexible
Add flexible http client
2 parents b61b641 + 3e957e8 commit 28ec435

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

CHANGELOG.md

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

3+
## Unreleased
4+
5+
### Added
6+
7+
- Add a flexible http client providing both contract, and only emulating what's necessary
8+
9+
310
## 1.0.0 - 2016-01-27
411

512
### Changed

spec/HttpClientFlexibleSpec.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Common;
4+
5+
use Http\Client\Common\HttpAsyncClientDecorator;
6+
use Http\Client\Common\HttpClientDecorator;
7+
use Http\Client\Common\HttpClientFlexible;
8+
use Http\Client\HttpAsyncClient;
9+
use Http\Client\HttpClient;
10+
use PhpSpec\ObjectBehavior;
11+
use Prophecy\Prophet;
12+
13+
class HttpClientFlexibleSpec extends ObjectBehavior
14+
{
15+
function let(HttpClient $httpClient)
16+
{
17+
$this->beAnInstanceOf(
18+
'spec\Http\Client\Common\HttpClientFlexibleStub', [
19+
$httpClient
20+
]
21+
);
22+
}
23+
24+
function it_is_initializable()
25+
{
26+
$this->shouldHaveType('Http\Client\Common\HttpClientFlexible');
27+
}
28+
29+
function it_is_an_http_client()
30+
{
31+
$this->shouldImplement('Http\Client\HttpClient');
32+
}
33+
34+
function it_is_an_async_http_client()
35+
{
36+
$this->shouldImplement('Http\Client\HttpAsyncClient');
37+
}
38+
39+
function it_throw_exception_if_invalid_client()
40+
{
41+
$httpClient = null;
42+
$this->beConstructedWith($httpClient);
43+
44+
$this->shouldThrow('\LogicException')->duringInstantiation();
45+
}
46+
47+
function it_emulates_an_async_client(HttpClient $httpClient)
48+
{
49+
$this->beConstructedWith($httpClient);
50+
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+
}
56+
57+
function it_emulates_a_client(HttpAsyncClient $httpAsyncClient)
58+
{
59+
$this->beConstructedWith($httpAsyncClient);
60+
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');
65+
}
66+
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');
73+
74+
$this->beConstructedWith($httpClient);
75+
76+
$this->getClient()->shouldNotImplement('Http\Client\Common\EmulatedHttpClient');
77+
$this->getAsyncClient()->shouldNotImplement('Http\Client\Common\EmulatedHttpAsyncClient');
78+
}
79+
}
80+
81+
class HttpClientFlexibleStub extends HttpClientFlexible
82+
{
83+
public function getClient()
84+
{
85+
return $this->httpClient;
86+
}
87+
88+
public function getAsyncClient()
89+
{
90+
return $this->httpAsyncClient;
91+
}
92+
}

src/HttpClientFlexible.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Http\Client\Common;
4+
5+
use Http\Client\HttpAsyncClient;
6+
use Http\Client\HttpClient;
7+
8+
/**
9+
* A flexible http client, which implements both interface and will emulate
10+
* one contract, the other, or none at all depending on the injected client contract.
11+
*
12+
* @author Joel Wurtz <[email protected]>
13+
*/
14+
class HttpClientFlexible implements HttpClient, HttpAsyncClient
15+
{
16+
use HttpClientDecorator;
17+
use HttpAsyncClientDecorator;
18+
19+
/**
20+
* @param HttpClient|HttpAsyncClient $client
21+
*/
22+
public function __construct($client)
23+
{
24+
if (!($client instanceof HttpClient) && !($client instanceof HttpAsyncClient)) {
25+
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient');
26+
}
27+
28+
$this->httpClient = $client;
29+
$this->httpAsyncClient = $client;
30+
31+
if (!($this->httpClient instanceof HttpClient)) {
32+
$this->httpClient = new EmulatedHttpClient($this->httpClient);
33+
}
34+
35+
if (!($this->httpAsyncClient instanceof HttpAsyncClient)) {
36+
$this->httpAsyncClient = new EmulatedHttpAsyncClient($this->httpAsyncClient);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)