Skip to content

Commit 2658f4a

Browse files
committed
Add ability to fetch doc by ID
Also refactor tests to put the response for selecting a database into setUp()
1 parent 18641a8 commit 2658f4a

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/PHPCouchDB/Database.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,28 @@ public function create($doc)
124124
);
125125
}
126126
}
127+
128+
/**
129+
* Get a document whose ID you know
130+
*
131+
* @param string $id The doc's unique identifier
132+
* @return PHPCouchDB\Document The doc with the specified ID
133+
* @throws PHPCouchDB\Exception\ServerException if the response can't be understood
134+
* @throws PHPCouchDB\Exception\DatabaseException if the doc isn't found
135+
*/
136+
public function getDocById($id) : \PHPCouchDB\Document
137+
{
138+
$endpoint = "/" . $this->db_name . "/" . $id;
139+
$response = $this->client->request("GET", $endpoint);
140+
if ($response->getStatusCode() == 200) {
141+
if ($json_data = json_decode($response->getBody(), true)) {
142+
$doc = new Document($json_data);
143+
return $doc;
144+
} else {
145+
throw new \PHPCouchDB\Exception\ServerException('JSON response not received or not understood');
146+
}
147+
} else {
148+
throw new \PHPCouchDB\Exception\DatabaseException('Document not found');
149+
}
150+
}
127151
}

tests/PHPCouchDB/DatabaseTest.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ public function setUp() {
1616
// the mocks for any test that wants it
1717
$couchdb1 = '{"couchdb":"Welcome","uuid":"fce3d5aabfe189c988273c0ffa8d375b","version":"1.6.0","vendor":{"name":"Ubuntu","version":"15.10"}}';
1818
$this->db_response = new Response(200, [], $couchdb1);
19+
20+
// offer a use_response for when selecting this database
21+
$egdb1 = '{"db_name":"egdb","update_seq":"0-g1AAAABXeJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuRAY-iPBYgydAApP5D1GYBAJmvHGw","sizes":{"file":8488,"external":0,"active":0},"purge_seq":0,"other":{"data_size":0},"doc_del_count":0,"doc_count":0,"disk_size":8488,"disk_format_version":6,"data_size":0,"compact_running":false,"instance_start_time":"0"}';
22+
$this->use_response = new Response(200, [], $egdb1);
1923
}
2024

2125
public function testGetAllDocs() {
22-
$egdb1 = '{"db_name":"egdb","update_seq":"0-g1AAAABXeJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuRAY-iPBYgydAApP5D1GYBAJmvHGw","sizes":{"file":8488,"external":0,"active":0},"purge_seq":0,"other":{"data_size":0},"doc_del_count":0,"doc_count":0,"disk_size":8488,"disk_format_version":6,"data_size":0,"compact_running":false,"instance_start_time":"0"}';
23-
$use_response = new Response(200, [], $egdb1);
2426
$docs = '{"total_rows":2,"offset":0,"rows":[
2527
{"id":"95613816b3a7490727388ebb470001a6","key":"95613816b3a7490727388ebb470001a6","value":{"rev":"1-71e39cb1ac06a5974a16c72b26969009"},"doc":{"_id":"95613816b3a7490727388ebb470001a6","_rev":"1-71e39cb1ac06a5974a16c72b26969009","sound":"squeak"}},
2628
{"id":"95613816b3a7490727388ebb4700165a","key":"95613816b3a7490727388ebb4700165a","value":{"rev":"1-1ed93c4b346f531c5e7d4d69b755ee71"},"doc":{"_id":"95613816b3a7490727388ebb4700165a","_rev":"1-1ed93c4b346f531c5e7d4d69b755ee71","noise":"pop"}}
2729
]}';
2830
$docs_response = new Response(200, [], $docs);
2931

30-
$mock = new MockHandler([ $this->db_response, $use_response, $docs_response ]);
32+
$mock = new MockHandler([ $this->db_response, $this->use_response, $docs_response ]);
3133

3234
$handler = HandlerStack::create($mock);
3335
$client = new Client(['handler' => $handler]);
@@ -42,14 +44,12 @@ public function testGetAllDocs() {
4244
}
4345

4446
public function testGetAllDocsWithNoDocs() {
45-
$egdb1 = '{"db_name":"egdb","update_seq":"0-g1AAAABXeJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuRAY-iPBYgydAApP5D1GYBAJmvHGw","sizes":{"file":8488,"external":0,"active":0},"purge_seq":0,"other":{"data_size":0},"doc_del_count":0,"doc_count":0,"disk_size":8488,"disk_format_version":6,"data_size":0,"compact_running":false,"instance_start_time":"0"}';
46-
$use_response = new Response(200, [], $egdb1);
4747
$docs = '{"total_rows":0,"offset":0,"rows":[
4848
4949
]}';
5050
$docs_response = new Response(200, [], $docs);
5151

