Skip to content

Commit e8b92cb

Browse files
authored
Merge pull request #6 from shadiakiki1986/master
endpoint GET able to pass parameters
2 parents 1d55c84 + 53b329e commit e8b92cb

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ $user = $client->users()->get(2);
4343

4444
print_r($user);
4545
```
46+
47+
## Testing
48+
```bash
49+
composer install
50+
vendor/bin/peridot
51+
```

specs/Endpoint/abstract-wp-endpoint.spec.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@
3434
expect($data)->to->equal(['foo' => 'bar']);
3535
});
3636

37+
it('should make a GET request with parameters', function () {
38+
$client = $this->getProphet()->prophesize(WpClient::class);
39+
40+
$request = new Request('GET', '/foo?bar=baz');
41+
$response = new \GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], '{"foo": "bar"}');
42+
43+
$client->send($request)->willReturn($response)->shouldBeCalled();
44+
45+
$endpoint = new FakeEndpoint($client->reveal());
46+
47+
$data = $endpoint->get(null, ['bar'=>'baz']);
48+
expect($data)->to->equal(['foo' => 'bar']);
49+
});
50+
3751
});
3852

3953
describe('save()', function () {

src/Endpoint/AbstractWpEndpoint.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ abstract protected function getEndpoint();
3030

3131
/**
3232
* @param int $id
33+
* @param array $params - parameters that can be passed to GET
34+
* e.g. for tags: https://developer.wordpress.org/rest-api/reference/tags/#arguments
3335
* @return array
3436
*/
35-
public function get($id = null)
37+
public function get($id = null, array $params = null)
3638
{
37-
$request = new Request('GET', $this->getEndpoint() . (is_null($id)?'': '/' . $id));
39+
$uri = $this->getEndpoint();
40+
$uri .= (is_null($id)?'': '/' . $id);
41+
$uri .= (is_null($params)?'': '?' . http_build_query($params));
42+
43+
$request = new Request('GET', $uri);
3844
$response = $this->client->send($request);
3945

4046
if ($response->hasHeader('Content-Type')

0 commit comments

Comments
 (0)