Skip to content

Commit da7a7c7

Browse files
authored
Merge pull request #31 from chadicus/fea/optional-id-delete
Allow null id with delete request
2 parents fb69f41 + 53e6207 commit da7a7c7

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

src/Client.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,13 @@ public function put($resource, $id, array $data)
272272
*/
273273
public function startDelete($resource, $id, array $data = null)
274274
{
275-
Util::throwIfNotType(['string' => [$resource, $id]], true);
276-
$url = "{$this->_baseUrl}/" . urlencode($resource) . '/' . urlencode($id);
275+
Util::throwIfNotType(['string' => [$resource]], true);
276+
$url = "{$this->_baseUrl}/" . urlencode($resource);
277+
if ($id !== null) {
278+
Util::throwIfNotType(['string' => [$id]], true);
279+
$url .= '/' . urlencode($id);
280+
}
281+
277282
$json = $data !== null ? json_encode($data) : null;
278283
return $this->_start($url, 'DELETE', $json, ['Content-Type' => 'application/json']);
279284
}

tests/ClientTest.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,25 @@ public function delete()
286286
$this->assertSame(['Content-Type' => ['application/json']], $response->getResponseHeaders());
287287
}
288288

289+
/**
290+
* Verify behavior of startDelete when no id is given.
291+
*
292+
* @test
293+
* @covers ::startDelete
294+
*
295+
* @return void
296+
*/
297+
public function deleteWithoutId()
298+
{
299+
$adapter = new DeleteAdapter();
300+
$authentication = Authentication::createClientCredentials('not under test', 'not under test');
301+
$client = new Client($adapter, $authentication, 'baseUrl/v1');
302+
$client->startDelete('resource', null, ['foo' => 'bar']);
303+
$this->assertSame('baseUrl/v1/resource', $adapter->request->getUrl());
304+
$this->assertSame('DELETE', $adapter->request->getMethod());
305+
$this->assertSame(json_encode(['foo' => 'bar']), $adapter->request->getBody());
306+
}
307+
289308
/**
290309
* Verfiy delete creates the request body properly
291310
*
@@ -715,29 +734,29 @@ public function end($handle)
715734

716735
final class DeleteAdapter implements Adapter
717736
{
718-
private $_request;
737+
public $request;
719738

720739
public function start(Request $request)
721740
{
722-
$this->_request = $request;
741+
$this->request = $request;
723742
}
724743

725744
public function end($handle)
726745
{
727-
if (substr($this->_request->getUrl(), -5) === 'token') {
746+
if (substr($this->request->getUrl(), -5) === 'token') {
728747
return new Response(200, ['Content-Type' => ['application/json']], ['access_token' => 'a token', 'expires_in' => 1]);
729748
}
730749

731750
if (
732-
$this->_request->getMethod() === 'DELETE' &&
733-
$this->_request->getUrl() === 'baseUrl/v1/resource+name/the+id' &&
734-
$this->_request->getHeaders() === [
751+
$this->request->getMethod() === 'DELETE' &&
752+
$this->request->getUrl() === 'baseUrl/v1/resource+name/the+id' &&
753+
$this->request->getHeaders() === [
735754
'Content-Type' => 'application/json',
736755
'Accept-Encoding' => 'gzip',
737756
'Authorization' => 'Bearer a token',
738757
]
739758
) {
740-
$body = $this->_request->getBody();
759+
$body = $this->request->getBody();
741760

742761
if ($body === null || $body === '{"the key":"the value"}') {
743762
return new Response(204, ['Content-Type' => ['application/json']]);

0 commit comments

Comments
 (0)