21
21
22
22
class WatchFunctionalTest extends FunctionalTestCase
23
23
{
24
+ private static $ wireVersionForStartAtOperationTime = 7 ;
25
+
24
26
private $ defaultOptions = ['maxAwaitTimeMS ' => 500 ];
25
27
26
28
public function setUp ()
@@ -134,6 +136,8 @@ function(array $event) use (&$commands) {
134
136
135
137
public function testResumeBeforeReceivingAnyResultsIncludesStartAtOperationTime ()
136
138
{
139
+ $ this ->skipIfStartAtOperationTimeNotSupported ();
140
+
137
141
$ operation = new Watch ($ this ->manager , $ this ->getDatabaseName (), $ this ->getCollectionName (), [], $ this ->defaultOptions );
138
142
139
143
$ operationTime = null ;
@@ -150,7 +154,9 @@ function (array $event) use (&$events) {
150
154
151
155
$ this ->assertCount (1 , $ events );
152
156
$ this ->assertSame ('aggregate ' , $ events [0 ]['started ' ]->getCommandName ());
153
- $ operationTime = $ events [0 ]['succeeded ' ]->getReply ()->operationTime ;
157
+ $ reply = $ events [0 ]['succeeded ' ]->getReply ();
158
+ $ this ->assertObjectHasAttribute ('operationTime ' , $ reply );
159
+ $ operationTime = $ reply ->operationTime ;
154
160
$ this ->assertInstanceOf (TimestampInterface::class, $ operationTime );
155
161
156
162
$ this ->assertNull ($ changeStream ->current ());
@@ -398,20 +404,29 @@ public function testResumeMultipleTimesInSuccession()
398
404
399
405
$ this ->insertDocument (['_id ' => 1 ]);
400
406
407
+ /* Insert a document and advance the change stream to ensure we capture
408
+ * a resume token. This is necessary when startAtOperationTime is not
409
+ * supported (i.e. 3.6 server version). */
410
+ $ changeStream ->next ();
411
+ $ this ->assertTrue ($ changeStream ->valid ());
412
+ $ this ->assertSame (0 , $ changeStream ->key ());
413
+
414
+ $ this ->insertDocument (['_id ' => 2 ]);
415
+
401
416
/* Killing the cursor and advancing when there is a result will test
402
417
* that next()'s resume attempt picks up the latest change. */
403
418
$ this ->killChangeStreamCursor ($ changeStream );
404
419
405
420
$ changeStream ->next ();
406
421
$ this ->assertTrue ($ changeStream ->valid ());
407
- $ this ->assertSame (0 , $ changeStream ->key ());
422
+ $ this ->assertSame (1 , $ changeStream ->key ());
408
423
409
424
$ expectedResult = [
410
425
'_id ' => $ changeStream ->current ()->_id ,
411
426
'operationType ' => 'insert ' ,
412
- 'fullDocument ' => ['_id ' => 1 ],
427
+ 'fullDocument ' => ['_id ' => 2 ],
413
428
'ns ' => ['db ' => $ this ->getDatabaseName (), 'coll ' => $ this ->getCollectionName ()],
414
- 'documentKey ' => ['_id ' => 1 ],
429
+ 'documentKey ' => ['_id ' => 2 ],
415
430
];
416
431
417
432
$ this ->assertMatchesDocument ($ expectedResult , $ changeStream ->current ());
@@ -424,48 +439,48 @@ public function testResumeMultipleTimesInSuccession()
424
439
425
440
$ changeStream ->rewind ();
426
441
$ this ->assertTrue ($ changeStream ->valid ());
427
- $ this ->assertSame (0 , $ changeStream ->key ());
442
+ $ this ->assertSame (1 , $ changeStream ->key ());
428
443
429
444
$ expectedResult = [
430
445
'_id ' => $ changeStream ->current ()->_id ,
431
446
'operationType ' => 'insert ' ,
432
- 'fullDocument ' => ['_id ' => 1 ],
447
+ 'fullDocument ' => ['_id ' => 2 ],
433
448
'ns ' => ['db ' => $ this ->getDatabaseName (), 'coll ' => $ this ->getCollectionName ()],
434
- 'documentKey ' => ['_id ' => 1 ],
449
+ 'documentKey ' => ['_id ' => 2 ],
435
450
];
436
451
437
452
$ this ->assertMatchesDocument ($ expectedResult , $ changeStream ->current ());
438
453
439
- $ this ->insertDocument (['_id ' => 2 ]);
454
+ $ this ->insertDocument (['_id ' => 3 ]);
440
455
441
456
$ changeStream ->next ();
442
457
$ this ->assertTrue ($ changeStream ->valid ());
443
- $ this ->assertSame (1 , $ changeStream ->key ());
458
+ $ this ->assertSame (2 , $ changeStream ->key ());
444
459
445
460
$ expectedResult = [
446
461
'_id ' => $ changeStream ->current ()->_id ,
447
462
'operationType ' => 'insert ' ,
448
- 'fullDocument ' => ['_id ' => 2 ],
463
+ 'fullDocument ' => ['_id ' => 3 ],
449
464
'ns ' => ['db ' => $ this ->getDatabaseName (), 'coll ' => $ this ->getCollectionName ()],
450
- 'documentKey ' => ['_id ' => 2 ],
465
+ 'documentKey ' => ['_id ' => 3 ],
451
466
];
452
467
453
468
$ this ->assertMatchesDocument ($ expectedResult , $ changeStream ->current ());
454
469
455
470
$ this ->killChangeStreamCursor ($ changeStream );
456
471
457
- $ this ->insertDocument (['_id ' => 3 ]);
472
+ $ this ->insertDocument (['_id ' => 4 ]);
458
473
459
474
$ changeStream ->next ();
460
475
$ this ->assertTrue ($ changeStream ->valid ());
461
- $ this ->assertSame (2 , $ changeStream ->key ());
476
+ $ this ->assertSame (3 , $ changeStream ->key ());
462
477
463
478
$ expectedResult = [
464
479
'_id ' => $ changeStream ->current ()->_id ,
465
480
'operationType ' => 'insert ' ,
466
- 'fullDocument ' => ['_id ' => 3 ],
481
+ 'fullDocument ' => ['_id ' => 4 ],
467
482
'ns ' => ['db ' => $ this ->getDatabaseName (), 'coll ' => $ this ->getCollectionName ()],
468
- 'documentKey ' => ['_id ' => 3 ],
483
+ 'documentKey ' => ['_id ' => 4 ],
469
484
];
470
485
471
486
$ this ->assertMatchesDocument ($ expectedResult , $ changeStream ->current ());
@@ -476,18 +491,18 @@ public function testResumeMultipleTimesInSuccession()
476
491
* we'll see {_id: 3} returned again. */
477
492
$ this ->killChangeStreamCursor ($ changeStream );
478
493
479
- $ this ->insertDocument (['_id ' => 4 ]);
494
+ $ this ->insertDocument (['_id ' => 5 ]);
480
495
481
496
$ changeStream ->next ();
482
497
$ this ->assertTrue ($ changeStream ->valid ());
483
- $ this ->assertSame (3 , $ changeStream ->key ());
498
+ $ this ->assertSame (4 , $ changeStream ->key ());
484
499
485
500
$ expectedResult = [
486
501
'_id ' => $ changeStream ->current ()->_id ,
487
502
'operationType ' => 'insert ' ,
488
- 'fullDocument ' => ['_id ' => 4 ],
503
+ 'fullDocument ' => ['_id ' => 5 ],
489
504
'ns ' => ['db ' => $ this ->getDatabaseName (), 'coll ' => $ this ->getCollectionName ()],
490
- 'documentKey ' => ['_id ' => 4 ],
505
+ 'documentKey ' => ['_id ' => 5 ],
491
506
];
492
507
493
508
$ this ->assertMatchesDocument ($ expectedResult , $ changeStream ->current ());
@@ -944,4 +959,11 @@ private function killChangeStreamCursor(ChangeStream $changeStream)
944
959
$ operation = new DatabaseCommand ($ this ->getDatabaseName (), $ command );
945
960
$ operation ->execute ($ this ->getPrimaryServer ());
946
961
}
962
+
963
+ private function skipIfStartAtOperationTimeNotSupported ()
964
+ {
965
+ if (!\MongoDB \server_supports_feature ($ this ->getPrimaryServer (), self ::$ wireVersionForStartAtOperationTime )) {
966
+ $ this ->markTestSkipped ('Operation time is not supported ' );
967
+ }
968
+ }
947
969
}
0 commit comments