@@ -933,65 +933,93 @@ public function testChangeStreamExample_1_4()
933933 }
934934
935935 $ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
936+ $ db ->dropCollection ('inventory ' );
936937
937938 // Start Changestream Example 1
938939 $ changeStream = $ db ->inventory ->watch ();
940+ $ changeStream ->rewind ();
941+
942+ $ firstChange = $ changeStream ->current ();
943+
939944 $ changeStream ->next ();
940- $ document = $ changeStream ->current ();
945+
946+ $ secondChange = $ changeStream ->current ();
941947 // End Changestream Example 1
942948
943- $ this ->assertNull ($ document );
949+ $ this ->assertNull ($ firstChange );
950+ $ this ->assertNull ($ secondChange );
944951
945952 // Start Changestream Example 2
946953 $ changeStream = $ db ->inventory ->watch ([], ['fullDocument ' => \MongoDB \Operation \Watch::FULL_DOCUMENT_UPDATE_LOOKUP ]);
954+ $ changeStream ->rewind ();
955+
956+ $ firstChange = $ changeStream ->current ();
957+
947958 $ changeStream ->next ();
948- $ document = $ changeStream ->current ();
959+
960+ $ nextChange = $ changeStream ->current ();
949961 // End Changestream Example 2
950962
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 ());
952971
953- $ insertedResult = $ db ->inventory ->insertOne (['x ' => 1 ]);
954- $ insertedId = $ insertedResult ->getInsertedId ();
955972 $ changeStream ->next ();
956- $ document = $ changeStream ->current ();
973+ $ this ->assertTrue ($ changeStream ->valid ());
974+ $ lastChange = $ changeStream ->current ();
957975
958976 $ expectedChange = [
959- '_id ' => $ document ->_id ,
977+ '_id ' => $ lastChange ->_id ,
960978 'operationType ' => 'insert ' ,
961- 'fullDocument ' => ['_id ' => $ insertedId , 'x ' => 1 ],
979+ 'fullDocument ' => ['_id ' => 1 , 'x ' => ' foo ' ],
962980 'ns ' => ['db ' => $ this ->getDatabaseName (), 'coll ' => 'inventory ' ],
963- 'documentKey ' => ['_id ' => $ insertedId ],
981+ 'documentKey ' => ['_id ' => 1 ],
964982 ];
965983
966- $ this ->assertSameDocument ($ expectedChange , $ document );
984+ $ this ->assertSameDocument ($ expectedChange , $ lastChange );
967985
968986 // 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 ' );
973991 }
974- // End Changestream Example 3
975992
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
979998
980999 $ expectedChange = [
981- '_id ' => $ changeStream -> current () ->_id ,
1000+ '_id ' => $ nextChange ->_id ,
9821001 'operationType ' => 'insert ' ,
983- 'fullDocument ' => ['_id ' => $ insertedId , 'x ' => 2 ],
1002+ 'fullDocument ' => ['_id ' => 2 , 'x ' => ' bar ' ],
9841003 'ns ' => ['db ' => $ this ->getDatabaseName (), 'coll ' => 'inventory ' ],
985- 'documentKey ' => ['_id ' => $ insertedId ],
1004+ 'documentKey ' => ['_id ' => 2 ],
9861005 ];
9871006
988- $ this ->assertSameDocument ($ expectedChange , $ changeStream -> current () );
1007+ $ this ->assertSameDocument ($ expectedChange , $ nextChange );
9891008
9901009 // Start Changestream Example 4
9911010 $ pipeline = [['$match ' => ['$or ' => [['fullDocument.username ' => 'alice ' ], ['operationType ' => 'delete ' ]]]]];
9921011 $ changeStream = $ db ->inventory ->watch ($ pipeline );
1012+ $ changeStream ->rewind ();
1013+
1014+ $ firstChange = $ changeStream ->current ();
1015+
9931016 $ changeStream ->next ();
1017+
1018+ $ nextChange = $ changeStream ->current ();
9941019 // End Changestream Example 4
1020+
1021+ $ this ->assertNull ($ firstChange );
1022+ $ this ->assertNull ($ nextChange );
9951023 }
9961024
9971025 /**
0 commit comments