Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions specs/Endpoint/abstract-wp-endpoint.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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, ['Content-Type' => 'application/json'], '{"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 () {
Expand All @@ -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');
});
});

Expand Down
12 changes: 6 additions & 6 deletions src/Endpoint/AbstractWpEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
62 changes: 62 additions & 0 deletions src/Endpoint/ResultSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Vnn\WpApiClient\Endpoint;

use ArrayObject;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\RequestInterface;

/**
* Class ResultSet
* @package Vnn\WpApiClient\Endpoint
*/
class ResultSet extends ArrayObject
{
/**
* @var int
*/
public $total = 0;

/**
* @var int
*/
public $totalPages = 0;

/**
* @var Psr\Http\Message\RequestInterface
*/
public $request;

/**
* @param RequestInterface $request
* @param ResponseInterface &$response
* @return ResultSet
*/
public function __construct(RequestInterface $request, ResponseInterface &$response)
{
$this->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');
}
}