/** * @depends testCreateBulkUpdater */ public function testGetChanges() { $updater = $this->couchClient->createBulkUpdater(); $updater->updateDocument(array("_id" => "test1", "foo" => "bar")); $updater->updateDocument(array("_id" => "test2", "bar" => "baz")); $updater->execute(); $changes = $this->couchClient->getChanges(); $this->assertArrayHasKey('results', $changes); $this->assertEquals(2, count($changes['results'])); $this->assertEquals(2, $changes['last_seq']); // Check the limit parameter. $changes = $this->couchClient->getChanges(array('limit' => 1)); $this->assertArrayHasKey('results', $changes); $this->assertEquals(1, count($changes['results'])); $this->assertEquals(1, $changes['last_seq']); // Checks the descending parameter. $changes = $this->couchClient->getChanges(array('descending' => true)); $this->assertArrayHasKey('results', $changes); $this->assertEquals(2, count($changes['results'])); $this->assertEquals(1, $changes['last_seq']); // Checks the since parameter. $changes = $this->couchClient->getChanges(array('since' => 1)); $this->assertArrayHasKey('results', $changes); $this->assertEquals(1, count($changes['results'])); $this->assertEquals(2, $changes['last_seq']); // Checks the filter parameter. $designDocPath = __DIR__ . "/../../Models/CMS/_files"; // Create a filter, that filters the only doc with {"_id":"test1"} $client = $this->couchClient; $client->createDesignDocument('test-filter', new FolderDesignDocument($designDocPath)); $changes = $this->couchClient->getChanges(array('filter' => 'test-filter/my_filter')); $this->assertEquals(1, count($changes['results'])); $this->assertEquals(3, $changes['last_seq']); }
/** * @depends testExecute */ public function testDeleteDocument() { $doc = array("_id" => "test1", "foo" => "bar"); $this->bulkUpdater->updateDocument($doc); $response = $this->bulkUpdater->execute(); $rev = $response->body[0]["rev"]; $bulkUpdater2 = $this->couchClient->createBulkUpdater(); $bulkUpdater2->deleteDocument("test1", $rev); $response = $bulkUpdater2->execute(); $response = $this->couchClient->findDocument("test1"); $this->assertEquals(404, $response->status); }
/** * @depends testCreateBulkUpdater */ public function testTransferChangedDocuments() { $client = $this->couchClient; // Recreate DB $client->deleteDatabase($this->getTestDatabase()); $client->createDatabase($this->getTestDatabase()); // Doc id. $id = 'multiple_attachments'; // 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 = $this->couchClient->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() . '_copy'; $client->createDatabase($copyDb); $copyClient = new CouchDBClient($client->getHttpClient(), $copyDb); // Missing revisions in the $copyDb. $missingRevs = array('1-abc', '1-bcd'); // Transfer the missing revisions from the source to the target. list($docStack, $responses) = $client->transferChangedDocuments($id, $missingRevs, $copyClient); // $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); }
/** * @param array $revDiff * @return array|void * @throws HTTPException */ public function replicateChanges(&$revDiff) { $allResponse = array('multipartResponse' => array(), 'bulkResponse' => array()); foreach ($revDiff as $docId => $revMisses) { $bulkUpdater = $this->target->createBulkUpdater(); $bulkUpdater->setNewEdits(false); try { list($docStack, $multipartResponse) = $this->source->transferChangedDocuments($docId, $revMisses['missing'], $this->target); } catch (\Exception $e) { // Deal with the failures. Try again once to deal with the // connection establishment failures of the client. // It's better to deal this in the client itself. usleep(500); list($docStack, $multipartResponse) = $this->source->transferChangedDocuments($docId, $revMisses['missing'], $this->target); } $bulkUpdater->updateDocuments($docStack); // $multipartResponse is an empty array in case there was no // transferred revision that had attachment in the current doc. $allResponse['multipartResponse'][$docId] = $multipartResponse; $allResponse['bulkResponse'][$docId] = $bulkUpdater->execute()->status; } return $allResponse; }
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); }