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