Commit 1f0d7bc5 authored by Jeremy Mikola's avatar Jeremy Mikola

Revise change stream doc examples to use rewind()

This also adds more assertions between the doc examples.
parent 90ceb3a3
...@@ -933,65 +933,93 @@ class DocumentationExamplesTest extends FunctionalTestCase ...@@ -933,65 +933,93 @@ class DocumentationExamplesTest extends FunctionalTestCase
} }
$db = new Database($this->manager, $this->getDatabaseName()); $db = new Database($this->manager, $this->getDatabaseName());
$db->dropCollection('inventory');
// Start Changestream Example 1 // Start Changestream Example 1
$changeStream = $db->inventory->watch(); $changeStream = $db->inventory->watch();
$changeStream->rewind();
$firstChange = $changeStream->current();
$changeStream->next(); $changeStream->next();
$document = $changeStream->current();
$secondChange = $changeStream->current();
// End Changestream Example 1 // End Changestream Example 1
$this->assertNull($document); $this->assertNull($firstChange);
$this->assertNull($secondChange);
// Start Changestream Example 2 // Start Changestream Example 2
$changeStream = $db->inventory->watch([], ['fullDocument' => \MongoDB\Operation\Watch::FULL_DOCUMENT_UPDATE_LOOKUP]); $changeStream = $db->inventory->watch([], ['fullDocument' => \MongoDB\Operation\Watch::FULL_DOCUMENT_UPDATE_LOOKUP]);
$changeStream->rewind();
$firstChange = $changeStream->current();
$changeStream->next(); $changeStream->next();
$document = $changeStream->current();
$nextChange = $changeStream->current();
// End Changestream Example 2 // End Changestream Example 2
$this->assertNull($document); $this->assertNull($firstChange);
$this->assertNull($nextChange);
$insertManyResult = $db->inventory->insertMany([
['_id' => 1, 'x' => 'foo'],
['_id' => 2, 'x' => 'bar'],
]);
$this->assertEquals(2, $insertManyResult->getInsertedCount());
$insertedResult = $db->inventory->insertOne(['x' => 1]);
$insertedId = $insertedResult->getInsertedId();
$changeStream->next(); $changeStream->next();
$document = $changeStream->current(); $this->assertTrue($changeStream->valid());
$lastChange = $changeStream->current();
$expectedChange = [ $expectedChange = [
'_id' => $document->_id, '_id' => $lastChange->_id,
'operationType' => 'insert', 'operationType' => 'insert',
'fullDocument' => ['_id' => $insertedId, 'x' => 1], 'fullDocument' => ['_id' => 1, 'x' => 'foo'],
'ns' => ['db' => $this->getDatabaseName(), 'coll' => 'inventory'], 'ns' => ['db' => $this->getDatabaseName(), 'coll' => 'inventory'],
'documentKey' => ['_id' => $insertedId], 'documentKey' => ['_id' => 1],
]; ];
$this->assertSameDocument($expectedChange, $document); $this->assertSameDocument($expectedChange, $lastChange);
// Start Changestream Example 3 // Start Changestream Example 3
$resumeToken = ($document !== null) ? $document->_id : null; $resumeToken = ($lastChange !== null) ? $lastChange->_id : null;
if ($resumeToken !== null) {
$changeStream = $db->inventory->watch([], ['resumeAfter' => $resumeToken]); if ($resumeToken === null) {
$changeStream->next(); throw new \Exception('resumeToken was not found');
} }
// End Changestream Example 3
$insertedResult = $db->inventory->insertOne(['x' => 2]); $changeStream = $db->inventory->watch([], ['resumeAfter' => $resumeToken]);
$insertedId = $insertedResult->getInsertedId(); $changeStream->rewind();
$changeStream->next();
$nextChange = $changeStream->current();
// End Changestream Example 3
$expectedChange = [ $expectedChange = [
'_id' => $changeStream->current()->_id, '_id' => $nextChange->_id,
'operationType' => 'insert', 'operationType' => 'insert',
'fullDocument' => ['_id' => $insertedId, 'x' => 2], 'fullDocument' => ['_id' => 2, 'x' => 'bar'],
'ns' => ['db' => $this->getDatabaseName(), 'coll' => 'inventory'], 'ns' => ['db' => $this->getDatabaseName(), 'coll' => 'inventory'],
'documentKey' => ['_id' => $insertedId], 'documentKey' => ['_id' => 2],
]; ];
$this->assertSameDocument($expectedChange, $changeStream->current()); $this->assertSameDocument($expectedChange, $nextChange);
// Start Changestream Example 4 // Start Changestream Example 4
$pipeline = [['$match' => ['$or' => [['fullDocument.username' => 'alice'], ['operationType' => 'delete']]]]]; $pipeline = [['$match' => ['$or' => [['fullDocument.username' => 'alice'], ['operationType' => 'delete']]]]];
$changeStream = $db->inventory->watch($pipeline); $changeStream = $db->inventory->watch($pipeline);
$changeStream->rewind();
$firstChange = $changeStream->current();
$changeStream->next(); $changeStream->next();
$nextChange = $changeStream->current();
// End Changestream Example 4 // End Changestream Example 4
$this->assertNull($firstChange);
$this->assertNull($nextChange);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment