From 3e957e8c7bcda97e76906c8027a71edc6f67279b Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Sun, 31 Jan 2016 20:28:14 +0100 Subject: [PATCH] Add flexible http client --- CHANGELOG.md | 7 +++ spec/HttpClientFlexibleSpec.php | 92 +++++++++++++++++++++++++++++++++ src/HttpClientFlexible.php | 39 ++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 spec/HttpClientFlexibleSpec.php create mode 100644 src/HttpClientFlexible.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7673954..a213e69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log +## Unreleased + +### Added + +- Add a flexible http client providing both contract, and only emulating what's necessary + + ## 1.0.0 - 2016-01-27 ### Changed diff --git a/spec/HttpClientFlexibleSpec.php b/spec/HttpClientFlexibleSpec.php new file mode 100644 index 0000000..e0489b7 --- /dev/null +++ b/spec/HttpClientFlexibleSpec.php @@ -0,0 +1,92 @@ +beAnInstanceOf( + 'spec\Http\Client\Common\HttpClientFlexibleStub', [ + $httpClient + ] + ); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Client\Common\HttpClientFlexible'); + } + + function it_is_an_http_client() + { + $this->shouldImplement('Http\Client\HttpClient'); + } + + function it_is_an_async_http_client() + { + $this->shouldImplement('Http\Client\HttpAsyncClient'); + } + + function it_throw_exception_if_invalid_client() + { + $httpClient = null; + $this->beConstructedWith($httpClient); + + $this->shouldThrow('\LogicException')->duringInstantiation(); + } + + function it_emulates_an_async_client(HttpClient $httpClient) + { + $this->beConstructedWith($httpClient); + + $this->getClient()->shouldImplement('Http\Client\HttpClient'); + $this->getAsyncClient()->shouldImplement('Http\Client\HttpAsyncClient'); + $this->getClient()->shouldNotImplement('Http\Client\Common\EmulatedHttpClient'); + $this->getAsyncClient()->shouldImplement('Http\Client\Common\EmulatedHttpAsyncClient'); + } + + function it_emulates_a_client(HttpAsyncClient $httpAsyncClient) + { + $this->beConstructedWith($httpAsyncClient); + + $this->getClient()->shouldImplement('Http\Client\HttpClient'); + $this->getAsyncClient()->shouldImplement('Http\Client\HttpAsyncClient'); + $this->getClient()->shouldImplement('Http\Client\Common\EmulatedHttpClient'); + $this->getAsyncClient()->shouldNotImplement('Http\Client\EmulatedHttpAsyncClient'); + } + + function it_does_not_emulates_a_client() + { + $prophet = new Prophet(); + $httpClient = $prophet->prophesize(); + $httpClient->willImplement('Http\Client\HttpClient'); + $httpClient->willImplement('Http\Client\HttpAsyncClient'); + + $this->beConstructedWith($httpClient); + + $this->getClient()->shouldNotImplement('Http\Client\Common\EmulatedHttpClient'); + $this->getAsyncClient()->shouldNotImplement('Http\Client\Common\EmulatedHttpAsyncClient'); + } +} + +class HttpClientFlexibleStub extends HttpClientFlexible +{ + public function getClient() + { + return $this->httpClient; + } + + public function getAsyncClient() + { + return $this->httpAsyncClient; + } +} diff --git a/src/HttpClientFlexible.php b/src/HttpClientFlexible.php new file mode 100644 index 0000000..e757610 --- /dev/null +++ b/src/HttpClientFlexible.php @@ -0,0 +1,39 @@ + + */ +class HttpClientFlexible implements HttpClient, HttpAsyncClient +{ + use HttpClientDecorator; + use HttpAsyncClientDecorator; + + /** + * @param HttpClient|HttpAsyncClient $client + */ + public function __construct($client) + { + if (!($client instanceof HttpClient) && !($client instanceof HttpAsyncClient)) { + throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient'); + } + + $this->httpClient = $client; + $this->httpAsyncClient = $client; + + if (!($this->httpClient instanceof HttpClient)) { + $this->httpClient = new EmulatedHttpClient($this->httpClient); + } + + if (!($this->httpAsyncClient instanceof HttpAsyncClient)) { + $this->httpAsyncClient = new EmulatedHttpAsyncClient($this->httpAsyncClient); + } + } +}