Commit e847b06c authored by Katherine Walker's avatar Katherine Walker

PHPLIB-327: ChangeStream::next() should not increment key for the first event

parent d42c3b4a
...@@ -38,7 +38,8 @@ class ChangeStream implements Iterator ...@@ -38,7 +38,8 @@ class ChangeStream implements Iterator
private $resumeToken; private $resumeToken;
private $resumeCallable; private $resumeCallable;
private $csIt; private $csIt;
private $key; private $key = 0;
private $hasAdvanced = false;
const CURSOR_NOT_FOUND = 43; const CURSOR_NOT_FOUND = 43;
...@@ -53,8 +54,6 @@ class ChangeStream implements Iterator ...@@ -53,8 +54,6 @@ class ChangeStream implements Iterator
{ {
$this->resumeCallable = $resumeCallable; $this->resumeCallable = $resumeCallable;
$this->csIt = new IteratorIterator($cursor); $this->csIt = new IteratorIterator($cursor);
$this->key = 0;
} }
/** /**
...@@ -97,7 +96,10 @@ class ChangeStream implements Iterator ...@@ -97,7 +96,10 @@ class ChangeStream implements Iterator
$this->csIt->next(); $this->csIt->next();
if ($this->valid()) { if ($this->valid()) {
$this->resumeToken = $this->extractResumeToken($this->csIt->current()); $this->resumeToken = $this->extractResumeToken($this->csIt->current());
$this->key++; if ($this->hasAdvanced) {
$this->key++;
}
$this->hasAdvanced = true;
} }
} catch (RuntimeException $e) { } catch (RuntimeException $e) {
if (strpos($e->getMessage(), "not master") !== false) { if (strpos($e->getMessage(), "not master") !== false) {
...@@ -126,6 +128,7 @@ class ChangeStream implements Iterator ...@@ -126,6 +128,7 @@ class ChangeStream implements Iterator
$this->csIt->rewind(); $this->csIt->rewind();
if ($this->valid()) { if ($this->valid()) {
$this->resumeToken = $this->extractResumeToken($this->csIt->current()); $this->resumeToken = $this->extractResumeToken($this->csIt->current());
$this->hasAdvanced = true;
} }
} catch (RuntimeException $e) { } catch (RuntimeException $e) {
if (strpos($e->getMessage(), "not master") !== false) { if (strpos($e->getMessage(), "not master") !== false) {
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\ChangeStream; use MongoDB\ChangeStream;
use MongoDB\Client;
use MongoDB\Driver\Manager; use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server; use MongoDB\Driver\Server;
...@@ -536,6 +535,23 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -536,6 +535,23 @@ 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());
}
private function insertDocument($document) private function insertDocument($document)
{ {
$insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document); $insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document);
......
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