Skip to content

Commit 4bc9dd1

Browse files
authored
PHPLIB-1230: Fix explain tests on MongoDB latest (#1155)
1 parent 02d07a9 commit 4bc9dd1

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

tests/Operation/AggregateFunctionalTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use MongoDB\Tests\Fixtures\Document\TestObject;
1414
use stdClass;
1515

16+
use function array_key_exists;
17+
use function array_key_first;
1618
use function current;
1719
use function iterator_to_array;
1820

@@ -182,9 +184,16 @@ public function testExplainOption(): void
182184

183185
$this->assertCount(1, $results);
184186

187+
$checkResult = $results[0];
188+
// Sharded clusters and load balanced servers list plans per shard
189+
if (array_key_exists('shards', $results[0])) {
190+
$firstShard = array_key_first((array) $results[0]['shards']);
191+
$checkResult = (array) $results[0]['shards']->$firstShard;
192+
}
193+
185194
/* MongoDB 4.2 may optimize aggregate pipelines into queries, which can
186195
* result in different explain output (see: SERVER-24860) */
187-
$this->assertThat($results[0], $this->logicalOr(
196+
$this->assertThat($checkResult, $this->logicalOr(
188197
$this->arrayHasKey('stages'),
189198
$this->arrayHasKey('queryPlanner'),
190199
));

tests/Operation/ExplainFunctionalTest.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use MongoDB\Operation\UpdateOne;
2222
use MongoDB\Tests\CommandObserver;
2323

24+
use function array_key_exists;
25+
use function array_key_first;
26+
2427
class ExplainFunctionalTest extends FunctionalTestCase
2528
{
2629
/** @dataProvider provideVerbosityInformation */
@@ -368,21 +371,28 @@ public function provideVerbosityInformation()
368371

369372
private function assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected, $stagesExpected = false): void
370373
{
374+
$checkResult = $result;
375+
376+
if (array_key_exists('shards', $result)) {
377+
$firstShard = array_key_first($result['shards']);
378+
$checkResult = $result['shards'][$firstShard];
379+
}
380+
371381
if ($stagesExpected) {
372-
$this->assertArrayHasKey('stages', $result);
382+
$this->assertArrayHasKey('stages', $checkResult);
373383
} else {
374-
$this->assertArrayHasKey('queryPlanner', $result);
384+
$this->assertArrayHasKey('queryPlanner', $checkResult);
375385
}
376386

377387
if ($executionStatsExpected) {
378-
$this->assertArrayHasKey('executionStats', $result);
388+
$this->assertArrayHasKey('executionStats', $checkResult);
379389
if ($allPlansExecutionExpected) {
380-
$this->assertArrayHasKey('allPlansExecution', $result['executionStats']);
390+
$this->assertArrayHasKey('allPlansExecution', $checkResult['executionStats']);
381391
} else {
382-
$this->assertArrayNotHasKey('allPlansExecution', $result['executionStats']);
392+
$this->assertArrayNotHasKey('allPlansExecution', $checkResult['executionStats']);
383393
}
384394
} else {
385-
$this->assertArrayNotHasKey('executionStats', $result);
395+
$this->assertArrayNotHasKey('executionStats', $checkResult);
386396
}
387397
}
388398

0 commit comments

Comments
 (0)