Skip to content

PHPLIB-381: Update fourth change stream example #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions tests/DocumentationExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -954,11 +954,11 @@ public function testChangeStreamExample_1_4()

$changeStream->next();

$nextChange = $changeStream->current();
$secondChange = $changeStream->current();
// End Changestream Example 2

$this->assertNull($firstChange);
$this->assertNull($nextChange);
$this->assertNull($secondChange);

$insertManyResult = $db->inventory->insertMany([
['_id' => 1, 'x' => 'foo'],
Expand All @@ -984,39 +984,42 @@ public function testChangeStreamExample_1_4()
$resumeToken = ($lastChange !== null) ? $lastChange->_id : null;

if ($resumeToken === null) {
throw new \Exception('resumeToken was not found');
throw new \Exception('Resume token was not found');
}

$changeStream = $db->inventory->watch([], ['resumeAfter' => $resumeToken]);
$changeStream->rewind();

$nextChange = $changeStream->current();
$firstChange = $changeStream->current();
// End Changestream Example 3

$expectedChange = [
'_id' => $nextChange->_id,
'_id' => $firstChange->_id,
'operationType' => 'insert',
'fullDocument' => ['_id' => 2, 'x' => 'bar'],
'ns' => ['db' => $this->getDatabaseName(), 'coll' => 'inventory'],
'documentKey' => ['_id' => 2],
];

$this->assertMatchesDocument($expectedChange, $nextChange);
$this->assertMatchesDocument($expectedChange, $firstChange);

// Start Changestream Example 4
$pipeline = [['$match' => ['$or' => [['fullDocument.username' => 'alice'], ['operationType' => 'delete']]]]];
$pipeline = [
['$match' => ['fullDocument.username' => 'alice']],
['$addFields' => ['newField' => 'this is an added field!']],
];
$changeStream = $db->inventory->watch($pipeline);
$changeStream->rewind();

$firstChange = $changeStream->current();

$changeStream->next();

$nextChange = $changeStream->current();
$secondChange = $changeStream->current();
// End Changestream Example 4

$this->assertNull($firstChange);
$this->assertNull($nextChange);
$this->assertNull($secondChange);
}

public function testAggregation_example_1()
Expand Down