|
3 | 3 | namespace MongoDB\Tests\Operation;
|
4 | 4 |
|
5 | 5 | use MongoDB\ChangeStream;
|
6 |
| -use MongoDB\Client; |
7 | 6 | use MongoDB\Driver\Manager;
|
8 | 7 | use MongoDB\Driver\ReadPreference;
|
9 | 8 | use MongoDB\Driver\Server;
|
10 | 9 | use MongoDB\Driver\Exception\ConnectionTimeoutException;
|
| 10 | +use MongoDB\Exception\ResumeTokenException; |
11 | 11 | use MongoDB\Operation\DatabaseCommand;
|
12 | 12 | use MongoDB\Operation\InsertOne;
|
13 | 13 | use MongoDB\Operation\Watch;
|
@@ -536,6 +536,58 @@ public function provideTypeMapOptionsAndExpectedChangeDocument()
|
536 | 536 | ];
|
537 | 537 | }
|
538 | 538 |
|
| 539 | + public function testNextAdvancesKey() |
| 540 | + { |
| 541 | + $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions); |
| 542 | + $changeStream = $operation->execute($this->getPrimaryServer()); |
| 543 | + |
| 544 | + $this->insertDocument(['x' => 1]); |
| 545 | + $this->insertDocument(['x' => 2]); |
| 546 | + |
| 547 | + $changeStream->next(); |
| 548 | + |
| 549 | + $this->assertSame(0, $changeStream->key()); |
| 550 | + |
| 551 | + $changeStream->next(); |
| 552 | + |
| 553 | + $this->assertSame(1, $changeStream->key()); |
| 554 | + } |
| 555 | + |
| 556 | + public function testResumeTokenNotFoundAdvancesKey() |
| 557 | + { |
| 558 | + $pipeline = [['$project' => ['_id' => 0 ]]]; |
| 559 | + |
| 560 | + $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, $this->defaultOptions); |
| 561 | + $changeStream = $operation->execute($this->getPrimaryServer()); |
| 562 | + |
| 563 | + /* Note: we intentionally do not start iteration with rewind() to ensure |
| 564 | + * that we test extraction functionality within next(). */ |
| 565 | + $this->insertDocument(['x' => 1]); |
| 566 | + $this->insertDocument(['x' => 2]); |
| 567 | + $this->insertDocument(['x' => 3]); |
| 568 | + |
| 569 | + try { |
| 570 | + $changeStream->rewind(); |
| 571 | + $this->fail('ResumeTokenException was not thrown'); |
| 572 | + } catch (ResumeTokenException $e) {} |
| 573 | + |
| 574 | + $this->assertSame(0, $changeStream->key()); |
| 575 | + |
| 576 | + try { |
| 577 | + $changeStream->next(); |
| 578 | + $this->fail('ResumeTokenException was not thrown'); |
| 579 | + } catch (ResumeTokenException $e) {} |
| 580 | + |
| 581 | + $this->assertSame(1, $changeStream->key()); |
| 582 | + |
| 583 | + try { |
| 584 | + $changeStream->next(); |
| 585 | + $this->fail('ResumeTokenException was not thrown'); |
| 586 | + } catch (ResumeTokenException $e) {} |
| 587 | + |
| 588 | + $this->assertSame(2, $changeStream->key()); |
| 589 | + } |
| 590 | + |
539 | 591 | private function insertDocument($document)
|
540 | 592 | {
|
541 | 593 | $insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document);
|
|
0 commit comments