From 3c298ed1bd92c0a01c3a21b31562ba6d59edb175 Mon Sep 17 00:00:00 2001 From: andyvanee <1andyvanee@gmail.com> Date: Wed, 9 May 2018 13:03:38 -0600 Subject: [PATCH 1/2] Responses are ResultSet objects which extend ArrayObject ResultSet objects provide access to the original request as well as the total and totalPages headers supplied by the REST API. --- specs/Endpoint/abstract-wp-endpoint.spec.php | 45 ++++++++++++-- src/Endpoint/AbstractWpEndpoint.php | 12 ++-- src/Endpoint/ResultSet.php | 62 ++++++++++++++++++++ 3 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 src/Endpoint/ResultSet.php diff --git a/specs/Endpoint/abstract-wp-endpoint.spec.php b/specs/Endpoint/abstract-wp-endpoint.spec.php index 11c82c7..aea8d09 100644 --- a/specs/Endpoint/abstract-wp-endpoint.spec.php +++ b/specs/Endpoint/abstract-wp-endpoint.spec.php @@ -17,7 +17,7 @@ $endpoint = new FakeEndpoint($client->reveal()); $data = $endpoint->get(55); - expect($data)->to->equal(['foo' => 'bar']); + expect($data['foo'])->to->equal('bar'); }); it('should make a GET request without any ID', function () { @@ -31,7 +31,7 @@ $endpoint = new FakeEndpoint($client->reveal()); $data = $endpoint->get(); - expect($data)->to->equal(['foo' => 'bar']); + expect($data['foo'])->to->equal('bar'); }); it('should make a GET request with parameters', function () { @@ -45,9 +45,46 @@ $endpoint = new FakeEndpoint($client->reveal()); $data = $endpoint->get(null, ['bar'=>'baz']); - expect($data)->to->equal(['foo' => 'bar']); + expect($data['foo'])->to->equal('bar'); }); + it('should expose WP-Total headers', function () { + $client = $this->getProphet()->prophesize(WpClient::class); + + $request = new Request('GET', '/foo/55'); + + $headers = [ + 'Content-Type' => 'application/json', + 'X-WP-Total' => 1, + 'X-WP-TotalPages' => 2, + ]; + + $response = new \GuzzleHttp\Psr7\Response(200, $headers, '{"foo": "bar"}'); + + $client->send($request)->willReturn($response)->shouldBeCalled(); + + $endpoint = new FakeEndpoint($client->reveal()); + + $data = $endpoint->get(55); + + expect($data->total)->to->equal(1); + expect($data->totalPages)->to->equal(2); + }); + + it('should include original request', function () { + $client = $this->getProphet()->prophesize(WpClient::class); + + $request = new Request('GET', '/foo/55'); + $response = new \GuzzleHttp\Psr7\Response(200, [], '{"foo": "bar"}'); + + $client->send($request)->willReturn($response)->shouldBeCalled(); + + $endpoint = new FakeEndpoint($client->reveal()); + + $data = $endpoint->get(55); + + expect($data->request->getUri()->getPath())->to->equal('/foo/55'); + }); }); describe('save()', function () { @@ -59,7 +96,7 @@ $endpoint = new FakeEndpoint($client->reveal()); $data = $endpoint->save(['foo' => 'bar']); - expect($data)->to->equal(['foo' => 'bar']); + expect($data['foo'])->to->equal('bar'); }); }); diff --git a/src/Endpoint/AbstractWpEndpoint.php b/src/Endpoint/AbstractWpEndpoint.php index d1664b6..0b7c327 100644 --- a/src/Endpoint/AbstractWpEndpoint.php +++ b/src/Endpoint/AbstractWpEndpoint.php @@ -42,10 +42,10 @@ public function get($id = null, array $params = null) $request = new Request('GET', $uri); $response = $this->client->send($request); + $results = new ResultSet($request, $response); - if ($response->hasHeader('Content-Type') - && substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') { - return json_decode($response->getBody()->getContents(), true); + if (count($results)) { + return $results; } throw new RuntimeException('Unexpected response'); @@ -66,10 +66,10 @@ public function save(array $data) $request = new Request('POST', $url, ['Content-Type' => 'application/json'], json_encode($data)); $response = $this->client->send($request); + $results = new ResultSet($request, $response); - if ($response->hasHeader('Content-Type') - && substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') { - return json_decode($response->getBody()->getContents(), true); + if (count($results)) { + return $results; } throw new RuntimeException('Unexpected response'); diff --git a/src/Endpoint/ResultSet.php b/src/Endpoint/ResultSet.php new file mode 100644 index 0000000..d2c8173 --- /dev/null +++ b/src/Endpoint/ResultSet.php @@ -0,0 +1,62 @@ +request = $request; + + if ($this->validateResponse($response)) { + parent::__construct(json_decode($response->getBody()->getContents(), true)); + $this->setHeaders($response); + } + } + + private function setHeaders(ResponseInterface &$response) + { + if ($response->hasHeader('X-WP-Total')) { + $this->total = (int) $response->getHeader('X-WP-Total')[0]; + } + + if ($response->hasHeader('X-WP-TotalPages')) { + $this->totalPages = (int) $response->getHeader('X-WP-TotalPages')[0]; + } + } + + private function validateResponse(ResponseInterface &$response) + { + return ( + $response->hasHeader('Content-Type') && + substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json'); + } +} From 097c0f90caf68e67a8ae2ebf1fb08aa4bb6d98dd Mon Sep 17 00:00:00 2001 From: andyvanee <1andyvanee@gmail.com> Date: Wed, 9 May 2018 14:22:55 -0600 Subject: [PATCH 2/2] Fix failing test --- specs/Endpoint/abstract-wp-endpoint.spec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/Endpoint/abstract-wp-endpoint.spec.php b/specs/Endpoint/abstract-wp-endpoint.spec.php index aea8d09..a1c7d5e 100644 --- a/specs/Endpoint/abstract-wp-endpoint.spec.php +++ b/specs/Endpoint/abstract-wp-endpoint.spec.php @@ -75,7 +75,7 @@ $client = $this->getProphet()->prophesize(WpClient::class); $request = new Request('GET', '/foo/55'); - $response = new \GuzzleHttp\Psr7\Response(200, [], '{"foo": "bar"}'); + $response = new \GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], '{"foo": "bar"}'); $client->send($request)->willReturn($response)->shouldBeCalled();