Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
1f0d7bc5
Commit
1f0d7bc5
authored
Feb 02, 2018
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revise change stream doc examples to use rewind()
This also adds more assertions between the doc examples.
parent
90ceb3a3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
23 deletions
+51
-23
DocumentationExamplesTest.php
tests/DocumentationExamplesTest.php
+51
-23
No files found.
tests/DocumentationExamplesTest.php
View file @
1f0d7bc5
...
@@ -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
);
}
}
/**
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment