Skip to content

Commit 35310a2

Browse files
committed
Add the ability to get a list of databases
1 parent a4472e1 commit 35310a2

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/PHPCouchDB/Server.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,21 @@ public function getVersion() : string
6666
}
6767
}
6868
}
69+
70+
/**
71+
* Get a list of databases
72+
*
73+
* @return array The database names
74+
*/
75+
public function getAllDbs() {
76+
$response = $this->client->request("GET", "/_all_dbs");
77+
if ($response->getStatusCode() == 200) {
78+
// try to decode JSON
79+
if ($json_data = json_decode($response->getBody(), true)) {
80+
return $json_data;
81+
} else {
82+
throw new Exception('JSON response not received or not understood');
83+
}
84+
}
85+
}
6986
}

tests/PHPCouchDB/ServerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,18 @@ public function testGetVersion() {
4646
$this->assertEquals("1.6.0", $server->getVersion());
4747

4848
}
49+
50+
public function testGetAllDbs() {
51+
$dbs = ["test", "items"];
52+
$response1 = new Response(200, [], json_encode($dbs));
53+
54+
$mock = new MockHandler([ $response1 ]);
55+
$handler = HandlerStack::create($mock);
56+
$client = new Client(['handler' => $handler]);
57+
58+
// userland code starts
59+
$server = new \PHPCouchDB\Server(["client" => $client]);
60+
$this->assertEquals($dbs, $server->getAllDbs());
61+
62+
}
4963
}

0 commit comments

Comments
 (0)