From 2f45530b47b14f20b3d23836146c95a7b1bedf53 Mon Sep 17 00:00:00 2001 From: Meike Pabst Date: Tue, 27 Jun 2017 14:12:27 +0200 Subject: [PATCH 1/5] Add ability to supply headers per verb --- src/Json.php | 18 +++++++++--------- src/JsonInterface.php | 15 ++++++++++----- tests/JsonTest.php | 17 +++++++++++++++-- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/Json.php b/src/Json.php index 3906fd1..1cea25a 100644 --- a/src/Json.php +++ b/src/Json.php @@ -57,27 +57,27 @@ public function getEndpoint() return $this->endpoint; } - public function get($path, array $params = []) + public function get($path, array $params = [], array $headers = []) { - return $this->request('GET', $path, ['query' => $params]); + return $this->request('GET', $path, ['query' => $params, 'headers' => $headers]); } - public function post($path, array $postData = []) + public function post($path, array $postData = [], array $headers = []) { - return $this->request('POST', $path, ['json' => $postData]); + return $this->request('POST', $path, ['json' => $postData, 'headers' => $headers]); } - public function put($path, array $putData = []) + public function put($path, array $putData = [], array $headers = []) { - return $this->request('PUT', $path, ['json' => $putData]); + return $this->request('PUT', $path, ['json' => $putData, 'headers' => $headers]); } - public function delete($endpoint, array $data = []) + public function delete($endpoint, array $data = [], array $headers = []) { throw new \BadMethodCallException("Not implemented yet"); } - public function patch($endpoint, array $data = []) + public function patch($endpoint, array $data = [], array $headers = []) { throw new \BadMethodCallException("Not implemented yet"); } @@ -96,7 +96,7 @@ protected function request($method, $path, $options) { try { $uri = $this->endpoint . '/' . ltrim($path, '/'); - $options = array_merge($options, $this->additionalOptions); + $options = array_merge(array_filter($options), $this->additionalOptions); $response = $this->httpClient->request($method, $uri, $options); return $this->getJsonContentFromResponse($response); diff --git a/src/JsonInterface.php b/src/JsonInterface.php index c51be97..e1e5181 100644 --- a/src/JsonInterface.php +++ b/src/JsonInterface.php @@ -7,50 +7,55 @@ interface JsonInterface /** * @param string $path * @param array $data + * @param array $headers additional headers to send with the request, if any * * @throws Exception * * @return array */ - public function get($path, array $data = []); + public function get($path, array $data = [], array $headers = []); /** * @param string $path * @param array $data + * @param array $headers additional headers to send with the request, if any * * @throws Exception * * @return array */ - public function post($path, array $data = []); + public function post($path, array $data = [], array $headers = []); /** * @param string $path * @param array $data + * @param array $headers additional headers to send with the request, if any * * @throws Exception * * @return array */ - public function put($path, array $data = []); + public function put($path, array $data = [], array $headers = []); /** * @param string $path * @param array $data + * @param array $headers additional headers to send with the request, if any * * @throws Exception * * @return array */ - public function delete($path, array $data = []); + public function delete($path, array $data = [], array $headers = []); /** * @param string $path * @param array $data + * @param array $headers additional headers to send with the request, if any * * @throws Exception * * @return array */ - public function patch($path, array $data = []); + public function patch($path, array $data = [], array $headers = []); } diff --git a/tests/JsonTest.php b/tests/JsonTest.php index 98dcaba..7ae0eec 100644 --- a/tests/JsonTest.php +++ b/tests/JsonTest.php @@ -219,7 +219,7 @@ public function convertsGuzzleResponseExceptionsIntoFriendlyClientExceptions() ); $this->mockClient() - ->request('GET', $this->url('/foo'), ['query' => []]) + ->request('GET', $this->url('/foo'), []) ->willThrow($originalException); try { @@ -238,12 +238,25 @@ public function convertsGuzzleResponseExceptionsIntoFriendlyClientExceptions() */ public function throwsInvalidJsonResponse() { - $this->mockClient()->request('GET', $this->url('/foo'), ['query' => []]) + $this->mockClient()->request('GET', $this->url('/foo'), []) ->willReturn($this->mockResponseBody('invalid')->reveal()); $this->client->get('/foo'); } + /** + * @test + */ + public function passesOnHeaders() + { + $this->mockClient()->request('GET', $this->url('/foo'), ['headers' => ['Content-Type' => 'application/json; charset=utf-8']]) + ->willReturn($this->fooBarResponse()); + + $data = $this->client->get('/foo', [], ['Content-Type' => 'application/json; charset=utf-8']); + + $this->assertFooBarResponse($data); + } + /** * @return \Prophecy\Prophecy\ObjectProphecy */ From 97fffcaaa98ea2002bc00f6be9038cd8037555ec Mon Sep 17 00:00:00 2001 From: Meike Pabst Date: Tue, 27 Jun 2017 14:15:26 +0200 Subject: [PATCH 2/5] Fix README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5b224b3..c838eb6 100644 --- a/README.md +++ b/README.md @@ -85,8 +85,8 @@ Only tested code will be accepted. Please follow fix the style guide. ```bash # Fixes code -./vendor/bin/broc-code fix src -./vendor/bin/broc-code fix tests +./vendor/bin/bro-code fix src +./vendor/bin/bro-code fix tests ``` ## Lincense From 4d10704dd9dfd99765a5b675a13c52aa52f48204 Mon Sep 17 00:00:00 2001 From: Meike Pabst Date: Tue, 27 Jun 2017 18:08:36 +0200 Subject: [PATCH 3/5] Pass $options instead of $headers to verb methods --- src/Json.php | 18 +++++++++--------- tests/JsonTest.php | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Json.php b/src/Json.php index 1cea25a..3f65d61 100644 --- a/src/Json.php +++ b/src/Json.php @@ -57,27 +57,27 @@ public function getEndpoint() return $this->endpoint; } - public function get($path, array $params = [], array $headers = []) + public function get($path, array $params = [], array $options = []) { - return $this->request('GET', $path, ['query' => $params, 'headers' => $headers]); + return $this->request('GET', $path, array_merge($options, ['query' => $params])); } - public function post($path, array $postData = [], array $headers = []) + public function post($path, array $postData = [], array $options = []) { - return $this->request('POST', $path, ['json' => $postData, 'headers' => $headers]); + return $this->request('POST', $path, array_merge($options, ['json' => $postData])); } - public function put($path, array $putData = [], array $headers = []) + public function put($path, array $putData = [], array $options = []) { - return $this->request('PUT', $path, ['json' => $putData, 'headers' => $headers]); + return $this->request('PUT', $path, array_merge($options, ['json' => $putData])); } - public function delete($endpoint, array $data = [], array $headers = []) + public function delete($endpoint, array $data = [], array $options = []) { throw new \BadMethodCallException("Not implemented yet"); } - public function patch($endpoint, array $data = [], array $headers = []) + public function patch($endpoint, array $data = [], array $options = []) { throw new \BadMethodCallException("Not implemented yet"); } @@ -96,7 +96,7 @@ protected function request($method, $path, $options) { try { $uri = $this->endpoint . '/' . ltrim($path, '/'); - $options = array_merge(array_filter($options), $this->additionalOptions); + $options = array_merge($options, $this->additionalOptions); $response = $this->httpClient->request($method, $uri, $options); return $this->getJsonContentFromResponse($response); diff --git a/tests/JsonTest.php b/tests/JsonTest.php index 7ae0eec..8e332b1 100644 --- a/tests/JsonTest.php +++ b/tests/JsonTest.php @@ -219,7 +219,7 @@ public function convertsGuzzleResponseExceptionsIntoFriendlyClientExceptions() ); $this->mockClient() - ->request('GET', $this->url('/foo'), []) + ->request('GET', $this->url('/foo'), ["query" => []]) ->willThrow($originalException); try { @@ -238,7 +238,7 @@ public function convertsGuzzleResponseExceptionsIntoFriendlyClientExceptions() */ public function throwsInvalidJsonResponse() { - $this->mockClient()->request('GET', $this->url('/foo'), []) + $this->mockClient()->request('GET', $this->url('/foo'), ["query" => []]) ->willReturn($this->mockResponseBody('invalid')->reveal()); $this->client->get('/foo'); @@ -249,10 +249,10 @@ public function throwsInvalidJsonResponse() */ public function passesOnHeaders() { - $this->mockClient()->request('GET', $this->url('/foo'), ['headers' => ['Content-Type' => 'application/json; charset=utf-8']]) + $this->mockClient()->request('GET', $this->url('/foo'), ['headers' => ['Content-Type' => 'application/json; charset=utf-8'], "query" => []]) ->willReturn($this->fooBarResponse()); - $data = $this->client->get('/foo', [], ['Content-Type' => 'application/json; charset=utf-8']); + $data = $this->client->get('/foo', [], ['headers' => ['Content-Type' => 'application/json; charset=utf-8']]); $this->assertFooBarResponse($data); } From 667a6390f8d2f27f5423eb5f37ef70ad9d8da6fc Mon Sep 17 00:00:00 2001 From: Meike Pabst Date: Wed, 28 Jun 2017 10:17:11 +0200 Subject: [PATCH 4/5] Fixes interface --- src/JsonInterface.php | 20 ++++++++++---------- tests/JsonTest.php | 6 +++++- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/JsonInterface.php b/src/JsonInterface.php index e1e5181..fc8ba88 100644 --- a/src/JsonInterface.php +++ b/src/JsonInterface.php @@ -7,55 +7,55 @@ interface JsonInterface /** * @param string $path * @param array $data - * @param array $headers additional headers to send with the request, if any + * @param array $options additional options to send with the request, if any * * @throws Exception * * @return array */ - public function get($path, array $data = [], array $headers = []); + public function get($path, array $data = [], array $options = []); /** * @param string $path * @param array $data - * @param array $headers additional headers to send with the request, if any + * @param array $options additional options to send with the request, if any * * @throws Exception * * @return array */ - public function post($path, array $data = [], array $headers = []); + public function post($path, array $data = [], array $options = []); /** * @param string $path * @param array $data - * @param array $headers additional headers to send with the request, if any + * @param array $options additional options to send with the request, if any * * @throws Exception * * @return array */ - public function put($path, array $data = [], array $headers = []); + public function put($path, array $data = [], array $options = []); /** * @param string $path * @param array $data - * @param array $headers additional headers to send with the request, if any + * @param array $options additional options to send with the request, if any * * @throws Exception * * @return array */ - public function delete($path, array $data = [], array $headers = []); + public function delete($path, array $data = [], array $options = []); /** * @param string $path * @param array $data - * @param array $headers additional headers to send with the request, if any + * @param array $options additional options to send with the request, if any * * @throws Exception * * @return array */ - public function patch($path, array $data = [], array $headers = []); + public function patch($path, array $data = [], array $options = []); } diff --git a/tests/JsonTest.php b/tests/JsonTest.php index 8e332b1..7d02e00 100644 --- a/tests/JsonTest.php +++ b/tests/JsonTest.php @@ -249,7 +249,11 @@ public function throwsInvalidJsonResponse() */ public function passesOnHeaders() { - $this->mockClient()->request('GET', $this->url('/foo'), ['headers' => ['Content-Type' => 'application/json; charset=utf-8'], "query" => []]) + $options = [ + 'headers' => ['Content-Type' => 'application/json; charset=utf-8'], + 'query' => [] + ]; + $this->mockClient()->request('GET', $this->url('/foo'), $options) ->willReturn($this->fooBarResponse()); $data = $this->client->get('/foo', [], ['headers' => ['Content-Type' => 'application/json; charset=utf-8']]); From 503a00a75baafc6cd8b2322343680a9eabc41175 Mon Sep 17 00:00:00 2001 From: Meike Pabst Date: Wed, 28 Jun 2017 10:20:46 +0200 Subject: [PATCH 5/5] Fix indentation --- tests/JsonTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/JsonTest.php b/tests/JsonTest.php index 7d02e00..98367ed 100644 --- a/tests/JsonTest.php +++ b/tests/JsonTest.php @@ -251,7 +251,7 @@ public function passesOnHeaders() { $options = [ 'headers' => ['Content-Type' => 'application/json; charset=utf-8'], - 'query' => [] + 'query' => [] ]; $this->mockClient()->request('GET', $this->url('/foo'), $options) ->willReturn($this->fooBarResponse());