Skip to content

PHPLIB-1230: Fix explain tests on MongoDB latest #1155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion tests/Operation/AggregateFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use MongoDB\Tests\Fixtures\Document\TestObject;
use stdClass;

use function array_key_exists;
use function array_key_first;
use function current;
use function iterator_to_array;

Expand Down Expand Up @@ -182,9 +184,16 @@ public function testExplainOption(): void

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

$checkResult = $results[0];
// Sharded clusters and load balanced servers list plans per shard
if (array_key_exists('shards', $results[0])) {
$firstShard = array_key_first((array) $results[0]['shards']);
$checkResult = (array) $results[0]['shards']->$firstShard;
}

/* MongoDB 4.2 may optimize aggregate pipelines into queries, which can
* result in different explain output (see: SERVER-24860) */
$this->assertThat($results[0], $this->logicalOr(
$this->assertThat($checkResult, $this->logicalOr(
$this->arrayHasKey('stages'),
$this->arrayHasKey('queryPlanner'),
));
Expand Down
22 changes: 16 additions & 6 deletions tests/Operation/ExplainFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use MongoDB\Operation\UpdateOne;
use MongoDB\Tests\CommandObserver;

use function array_key_exists;
use function array_key_first;

class ExplainFunctionalTest extends FunctionalTestCase
{
/** @dataProvider provideVerbosityInformation */
Expand Down Expand Up @@ -368,21 +371,28 @@ public function provideVerbosityInformation()

private function assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected, $stagesExpected = false): void
{
$checkResult = $result;

if (array_key_exists('shards', $result)) {
$firstShard = array_key_first($result['shards']);
$checkResult = $result['shards'][$firstShard];
}

if ($stagesExpected) {
$this->assertArrayHasKey('stages', $result);
$this->assertArrayHasKey('stages', $checkResult);
} else {
$this->assertArrayHasKey('queryPlanner', $result);
$this->assertArrayHasKey('queryPlanner', $checkResult);
}

if ($executionStatsExpected) {
$this->assertArrayHasKey('executionStats', $result);
$this->assertArrayHasKey('executionStats', $checkResult);
if ($allPlansExecutionExpected) {
$this->assertArrayHasKey('allPlansExecution', $result['executionStats']);
$this->assertArrayHasKey('allPlansExecution', $checkResult['executionStats']);
} else {
$this->assertArrayNotHasKey('allPlansExecution', $result['executionStats']);
$this->assertArrayNotHasKey('allPlansExecution', $checkResult['executionStats']);
}
} else {
$this->assertArrayNotHasKey('executionStats', $result);
$this->assertArrayNotHasKey('executionStats', $checkResult);
}
}

Expand Down