Beispiel #1
0
 /**
  * Create a CouchDB-Lucene Query.
  *
  * @param string $designDocName
  * @param string $viewName
  * @return View\ODMLuceneQuery
  */
 public function createLuceneQuery($designDocName, $viewName)
 {
     $luceneHandlerName = $this->config->getLuceneHandlerName();
     $designDoc = $this->config->getDesignDocument($designDocName);
     if ($designDoc) {
         $designDoc = new $designDoc['className']($designDoc['options']);
     }
     $query = new ODMLuceneQuery($this->couchDBClient->getHttpClient(), $this->couchDBClient->getDatabase(), $luceneHandlerName, $designDocName, $viewName, $designDoc);
     $query->setDocumentManager($this);
     return $query;
 }
Beispiel #2
0
 /**
  * Transfer missing revisions to the target. The Content-Type of response
  * from the source should be multipart/mixed.
  *
  * @param string $docId
  * @param array $missingRevs
  * @param CouchDBClient $target
  * @return array|HTTP\ErrorResponse|string
  * @throws HTTPException
  */
 public function transferChangedDocuments($docId, $missingRevs, CouchDBClient $target)
 {
     $path = '/' . $this->getDatabase() . '/' . $docId;
     $params = array('revs' => true, 'latest' => true, 'open_revs' => json_encode($missingRevs));
     $query = http_build_query($params);
     $path .= '?' . $query;
     $targetPath = '/' . $target->getDatabase() . '/' . $docId . '?new_edits=false';
     $mutltipartHandler = new MultipartParserAndSender($this->getHttpClient(), $target->getHttpClient());
     return $mutltipartHandler->request('GET', $path, $targetPath, null, array('Accept' => 'multipart/mixed'));
 }
 public function testRequestSuccessWithAttachments()
 {
     $client = new CouchDBClient($this->getHttpClient(), $this->getTestDatabase());
     // Recreate DB
     $client->deleteDatabase($this->getTestDatabase());
     $client->createDatabase($this->getTestDatabase());
     // Doc id.
     $id = $this->docId;
     // Document with attachments.
     $docWithAttachment = array('_id' => $id, '_rev' => '1-abc', '_attachments' => array('foo.txt' => array('content_type' => 'text/plain', 'data' => 'VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ='), 'bar.txt' => array('content_type' => 'text/plain', 'data' => 'VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=')));
     // Doc without any attachment. The id of both the docs is same.
     // So we will get two leaf revisions.
     $doc = array('_id' => $id, 'foo' => 'bar', '_rev' => '1-bcd');
     // Add the documents to the test db using Bulk API.
     $updater = $client->createBulkUpdater();
     $updater->updateDocument($docWithAttachment);
     $updater->updateDocument($doc);
     // Set newedits to false to use the supplied _rev instead of assigning
     // new ones.
     $updater->setNewEdits(false);
     $response = $updater->execute();
     // Create the copy database and a copyClient to interact with it.
     $copyDb = $this->getTestDatabase() . '_multipart_copy';
     $client->createDatabase($copyDb);
     $copyClient = new CouchDBClient($client->getHttpClient(), $copyDb);
     // Missing revisions in the $copyDb.
     $missingRevs = array('1-abc', '1-bcd');
     $this->sourceParams['open_revs'] = json_encode($missingRevs);
     $query = http_build_query($this->sourceParams);
     $this->sourcePath .= '?' . $query;
     // Get the multipart data stream from real CouchDB instance.
     $stream = (new StreamClient())->getConnection($this->sourceMethod, $this->sourcePath, null, $this->sourceHeaders);
     // Set the return values for the mocked StreamClient.
     $this->streamClientMock->expects($this->once())->method('getConnection')->willReturn($stream);
     // Return header with status code as 200.
     $this->streamClientMock->expects($this->once())->method('getStreamHeaders')->willReturn(array('status' => 200));
     // Transfer the missing revisions from the source to the target.
     list($docStack, $responses) = $this->parserAndSender->request($this->sourceMethod, $this->sourcePath, $this->targetPath, null, $this->sourceHeaders);
     // $docStack should contain the doc that didn't have the attachment.
     $this->assertEquals(1, count($docStack));
     $this->assertEquals($doc, json_decode($docStack[0], true));
     // The doc with attachment should have been copied to the copyDb.
     $this->assertEquals(1, count($responses));
     $this->assertArrayHasKey('ok', $responses[0]);
     $this->assertEquals(true, $responses[0]['ok']);
     // Clean up.
     $client->deleteDatabase($this->getTestDatabase());
     $client->createDatabase($this->getTestDatabase());
     $client->deleteDatabase($copyDb);
 }