Skip to content

Commit 18a5b19

Browse files
committed
Add the ability to delete documents
1 parent 1c94ebb commit 18a5b19

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

src/PHPCouchDB/Document.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __debugInfo()
6565
* @throws \PHPCouchDB\Exception\DatabaseExecption if something else goes
6666
* wrong, with the previous exception included
6767
*/
68-
public function update()
68+
public function update() : Document
6969
{
7070
$endpoint = "/" . $this->database->getName() . "/" . $this->id;
7171

@@ -91,4 +91,31 @@ public function update()
9191
}
9292
}
9393
}
94+
95+
public function delete() : bool
96+
{
97+
$endpoint = "/" . $this->database->getName() . "/" . $this->id;
98+
$query = ["rev" => $this->rev];
99+
100+
try {
101+
$response = $this->client->request('DELETE', $endpoint, ["query" => $query]);
102+
// if successful, cool
103+
return true;
104+
} catch (\GuzzleHTTP\Exception\ClientException $e) {
105+
// our reaction depends on the status code
106+
$status = $e->getResponse()->getStatusCode();
107+
108+
if ($status == 404) {
109+
// a 404 error is fine, means the record is already gone
110+
return true;
111+
} elseif ($status == 409) {
112+
// conflict, we're deleting the wrong version of the doc
113+
throw new Exception\DocumentConflictException(
114+
'Document conflict. Only the current document revision can be deleted'
115+
);
116+
} else {
117+
throw new Exception\DatabaseException('The record could not be deleted', 0, $e);
118+
}
119+
}
120+
}
94121
}

tests/PHPCouchDB/DocumentTest.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function setUp() {
2727
$this->fetch_response = new Response(200, [], $fetch);
2828
}
2929

30-
public function testUpdateConflict() {
30+
public function testUpdate() {
3131
$update = '{"ok":true,"id":"abcde12345","rev":"2-74a0465bd6e3ea40a1a3752b93916762"}';
3232
$update_response = new Response(200, [], $update);
3333

@@ -55,7 +55,7 @@ public function testUpdateConflict() {
5555
/**
5656
* @expectedException \PHPCouchDB\Exception\DocumentConflictException
5757
*/
58-
public function testUpdate() {
58+
public function testUpdateConflict() {
5959
$update = '{"error":"conflict","reason":"Document update conflict."}';;
6060
$update_response = new Response(409, [], $update);
6161

@@ -72,4 +72,47 @@ public function testUpdate() {
7272
$newdoc = $doc->update();
7373
}
7474

75+
public function testDelete() {
76+
$delete = '{"ok":true,"id":"abcde12345","rev":"2-74a0465bd6e3ea40a1a3752b93916762"}';
77+
$delete_response = new Response(200, [], $delete);
78+
79+
$fetch3 = '{"error":"not_found","reason":"deleted"}';
80+
$fetch_response3 = new Response(404, [], $fetch3);
81+
82+
$mock = new MockHandler([ $this->db_response, $this->use_response, $this->create_response, $this->fetch_response, $delete_response, $fetch_response3 ]);
83+
$handler = HandlerStack::create($mock);
84+
$client = new Client(['handler' => $handler]);
85+
86+
// userland code starts
87+
$server = new \PHPCouchDB\Server(["client" => $client]);
88+
$database = $server->useDB(["name" => "egdb"]);
89+
$doc = $database->create(["noise" => "howl", "id" => "abcde12345"]);
90+
91+
$result = $doc->delete();
92+
$this->assertEquals(true, $result);
93+
94+
// should be able to delete an already-deleted doc without errors
95+
96+
$result = $doc->delete();
97+
$this->assertEquals(true, $result);
98+
}
99+
100+
/**
101+
* @expectedException \PHPCouchDB\Exception\DocumentConflictException
102+
*/
103+
public function testDeleteConflict() {
104+
$delete = '{"error":"conflict","reason":"Document update conflict."}';
105+
$delete_response = new Response(409, [], $delete);
106+
107+
$mock = new MockHandler([ $this->db_response, $this->use_response, $this->create_response, $this->fetch_response, $delete_response ]);
108+
$handler = HandlerStack::create($mock);
109+
$client = new Client(['handler' => $handler]);
110+
111+
// userland code starts
112+
$server = new \PHPCouchDB\Server(["client" => $client]);
113+
$database = $server->useDB(["name" => "egdb"]);
114+
$doc = $database->create(["noise" => "howl", "id" => "abcde12345"]);
115+
116+
$result = $doc->delete();
117+
}
75118
}

0 commit comments

Comments
 (0)