@@ -933,65 +933,93 @@ public function testChangeStreamExample_1_4()
933
933
}
934
934
935
935
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
936
+ $ db ->dropCollection ('inventory ' );
936
937
937
938
// Start Changestream Example 1
938
939
$ changeStream = $ db ->inventory ->watch ();
940
+ $ changeStream ->rewind ();
941
+
942
+ $ firstChange = $ changeStream ->current ();
943
+
939
944
$ changeStream ->next ();
940
- $ document = $ changeStream ->current ();
945
+
946
+ $ secondChange = $ changeStream ->current ();
941
947
// End Changestream Example 1
942
948
943
- $ this ->assertNull ($ document );
949
+ $ this ->assertNull ($ firstChange );
950
+ $ this ->assertNull ($ secondChange );
944
951
945
952
// Start Changestream Example 2
946
953
$ changeStream = $ db ->inventory ->watch ([], ['fullDocument ' => \MongoDB \Operation \Watch::FULL_DOCUMENT_UPDATE_LOOKUP ]);
954
+ $ changeStream ->rewind ();
955
+
956
+ $ firstChange = $ changeStream ->current ();
957
+
947
958
$ changeStream ->next ();
948
- $ document = $ changeStream ->current ();
959
+
960
+ $ nextChange = $ changeStream ->current ();
949
961
// End Changestream Example 2
950
962
951
- $ this ->assertNull ($ document );
963
+ $ this ->assertNull ($ firstChange );
964
+ $ this ->assertNull ($ nextChange );
965
+
966
+ $ insertManyResult = $ db ->inventory ->insertMany ([
967
+ ['_id ' => 1 , 'x ' => 'foo ' ],
968
+ ['_id ' => 2 , 'x ' => 'bar ' ],
969
+ ]);
970
+ $ this ->assertEquals (2 , $ insertManyResult ->getInsertedCount ());
952
971
953
- $ insertedResult = $ db ->inventory ->insertOne (['x ' => 1 ]);
954
- $ insertedId = $ insertedResult ->getInsertedId ();
955
972
$ changeStream ->next ();
956
- $ document = $ changeStream ->current ();
973
+ $ this ->assertTrue ($ changeStream ->valid ());
974
+ $ lastChange = $ changeStream ->current ();
957
975
958
976
$ expectedChange = [
959
- '_id ' => $ document ->_id ,
977
+ '_id ' => $ lastChange ->_id ,
960
978
'operationType ' => 'insert ' ,
961
- 'fullDocument ' => ['_id ' => $ insertedId , 'x ' => 1 ],
979
+ 'fullDocument ' => ['_id ' => 1 , 'x ' => ' foo ' ],
962
980
'ns ' => ['db ' => $ this ->getDatabaseName (), 'coll ' => 'inventory ' ],
963
- 'documentKey ' => ['_id ' => $ insertedId ],
981
+ 'documentKey ' => ['_id ' => 1 ],
964
982
];
965
983
966
- $ this ->assertSameDocument ($ expectedChange , $ document );
984
+ $ this ->assertSameDocument ($ expectedChange , $ lastChange );
967
985
968
986
// Start Changestream Example 3
969
- $ resumeToken = ($ document !== null ) ? $ document ->_id : null ;
970
- if ( $ resumeToken !== null ) {
971
- $ changeStream = $ db -> inventory -> watch ([], [ ' resumeAfter ' => $ resumeToken ]);
972
- $ changeStream -> next ( );
987
+ $ resumeToken = ($ lastChange !== null ) ? $ lastChange ->_id : null ;
988
+
989
+ if ( $ resumeToken === null ) {
990
+ throw new \ Exception ( ' resumeToken was not found ' );
973
991
}
974
- // End Changestream Example 3
975
992
976
- $ insertedResult = $ db ->inventory ->insertOne (['x ' => 2 ]);
977
- $ insertedId = $ insertedResult ->getInsertedId ();
978
- $ changeStream ->next ();
993
+ $ changeStream = $ db ->inventory ->watch ([], ['resumeAfter ' => $ resumeToken ]);
994
+ $ changeStream ->rewind ();
995
+
996
+ $ nextChange = $ changeStream ->current ();
997
+ // End Changestream Example 3
979
998
980
999
$ expectedChange = [
981
- '_id ' => $ changeStream -> current () ->_id ,
1000
+ '_id ' => $ nextChange ->_id ,
982
1001
'operationType ' => 'insert ' ,
983
- 'fullDocument ' => ['_id ' => $ insertedId , 'x ' => 2 ],
1002
+ 'fullDocument ' => ['_id ' => 2 , 'x ' => ' bar ' ],
984
1003
'ns ' => ['db ' => $ this ->getDatabaseName (), 'coll ' => 'inventory ' ],
985
- 'documentKey ' => ['_id ' => $ insertedId ],
1004
+ 'documentKey ' => ['_id ' => 2 ],
986
1005
];
987
1006
988
- $ this ->assertSameDocument ($ expectedChange , $ changeStream -> current () );
1007
+ $ this ->assertSameDocument ($ expectedChange , $ nextChange );
989
1008
990
1009
// Start Changestream Example 4
991
1010
$ pipeline = [['$match ' => ['$or ' => [['fullDocument.username ' => 'alice ' ], ['operationType ' => 'delete ' ]]]]];
992
1011
$ changeStream = $ db ->inventory ->watch ($ pipeline );
1012
+ $ changeStream ->rewind ();
1013
+
1014
+ $ firstChange = $ changeStream ->current ();
1015
+
993
1016
$ changeStream ->next ();
1017
+
1018
+ $ nextChange = $ changeStream ->current ();
994
1019
// End Changestream Example 4
1020
+
1021
+ $ this ->assertNull ($ firstChange );
1022
+ $ this ->assertNull ($ nextChange );
995
1023
}
996
1024
997
1025
/**
0 commit comments