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
39057724
Commit
39057724
authored
Mar 16, 2018
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'v1.3'
parents
c662aa19
99453efa
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
5 deletions
+59
-5
ChangeStream.php
src/ChangeStream.php
+7
-4
WatchFunctionalTest.php
tests/Operation/WatchFunctionalTest.php
+52
-1
No files found.
src/ChangeStream.php
View file @
39057724
...
...
@@ -38,7 +38,8 @@ class ChangeStream implements Iterator
private
$resumeToken
;
private
$resumeCallable
;
private
$csIt
;
private
$key
;
private
$key
=
0
;
private
$hasAdvanced
=
false
;
const
CURSOR_NOT_FOUND
=
43
;
...
...
@@ -53,8 +54,6 @@ class ChangeStream implements Iterator
{
$this
->
resumeCallable
=
$resumeCallable
;
$this
->
csIt
=
new
IteratorIterator
(
$cursor
);
$this
->
key
=
0
;
}
/**
...
...
@@ -96,8 +95,11 @@ class ChangeStream implements Iterator
try
{
$this
->
csIt
->
next
();
if
(
$this
->
valid
())
{
if
(
$this
->
hasAdvanced
)
{
$this
->
key
++
;
}
$this
->
hasAdvanced
=
true
;
$this
->
resumeToken
=
$this
->
extractResumeToken
(
$this
->
csIt
->
current
());
$this
->
key
++
;
}
}
catch
(
RuntimeException
$e
)
{
if
(
strpos
(
$e
->
getMessage
(),
"not master"
)
!==
false
)
{
...
...
@@ -125,6 +127,7 @@ class ChangeStream implements Iterator
try
{
$this
->
csIt
->
rewind
();
if
(
$this
->
valid
())
{
$this
->
hasAdvanced
=
true
;
$this
->
resumeToken
=
$this
->
extractResumeToken
(
$this
->
csIt
->
current
());
}
}
catch
(
RuntimeException
$e
)
{
...
...
tests/Operation/WatchFunctionalTest.php
View file @
39057724
...
...
@@ -3,7 +3,6 @@
namespace
MongoDB\Tests\Operation
;
use
MongoDB\ChangeStream
;
use
MongoDB\Client
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\Server
;
...
...
@@ -529,6 +528,58 @@ class WatchFunctionalTest extends FunctionalTestCase
];
}
public
function
testNextAdvancesKey
()
{
$operation
=
new
Watch
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[],
$this
->
defaultOptions
);
$changeStream
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
insertDocument
([
'x'
=>
1
]);
$this
->
insertDocument
([
'x'
=>
2
]);
$changeStream
->
next
();
$this
->
assertSame
(
0
,
$changeStream
->
key
());
$changeStream
->
next
();
$this
->
assertSame
(
1
,
$changeStream
->
key
());
}
public
function
testResumeTokenNotFoundAdvancesKey
()
{
$pipeline
=
[[
'$project'
=>
[
'_id'
=>
0
]]];
$operation
=
new
Watch
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$pipeline
,
$this
->
defaultOptions
);
$changeStream
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
/* Note: we intentionally do not start iteration with rewind() to ensure
* that we test extraction functionality within next(). */
$this
->
insertDocument
([
'x'
=>
1
]);
$this
->
insertDocument
([
'x'
=>
2
]);
$this
->
insertDocument
([
'x'
=>
3
]);
try
{
$changeStream
->
rewind
();
$this
->
fail
(
'ResumeTokenException was not thrown'
);
}
catch
(
ResumeTokenException
$e
)
{}
$this
->
assertSame
(
0
,
$changeStream
->
key
());
try
{
$changeStream
->
next
();
$this
->
fail
(
'ResumeTokenException was not thrown'
);
}
catch
(
ResumeTokenException
$e
)
{}
$this
->
assertSame
(
1
,
$changeStream
->
key
());
try
{
$changeStream
->
next
();
$this
->
fail
(
'ResumeTokenException was not thrown'
);
}
catch
(
ResumeTokenException
$e
)
{}
$this
->
assertSame
(
2
,
$changeStream
->
key
());
}
private
function
insertDocument
(
$document
)
{
$insertOne
=
new
InsertOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$document
);
...
...
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