diff --git a/lib/Doctrine/CouchDB/CouchDBClient.php b/lib/Doctrine/CouchDB/CouchDBClient.php index 17b094f..933ca00 100644 --- a/lib/Doctrine/CouchDB/CouchDBClient.php +++ b/lib/Doctrine/CouchDB/CouchDBClient.php @@ -807,6 +807,33 @@ public function ensureFullCommit() throw HTTPException::fromResponse($path, $response); } + return $response->body; + } + + /** + * Bulk get. + * + * @param array> $docs + * + * @throws HTTPException + * + * @return array + */ + public function bulkGet(array $docs = []) + { + $path = '/'.$this->databaseName.'/_bulk_get'; + + $response = $this->httpClient->request( + 'POST', + '/'.$this->databaseName.'/_bulk_get', + json_encode(['docs' => $docs]) + ); + + if ($response->status != 200) { + throw HTTPException::fromResponse($path, $response); + } + + return $response->body; } } diff --git a/tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php b/tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php index 1dd7db6..5322c7a 100644 --- a/tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php +++ b/tests/Doctrine/Tests/CouchDB/Functional/CouchDBClientTest.php @@ -393,20 +393,21 @@ public function testFindDocuments() } $response = $client->findDocuments($ids); - - $this->assertEquals(['total_rows' => 3, 'rows' => $expectedRows], $response->body); + // print_r(['total_rows' => 3, 'offset' => null, 'rows' => $expectedRows]); + // print_r($response->body); + $this->assertEquals(['total_rows' => 3, 'offset' => null, 'rows' => $expectedRows], $response->body); $response = $client->findDocuments($ids, 0); - $this->assertEquals(['total_rows' => 3, 'rows' => $expectedRows], $response->body); + $this->assertEquals(['total_rows' => 3, 'offset' => null, 'rows' => $expectedRows], $response->body); $response = $client->findDocuments($ids, 1); - $this->assertEquals(['total_rows' => 3, 'rows' => [$expectedRows[0]]], $response->body); + $this->assertEquals(['total_rows' => 3, 'offset' => null, 'rows' => [$expectedRows[0]]], $response->body); $response = $client->findDocuments($ids, 0, 2); - $this->assertEquals(['total_rows' => 3, 'rows' => [$expectedRows[2]]], $response->body); + $this->assertEquals(['total_rows' => 3, 'offset' => null, 'rows' => [$expectedRows[2]]], $response->body); $response = $client->findDocuments($ids, 1, 1); - $this->assertEquals(['total_rows' => 3, 'rows' => [$expectedRows[1]]], $response->body); + $this->assertEquals(['total_rows' => 3, 'offset' => null, 'rows' => [$expectedRows[1]]], $response->body); } public function testAllDocs() @@ -683,4 +684,53 @@ public function testEncodeQueryParamsCorrectly() $this->assertEquals(0, $result->getTotalRows()); } + + + public function testBulkGet(): void + { + $client = $this->couchClient; + + + $docs = []; + $expectedRows = []; + foreach (range(1, 3) as $i) { + list($id, $rev) = $client->postDocument(['foo' => 'bar'.$i]); + $docs[] = [ + 'id' => $id, + 'rev' => $rev + ]; + + $ids[] = $id; + + $expectedRows[] = [ + 'id' => $id, + 'docs' => [ + ['ok' => [ + '_id' => $id, + '_rev' => $rev, + 'foo' => 'bar'.$i, + ]] + ] + ]; + } + + + $bulkGet = $client->bulkGet($docs); + $this->assertEquals(['results' => $expectedRows], $bulkGet); + + $bulkGetNonexistent = $client->bulkGet([['id' => 'nonexistent-id']]); + $expectedRowsWhenNonexistent[] = [ + 'id' => 'nonexistent-id', + 'docs' => [ + ['error' => [ + 'id' => 'nonexistent-id', + 'rev' => 'undefined', + 'error' => 'not_found', + 'reason' => 'missing', + ]] + ] + ]; + + $this->assertEquals(['results' => $expectedRowsWhenNonexistent], $bulkGetNonexistent); + } }