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: 45 additions & 0 deletions specs/Endpoint/abstract-wp-endpoint.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,51 @@

});

describe('getResponse()', function () {
it('should make a GET request to the endpoint URL', 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->getResponse(55);
expect($data)->to->equal($response);
});

it('should make a GET request without any ID', function () {
$client = $this->getProphet()->prophesize(WpClient::class);

$request = new Request('GET', '/foo');
$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->getResponse();
expect($data)->to->equal($response);
});

it('should make a GET request with parameters', function () {
$client = $this->getProphet()->prophesize(WpClient::class);

$request = new Request('GET', '/foo?bar=baz');
$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->getResponse(null, ['bar'=>'baz']);
expect($data)->to->equal($response);
});

});

describe('save()', function () {
it('should make a POST request to the endpoint URL', function () {
$client = $this->getProphet()->prophesize(WpClient::class);
Expand Down
23 changes: 23 additions & 0 deletions src/Endpoint/AbstractWpEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ public function get($id = null, array $params = null)
throw new RuntimeException('Unexpected response');
}

/**
* @param int $id
* @param array $params - parameters that can be passed to GET
* e.g. for tags: https://developer.wordpress.org/rest-api/reference/tags/#arguments
* @return \Psr\Http\Message\ResponseInterface
*/
public function getResponse($id = null, array $params = null)
{
$uri = $this->getEndpoint();
$uri .= (is_null($id)?'': '/' . $id);
$uri .= (is_null($params)?'': '?' . http_build_query($params));

$request = new Request('GET', $uri);
$response = $this->client->send($request);

if ($response->hasHeader('Content-Type')
&& substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') {
return $response;
}

throw new RuntimeException('Unexpected response');
}

/**
* @param array $data
* @return array
Expand Down