Skip to content

Commit adbea29

Browse files
committed
Test behavior for multiple calls to ChangeStream::rewind()
1 parent 9f45714 commit adbea29

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/Operation/WatchFunctionalTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Driver\ReadPreference;
99
use MongoDB\Driver\Server;
1010
use MongoDB\Driver\Exception\ConnectionTimeoutException;
11+
use MongoDB\Driver\Exception\LogicException;
1112
use MongoDB\Exception\ResumeTokenException;
1213
use MongoDB\Operation\CreateCollection;
1314
use MongoDB\Operation\DatabaseCommand;
@@ -225,6 +226,62 @@ private function assertStartAtOperationTime(TimestampInterface $expectedOperatio
225226
$this->assertEquals($expectedOperationTime, $command->pipeline[0]->{'$changeStream'}->startAtOperationTime);
226227
}
227228

229+
public function testRewindMultipleTimesWithResults()
230+
{
231+
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
232+
$changeStream = $operation->execute($this->getPrimaryServer());
233+
234+
$this->insertDocument(['x' => 1]);
235+
$this->insertDocument(['x' => 2]);
236+
237+
$changeStream->rewind();
238+
$this->assertTrue($changeStream->valid());
239+
$this->assertSame(0, $changeStream->key());
240+
$this->assertNotNull($changeStream->current());
241+
242+
// Subsequent rewind does not change iterator state
243+
$changeStream->rewind();
244+
$this->assertTrue($changeStream->valid());
245+
$this->assertSame(0, $changeStream->key());
246+
$this->assertNotNull($changeStream->current());
247+
248+
$changeStream->next();
249+
250+
$this->assertTrue($changeStream->valid());
251+
$this->assertSame(1, $changeStream->key());
252+
$this->assertNotNull($changeStream->current());
253+
254+
// Rewinding after advancing the iterator is an error
255+
$this->expectException(LogicException::class);
256+
$changeStream->rewind();
257+
}
258+
259+
public function testRewindMultipleTimesWithNoResults()
260+
{
261+
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
262+
$changeStream = $operation->execute($this->getPrimaryServer());
263+
264+
$changeStream->rewind();
265+
$this->assertFalse($changeStream->valid());
266+
$this->assertNull($changeStream->key());
267+
$this->assertNull($changeStream->current());
268+
269+
// Subsequent rewind does not change iterator state
270+
$changeStream->rewind();
271+
$this->assertFalse($changeStream->valid());
272+
$this->assertNull($changeStream->key());
273+
$this->assertNull($changeStream->current());
274+
275+
$changeStream->next();
276+
$this->assertFalse($changeStream->valid());
277+
$this->assertNull($changeStream->key());
278+
$this->assertNull($changeStream->current());
279+
280+
// Rewinding after advancing the iterator is an error
281+
$this->expectException(LogicException::class);
282+
$changeStream->rewind();
283+
}
284+
228285
public function testRewindResumesAfterConnectionException()
229286
{
230287
/* In order to trigger a dropped connection, we'll use a new client with

0 commit comments

Comments
 (0)