99use MongoDB \Operation \Find ;
1010use MongoDB \Operation \MapReduce ;
1111use MongoDB \Tests \CommandObserver ;
12+ use function is_object ;
1213use function iterator_to_array ;
14+ use function usort ;
1315use function version_compare ;
1416
1517class MapReduceFunctionalTest extends FunctionalTestCase
@@ -92,12 +94,19 @@ public function testResult()
9294 $ result = $ operation ->execute ($ this ->getPrimaryServer ());
9395
9496 $ this ->assertInstanceOf (MapReduceResult::class, $ result );
95- $ this ->assertGreaterThanOrEqual (0 , $ result ->getExecutionTimeMS ());
96- $ this ->assertNotEmpty ($ result ->getCounts ());
97+
98+ if (version_compare ($ this ->getServerVersion (), '4.3.0 ' , '< ' )) {
99+ $ this ->assertGreaterThanOrEqual (0 , $ result ->getExecutionTimeMS ());
100+ $ this ->assertNotEmpty ($ result ->getCounts ());
101+ }
97102 }
98103
99104 public function testResultIncludesTimingWithVerboseOption ()
100105 {
106+ if (version_compare ($ this ->getServerVersion (), '4.3.0 ' , '>= ' )) {
107+ $ this ->markTestSkipped ('mapReduce statistics are no longer exposed ' );
108+ }
109+
101110 $ this ->createFixtures (3 );
102111
103112 $ map = new Javascript ('function() { emit(this.x, this.y); } ' );
@@ -115,6 +124,10 @@ public function testResultIncludesTimingWithVerboseOption()
115124
116125 public function testResultDoesNotIncludeTimingWithoutVerboseOption ()
117126 {
127+ if (version_compare ($ this ->getServerVersion (), '4.3.0 ' , '>= ' )) {
128+ $ this ->markTestSkipped ('mapReduce statistics are no longer exposed ' );
129+ }
130+
118131 $ this ->createFixtures (3 );
119132
120133 $ map = new Javascript ('function() { emit(this.x, this.y); } ' );
@@ -226,7 +239,7 @@ public function testTypeMapOptionWithInlineResults(array $typeMap = null, array
226239 $ operation = new MapReduce ($ this ->getDatabaseName (), $ this ->getCollectionName (), $ map , $ reduce , $ out , ['typeMap ' => $ typeMap ]);
227240 $ results = iterator_to_array ($ operation ->execute ($ this ->getPrimaryServer ()));
228241
229- $ this ->assertEquals ($ expectedDocuments , $ results );
242+ $ this ->assertEquals ($ this -> sortResults ( $ expectedDocuments) , $ this -> sortResults ( $ results) );
230243 }
231244
232245 public function provideTypeMapOptionsAndExpectedDocuments ()
@@ -273,12 +286,12 @@ public function testTypeMapOptionWithOutputCollection(array $typeMap = null, arr
273286 $ operation = new MapReduce ($ this ->getDatabaseName (), $ this ->getCollectionName (), $ map , $ reduce , $ out , ['typeMap ' => $ typeMap ]);
274287 $ results = iterator_to_array ($ operation ->execute ($ this ->getPrimaryServer ()));
275288
276- $ this ->assertEquals ($ expectedDocuments , $ results );
289+ $ this ->assertEquals ($ this -> sortResults ( $ expectedDocuments) , $ this -> sortResults ( $ results) );
277290
278291 $ operation = new Find ($ this ->getDatabaseName (), $ out , [], ['typeMap ' => $ typeMap ]);
279292 $ cursor = $ operation ->execute ($ this ->getPrimaryServer ());
280293
281- $ this ->assertEquals ($ expectedDocuments, iterator_to_array ($ cursor ));
294+ $ this ->assertEquals ($ this -> sortResults ( $ expectedDocuments), $ this -> sortResults ( iterator_to_array ($ cursor) ));
282295
283296 $ operation = new DropCollection ($ this ->getDatabaseName (), $ out );
284297 $ operation ->execute ($ this ->getPrimaryServer ());
@@ -302,4 +315,19 @@ private function createFixtures($n)
302315
303316 $ this ->assertEquals ($ n * 2 , $ result ->getInsertedCount ());
304317 }
318+
319+ private function sortResults (array $ results ) : array
320+ {
321+ $ sortFunction = static function ($ resultA , $ resultB ) : int {
322+ $ idA = is_object ($ resultA ) ? $ resultA ->_id : $ resultA ['_id ' ];
323+ $ idB = is_object ($ resultB ) ? $ resultB ->_id : $ resultB ['_id ' ];
324+
325+ return $ idA <=> $ idB ;
326+ };
327+
328+ $ sortedResults = $ results ;
329+ usort ($ sortedResults , $ sortFunction );
330+
331+ return $ sortedResults ;
332+ }
305333}
0 commit comments