Skip to content

Commit 8c8afa3

Browse files
committed
Add the ability to get all documents from database
1 parent 0ee6eb3 commit 8c8afa3

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

src/PHPCouchDB/Database.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,31 @@ public function __construct($options)
5252
);
5353
}
5454
}
55+
56+
/**
57+
* Fetch all the documents from the database
58+
*
59+
* @param array $options Any modifiers needed for the query These include:
60+
* - include_docs Defaults to true
61+
* @return array The array contains `PHPCouchDB\Document` objects
62+
*/
63+
public function getAllDocs($options = []) : array
64+
{
65+
$endpoint = "/" . $this->db_name . "/_all_docs";
66+
$query = ["include_docs" => "true"];
67+
$response = $this->client->request("GET", $endpoint, ["query" => $query]);
68+
if ($response->getStatusCode() == 200) {
69+
// try to decode JSON
70+
if ($json_data = json_decode($response->getBody(), true)) {
71+
// we have some data - extract the docs to return
72+
$docs = [];
73+
foreach ($json_data["rows"] as $document) {
74+
$docs[] = new Document($document["doc"]);
75+
}
76+
return $docs;
77+
} else {
78+
throw new \PHPCouchDB\Exception\ServerException('JSON response not received or not understood');
79+
}
80+
}
81+
}
5582
}

src/PHPCouchDB/Document.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Objects to represent individual documents
5+
*/
6+
7+
namespace PHPCouchDB;
8+
9+
/**
10+
* Documents are the "rows" in a document database. The object has keys and
11+
* values to represent the keys and values of the document.
12+
*/
13+
14+
15+
class Document
16+
{
17+
/**
18+
* Usually constructed by the Database object as it gets documents for us
19+
*
20+
* @param array $data Representation of the document
21+
*/
22+
public function __construct($data)
23+
{
24+
// possibly overly simple!
25+
// Add all array elements as properties on the new object
26+
foreach ($data as $field => $value) {
27+
if ($field == "_id") {
28+
$this->id = $value;
29+
} elseif ($field == "_rev") {
30+
$this->rev = $value;
31+
} else {
32+
$this->{$field} = $value;
33+
}
34+
}
35+
}
36+
}

src/PHPCouchDB/Server.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,4 @@ public function getClient() : \GuzzleHttp\ClientInterface
158158
{
159159
return $this->client;
160160
}
161-
162161
}

tests/PHPCouchDB/DatabaseTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
require __DIR__ . "/../../vendor/autoload.php";
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\HandlerStack;
8+
use GuzzleHttp\Psr7\Response;
9+
use GuzzleHttp\Psr7\Request;
10+
use GuzzleHttp\Exception\RequestException;
11+
12+
class DatabaseTest extends \PHPUnit\Framework\TestCase
13+
{
14+
public function setUp() {
15+
// create the first request to check we can connect, can be added to
16+
// the mocks for any test that wants it
17+
$couchdb1 = '{"couchdb":"Welcome","uuid":"fce3d5aabfe189c988273c0ffa8d375b","version":"1.6.0","vendor":{"name":"Ubuntu","version":"15.10"}}';
18+
$this->db_response = new Response(200, [], $couchdb1);
19+
}
20+
21+
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);
24+
$docs = '{"total_rows":2,"offset":0,"rows":[
25+
{"id":"95613816b3a7490727388ebb470001a6","key":"95613816b3a7490727388ebb470001a6","value":{"rev":"1-71e39cb1ac06a5974a16c72b26969009"},"doc":{"_id":"95613816b3a7490727388ebb470001a6","_rev":"1-71e39cb1ac06a5974a16c72b26969009","sound":"squeak"}},
26+
{"id":"95613816b3a7490727388ebb4700165a","key":"95613816b3a7490727388ebb4700165a","value":{"rev":"1-1ed93c4b346f531c5e7d4d69b755ee71"},"doc":{"_id":"95613816b3a7490727388ebb4700165a","_rev":"1-1ed93c4b346f531c5e7d4d69b755ee71","noise":"pop"}}
27+
]}';
28+
$docs_response = new Response(200, [], $docs);
29+
30+
$mock = new MockHandler([ $this->db_response, $use_response, $docs_response ]);
31+
32+
$handler = HandlerStack::create($mock);
33+
$client = new Client(['handler' => $handler]);
34+
35+
// userland code starts
36+
$server = new \PHPCouchDB\Server(["client" => $client]);
37+
$database = $server->useDB(["name" => "egdb"]);
38+
$docs = $database->getAllDocs();
39+
40+
$this->assertInternalType('array', $docs);
41+
$this->assertInstanceOf('\PHPCouchDB\Document', $docs[0]);
42+
}
43+
44+
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);
47+
$docs = '{"total_rows":0,"offset":0,"rows":[
48+
49+
]}';
50+
$docs_response = new Response(200, [], $docs);
51+
52+
$mock = new MockHandler([ $this->db_response, $use_response, $docs_response ]);
53+
54+
$handler = HandlerStack::create($mock);
55+
$client = new Client(['handler' => $handler]);
56+
57+
// userland code starts
58+
$server = new \PHPCouchDB\Server(["client" => $client]);
59+
$database = $server->useDB(["name" => "egdb"]);
60+
$docs = $database->getAllDocs();
61+
62+
$this->assertInternalType('array', $docs);
63+
$this->assertEmpty($docs);
64+
}
65+
}

0 commit comments

Comments
 (0)