From d71e015167fbc203efd3fa4fe066d881e49c5365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sat, 26 Dec 2015 02:32:44 +0100 Subject: [PATCH] Add emulated clients --- CHANGELOG.md | 7 ++ spec/EmulatedHttpAsyncClientSpec.php | 58 +++++++++ ...torSpec.php => EmulatedHttpClientSpec.php} | 42 ++++--- spec/HttpAsyncClientDecoratorSpec.php | 47 ------- spec/HttpAsyncClientEmulatorSpec.php | 52 -------- spec/HttpClientDecoratorSpec.php | 47 ------- spec/HttpMethodsClientSpec.php | 116 ------------------ src/EmulatedHttpAsyncClient.php | 27 ++++ src/EmulatedHttpClient.php | 27 ++++ 9 files changed, 144 insertions(+), 279 deletions(-) create mode 100644 spec/EmulatedHttpAsyncClientSpec.php rename spec/{HttpClientEmulatorSpec.php => EmulatedHttpClientSpec.php} (57%) delete mode 100644 spec/HttpAsyncClientDecoratorSpec.php delete mode 100644 spec/HttpAsyncClientEmulatorSpec.php delete mode 100644 spec/HttpClientDecoratorSpec.php delete mode 100644 spec/HttpMethodsClientSpec.php create mode 100644 src/EmulatedHttpAsyncClient.php create mode 100644 src/EmulatedHttpClient.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b7b8931..c724b15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Change Log +## Unreleased + +### Added + +- Emulated clients + + ## 0.1.0 - 2015-12-25 ### Added diff --git a/spec/EmulatedHttpAsyncClientSpec.php b/spec/EmulatedHttpAsyncClientSpec.php new file mode 100644 index 0000000..1ea8763 --- /dev/null +++ b/spec/EmulatedHttpAsyncClientSpec.php @@ -0,0 +1,58 @@ +beConstructedWith($httpClient); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Client\Common\EmulatedHttpAsyncClient'); + } + + 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_emulates_a_successful_request( + HttpClient $httpClient, + RequestInterface $request, + ResponseInterface $response + ) { + $httpClient->sendRequest($request)->willReturn($response); + + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); + } + + function it_emulates_a_failed_request(HttpClient $httpClient, RequestInterface $request) + { + $httpClient->sendRequest($request)->willThrow('Http\Client\Exception\TransferException'); + + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + } + + function it_decorates_the_underlying_client( + HttpClient $httpClient, + RequestInterface $request, + ResponseInterface $response + ) { + $httpClient->sendRequest($request)->willReturn($response); + + $this->sendRequest($request)->shouldReturn($response); + } +} diff --git a/spec/HttpClientEmulatorSpec.php b/spec/EmulatedHttpClientSpec.php similarity index 57% rename from spec/HttpClientEmulatorSpec.php rename to spec/EmulatedHttpClientSpec.php index 7a56a3c..976f772 100644 --- a/spec/HttpClientEmulatorSpec.php +++ b/spec/EmulatedHttpClientSpec.php @@ -5,27 +5,39 @@ use Http\Client\Exception\TransferException; use Http\Client\HttpClient; use Http\Client\HttpAsyncClient; -use Http\Client\Common\HttpClientEmulator; -use Http\Client\Common\HttpAsyncClientDecorator; use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; -class HttpClientEmulatorSpec extends ObjectBehavior +class EmulatedHttpClientSpec extends ObjectBehavior { function let(HttpAsyncClient $httpAsyncClient) { - $this->beAnInstanceOf('spec\Http\Client\Common\HttpClientEmulatorStub', [$httpAsyncClient]); + $this->beConstructedWith($httpAsyncClient); } function it_is_initializable() { - $this->shouldHaveType('spec\Http\Client\Common\HttpClientEmulatorStub'); + $this->shouldHaveType('Http\Client\Common\EmulatedHttpClient'); } - function it_emulates_a_successful_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise, ResponseInterface $response) + 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_emulates_a_successful_request( + HttpAsyncClient $httpAsyncClient, + RequestInterface $request, + Promise $promise, + ResponseInterface $response + ) { $promise->wait()->shouldBeCalled(); $promise->getState()->willReturn(Promise::FULFILLED); $promise->wait()->willReturn($response); @@ -45,18 +57,14 @@ function it_emulates_a_failed_request(HttpAsyncClient $httpAsyncClient, RequestI $this->shouldThrow('Http\Client\Exception')->duringSendRequest($request); } -} -class HttpClientEmulatorStub implements HttpAsyncClient, HttpClient -{ - use HttpAsyncClientDecorator; - use HttpClientEmulator; + function it_decorates_the_underlying_client( + HttpAsyncClient $httpAsyncClient, + RequestInterface $request, + Promise $promise + ) { + $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); - /** - * @param HttpAsyncClient $httpAsyncClient - */ - public function __construct(HttpAsyncClient $httpAsyncClient) - { - $this->httpAsyncClient = $httpAsyncClient; + $this->sendAsyncRequest($request)->shouldReturn($promise); } } diff --git a/spec/HttpAsyncClientDecoratorSpec.php b/spec/HttpAsyncClientDecoratorSpec.php deleted file mode 100644 index 01c4d79..0000000 --- a/spec/HttpAsyncClientDecoratorSpec.php +++ /dev/null @@ -1,47 +0,0 @@ -beAnInstanceOf('spec\Http\Client\Common\HttpAsyncClientDecoratorStub', [$httpAsyncClient]); - } - - function it_is_initializable() - { - $this->shouldHaveType('spec\Http\Client\Common\HttpAsyncClientDecoratorStub'); - } - - function it_is_an_http_async_client() - { - $this->shouldImplement('Http\Client\HttpAsyncClient'); - } - - function it_decorates_the_underlying_client(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise) - { - $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); - - $this->sendAsyncRequest($request)->shouldReturn($promise); - } -} - -class HttpAsyncClientDecoratorStub implements HttpAsyncClient -{ - use HttpAsyncClientDecorator; - - /** - * @param HttpAsyncClient $httpAsyncClient - */ - public function __construct(HttpAsyncClient $httpAsyncClient) - { - $this->httpAsyncClient = $httpAsyncClient; - } -} diff --git a/spec/HttpAsyncClientEmulatorSpec.php b/spec/HttpAsyncClientEmulatorSpec.php deleted file mode 100644 index 15eacc5..0000000 --- a/spec/HttpAsyncClientEmulatorSpec.php +++ /dev/null @@ -1,52 +0,0 @@ -beAnInstanceOf('spec\Http\Client\Common\HttpAsyncClientEmulatorStub', [$httpClient]); - } - - function it_is_initializable() - { - $this->shouldHaveType('spec\Http\Client\Common\HttpAsyncClientEmulatorStub'); - } - - function it_emulates_a_successful_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response) - { - $httpClient->sendRequest($request)->willReturn($response); - - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); - } - - function it_emulates_a_failed_request(HttpClient $httpClient, RequestInterface $request) - { - $httpClient->sendRequest($request)->willThrow('Http\Client\Exception\TransferException'); - - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); - } -} - -class HttpAsyncClientEmulatorStub implements HttpClient, HttpAsyncClient -{ - use HttpClientDecorator; - use HttpAsyncClientEmulator; - - /** - * @param HttpClient $httpClient - */ - public function __construct(HttpClient $httpClient) - { - $this->httpClient = $httpClient; - } -} diff --git a/spec/HttpClientDecoratorSpec.php b/spec/HttpClientDecoratorSpec.php deleted file mode 100644 index 2e24b7a..0000000 --- a/spec/HttpClientDecoratorSpec.php +++ /dev/null @@ -1,47 +0,0 @@ -beAnInstanceOf('spec\Http\Client\Common\HttpClientDecoratorStub', [$httpClient]); - } - - function it_is_initializable() - { - $this->shouldHaveType('spec\Http\Client\Common\HttpClientDecoratorStub'); - } - - function it_is_an_http_client() - { - $this->shouldImplement('Http\Client\HttpClient'); - } - - function it_decorates_the_underlying_client(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response) - { - $httpClient->sendRequest($request)->willReturn($response); - - $this->sendRequest($request)->shouldReturn($response); - } -} - -class HttpClientDecoratorStub implements HttpClient -{ - use HttpClientDecorator; - - /** - * @param HttpClient $httpClient - */ - public function __construct(HttpClient $httpClient) - { - $this->httpClient = $httpClient; - } -} diff --git a/spec/HttpMethodsClientSpec.php b/spec/HttpMethodsClientSpec.php deleted file mode 100644 index 07c0b47..0000000 --- a/spec/HttpMethodsClientSpec.php +++ /dev/null @@ -1,116 +0,0 @@ -beAnInstanceOf( - 'spec\Http\Client\Common\HttpMethodsClientStub', [ - $client, - $messageFactory - ] - ); - } - - function it_sends_a_get_request() - { - $data = HttpMethodsClientStub::$requestData; - - $this->get($data['uri'], $data['headers'])->shouldReturn(true); - } - - function it_sends_a_head_request() - { - $data = HttpMethodsClientStub::$requestData; - - $this->head($data['uri'], $data['headers'])->shouldReturn(true); - } - - function it_sends_a_trace_request() - { - $data = HttpMethodsClientStub::$requestData; - - $this->trace($data['uri'], $data['headers'])->shouldReturn(true); - } - - function it_sends_a_post_request() - { - $data = HttpMethodsClientStub::$requestData; - - $this->post($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); - } - - function it_sends_a_put_request() - { - $data = HttpMethodsClientStub::$requestData; - - $this->put($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); - } - - function it_sends_a_patch_request() - { - $data = HttpMethodsClientStub::$requestData; - - $this->patch($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); - } - - function it_sends_a_delete_request() - { - $data = HttpMethodsClientStub::$requestData; - - $this->delete($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); - } - - function it_sends_a_options_request() - { - $data = HttpMethodsClientStub::$requestData; - - $this->options($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); - } - - function it_sends_request_with_underlying_client(HttpClient $client, MessageFactory $messageFactory, RequestInterface $request, ResponseInterface $response) - { - $client->sendRequest($request)->shouldBeCalled()->willReturn($response); - - $this->beConstructedWith($client, $messageFactory); - $this->sendRequest($request)->shouldReturn($response); - } -} - -class HttpMethodsClientStub extends HttpMethodsClient -{ - public static $requestData = [ - 'uri' => '/uri', - 'headers' => [ - 'Content-Type' => 'text/plain', - ], - 'body' => 'body' - ]; - - /** - * {@inheritdoc} - */ - public function send($method, $uri, array $headers = [], $body = null) - { - if (in_array($method, ['GET', 'HEAD', 'TRACE'])) { - return $uri === self::$requestData['uri'] && - $headers === self::$requestData['headers'] && - is_null($body); - } - - return in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']) && - $uri === self::$requestData['uri'] && - $headers === self::$requestData['headers'] && - $body === self::$requestData['body']; - } -} diff --git a/src/EmulatedHttpAsyncClient.php b/src/EmulatedHttpAsyncClient.php new file mode 100644 index 0000000..1b16316 --- /dev/null +++ b/src/EmulatedHttpAsyncClient.php @@ -0,0 +1,27 @@ + + */ +class EmulatedHttpAsyncClient implements HttpClient, HttpAsyncClient +{ + use HttpAsyncClientEmulator; + use HttpClientDecorator; + + /** + * @param HttpClient $httpClient + */ + public function __construct(HttpClient $httpClient) + { + $this->httpClient = $httpClient; + } +} diff --git a/src/EmulatedHttpClient.php b/src/EmulatedHttpClient.php new file mode 100644 index 0000000..01046c8 --- /dev/null +++ b/src/EmulatedHttpClient.php @@ -0,0 +1,27 @@ + + */ +class EmulatedHttpClient implements HttpClient, HttpAsyncClient +{ + use HttpAsyncClientDecorator; + use HttpClientEmulator; + + /** + * @param HttpAsyncClient $httpAsyncClient + */ + public function __construct(HttpAsyncClient $httpAsyncClient) + { + $this->httpAsyncClient = $httpAsyncClient; + } +}