|
8 | 8 | use MongoDB\Driver\ReadPreference;
|
9 | 9 | use MongoDB\Driver\Server;
|
10 | 10 | use MongoDB\Driver\Exception\ConnectionTimeoutException;
|
| 11 | +use MongoDB\Driver\Exception\LogicException; |
11 | 12 | use MongoDB\Exception\ResumeTokenException;
|
12 | 13 | use MongoDB\Operation\CreateCollection;
|
13 | 14 | use MongoDB\Operation\DatabaseCommand;
|
@@ -225,6 +226,62 @@ private function assertStartAtOperationTime(TimestampInterface $expectedOperatio
|
225 | 226 | $this->assertEquals($expectedOperationTime, $command->pipeline[0]->{'$changeStream'}->startAtOperationTime);
|
226 | 227 | }
|
227 | 228 |
|
| 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 | + |
228 | 285 | public function testRewindResumesAfterConnectionException()
|
229 | 286 | {
|
230 | 287 | /* In order to trigger a dropped connection, we'll use a new client with
|
|
0 commit comments