52-
$mock = new MockHandler([ $this->db_response, $use_response, $docs_response ]);
52+
$mock = new MockHandler([ $this->db_response, $this->use_response, $docs_response ]);
5353

5454
$handler = HandlerStack::create($mock);
5555
$client = new Client(['handler' => $handler]);
@@ -64,14 +64,12 @@ public function testGetAllDocsWithNoDocs() {
6464
}
6565

6666
public function testCreateWithID() {
67-
$egdb1 = '{"db_name":"egdb","update_seq":"0-g1AAAABXeJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuRAY-iPBYgydAApP5D1GYBAJmvHGw","sizes":{"file":8488,"external":0,"active":0},"purge_seq":0,"other":{"data_size":0},"doc_del_count":0,"doc_count":0,"disk_size":8488,"disk_format_version":6,"data_size":0,"compact_running":false,"instance_start_time":"0"}';
68-
$use_response = new Response(200, [], $egdb1);
6967
$create = '{"ok":true,"id":"abcde12345","rev":"1-928ec193918889e122e7ad45cfd88e47"}';
7068
$create_response = new Response(201, [], $create);
7169
$fetch = '{"_id":"abcde12345","_rev":"1-928ec193918889e122e7ad45cfd88e47","noise":"howl"}';
7270
$fetch_response = new Response(200, [], $fetch);
7371

74-
$mock = new MockHandler([ $this->db_response, $use_response, $create_response, $fetch_response ]);
72+
$mock = new MockHandler([ $this->db_response, $this->use_response, $create_response, $fetch_response ]);
7573
$handler = HandlerStack::create($mock);
7674
$client = new Client(['handler' => $handler]);
7775

@@ -86,14 +84,12 @@ public function testCreateWithID() {
8684
}
8785

8886
public function testCreateWithoutID() {
89-
$egdb1 = '{"db_name":"egdb","update_seq":"0-g1AAAABXeJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuRAY-iPBYgydAApP5D1GYBAJmvHGw","sizes":{"file":8488,"external":0,"active":0},"purge_seq":0,"other":{"data_size":0},"doc_del_count":0,"doc_count":0,"disk_size":8488,"disk_format_version":6,"data_size":0,"compact_running":false,"instance_start_time":"0"}';
90-
$use_response = new Response(200, [], $egdb1);
9187
$create = '{"ok":true,"id":"95613816b3a7490727388ebb47002c0f","rev":"1-928ec193918889e122e7ad45cfd88e47"}';
9288
$create_response = new Response(201, [], $create);
9389
$fetch = '{"_id":"95613816b3a7490727388ebb47002c0f","_rev":"1-928ec193918889e122e7ad45cfd88e47","noise":"howl"}';
9490
$fetch_response = new Response(200, [], $fetch);
9591

96-
$mock = new MockHandler([ $this->db_response, $use_response, $create_response, $fetch_response ]);
92+
$mock = new MockHandler([ $this->db_response, $this->use_response, $create_response, $fetch_response ]);
9793
$handler = HandlerStack::create($mock);
9894
$client = new Client(['handler' => $handler]);
9995

@@ -105,4 +101,26 @@ public function testCreateWithoutID() {
105101
$this->assertInstanceOf('PHPCouchDB\Document', $doc);
106102
$this->assertObjectHasAttribute('id', $doc);
107103
}
104+
105+
public function testGetDocById() {
106+
// create the doc with the id (which also triggers a fetch), then fetch it
107+
$create = '{"ok":true,"id":"95613816b3a7490727388ebb47002c0f","rev":"1-928ec193918889e122e7ad45cfd88e47"}';
108+
$create_response = new Response(201, [], $create);
109+
$fetch = '{"_id":"95613816b3a7490727388ebb47002c0f","_rev":"1-928ec193918889e122e7ad45cfd88e47","noise":"howl"}';
110+
$fetch_response = new Response(200, [], $fetch);
111+
112+
$mock = new MockHandler([ $this->db_response, $this->use_response, $create_response, $fetch_response, $fetch_response ]);
113+
$handler = HandlerStack::create($mock);
114+
$client = new Client(['handler' => $handler]);
115+
116+
// userland code starts
117+
$server = new \PHPCouchDB\Server(["client" => $client]);
118+
$database = $server->useDB(["name" => "egdb"]);
119+
$doc = $database->create(["noise" => "crackle"]);
120+
121+
$fetched_doc = $database->getDocById($doc->id);
122+
123+
$this->assertInstanceOf('PHPCouchDB\Document', $fetched_doc);
124+
$this->assertObjectHasAttribute('id', $fetched_doc);
125+
}
108126
}

0 commit comments

Comments
 (0)