Skip to content

Commit

Permalink
PHPLIB-1153: Add helper FunctionalTestCase::skipIfServerVersion (#1107)
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN authored Jun 21, 2023
1 parent 53052b6 commit 5a14dee
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 153 deletions.
28 changes: 6 additions & 22 deletions tests/Collection/CrudSpecFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use MongoDB\Operation\FindOneAndReplace;
use MongoDB\UpdateResult;
use MultipleIterator;
use PHPUnit_Framework_SkippedTestError;

use function array_diff_key;
use function array_key_exists;
Expand All @@ -25,7 +24,6 @@
use function sprintf;
use function str_replace;
use function strtolower;
use function version_compare;

/**
* CRUD spec functional tests.
Expand Down Expand Up @@ -54,8 +52,12 @@ public function setUp(): void
/** @dataProvider provideSpecificationTests */
public function testSpecification(array $initialData, array $test, $minServerVersion, $maxServerVersion, $serverless): void
{
if (isset($minServerVersion) || isset($maxServerVersion)) {
$this->checkServerVersion($minServerVersion, $maxServerVersion);
if (isset($minServerVersion)) {
$this->skipIfServerVersion('<', $minServerVersion);
}

if (isset($maxServerVersion)) {
$this->skipIfServerVersion('>=', $maxServerVersion);
}

$this->checkServerlessRequirement($serverless);
Expand Down Expand Up @@ -143,24 +145,6 @@ private function checkServerlessRequirement(?string $serverless): void
}
}

/**
* Checks that the server version is within the allowed bounds (if any).
*
* @throws PHPUnit_Framework_SkippedTestError
*/
private function checkServerVersion(?string $minServerVersion, ?string $maxServerVersion): void
{
$serverVersion = $this->getServerVersion();

if (isset($minServerVersion) && version_compare($serverVersion, $minServerVersion, '<')) {
$this->markTestSkipped(sprintf('Server version "%s" < minServerVersion "%s"', $serverVersion, $minServerVersion));
}

if (isset($maxServerVersion) && version_compare($serverVersion, $maxServerVersion, '>=')) {
$this->markTestSkipped(sprintf('Server version "%s" >= maxServerVersion "%s"', $serverVersion, $maxServerVersion));
}
}

/**
* Executes an "operation" block.
*
Expand Down
18 changes: 4 additions & 14 deletions tests/DocumentationExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use function ob_start;
use function usleep;
use function var_dump;
use function version_compare;

/**
* Documentation examples to be parsed for inclusion in the MongoDB manual.
Expand Down Expand Up @@ -1549,9 +1548,7 @@ public function testCausalConsistency(): void

public function testSnapshotQueries(): void
{
if (version_compare($this->getServerVersion(), '5.0.0', '<')) {
$this->markTestSkipped('Snapshot queries outside of transactions are not supported');
}
$this->skipIfServerVersion('<', '5.0.0', 'Snapshot queries outside of transactions are not supported');

if (! ($this->isReplicaSet() || $this->isShardedClusterUsingReplicasets())) {
$this->markTestSkipped('Snapshot read concern is only supported with replicasets');
Expand Down Expand Up @@ -1682,13 +1679,8 @@ public function testVersionedApi(): void

public function testVersionedApiMigration(): void
{
if (version_compare($this->getServerVersion(), '5.0.0', '<')) {
$this->markTestSkipped('Versioned API is not supported');
}

if (version_compare($this->getServerVersion(), '5.0.9', '>=')) {
$this->markTestSkipped('The count command was added to API version 1 (SERVER-63850)');
}
$this->skipIfServerVersion('<', '5.0.0', 'Versioned API is not supported');
$this->skipIfServerVersion('>=', '5.0.9', 'The count command was added to API version 1 (SERVER-63850)');

$this->dropCollection($this->getDatabaseName(), 'sales');
$uriString = static::getUri(true);
Expand Down Expand Up @@ -1817,9 +1809,7 @@ public function testQueryableEncryption(): void
$this->markTestSkipped('Queryable encryption requires replica sets');
}

if (version_compare($this->getServerVersion(), '7.0.0', '<')) {
$this->markTestSkipped('Explicit encryption tests require MongoDB 7.0 or later');
}
$this->skipIfServerVersion('<', '7.0.0', 'Explicit encryption tests require MongoDB 7.0 or later');

if (! $this->isEnterprise()) {
$this->markTestSkipped('Automatic encryption requires MongoDB Enterprise');
Expand Down
7 changes: 7 additions & 0 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,13 @@ protected function isShardedClusterUsingReplicasets()
return preg_match('@^.*/.*:\d+@', $document['host']);
}

protected function skipIfServerVersion(string $operator, string $version, ?string $message = null): void
{
if (version_compare($this->getServerVersion(), $version, $operator)) {
$this->markTestSkipped($message ?? sprintf('Server version is %s %s', $operator, $version));
}
}

protected function skipIfChangeStreamIsNotSupported(): void
{
switch ($this->getPrimaryServer()->getType()) {
Expand Down
9 changes: 3 additions & 6 deletions tests/Operation/BulkWriteFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use stdClass;

use function is_array;
use function version_compare;

class BulkWriteFunctionalTest extends FunctionalTestCase
{
Expand Down Expand Up @@ -200,8 +199,8 @@ function (array $event) use ($expectedReplacement): void {
*/
public function testUpdateDocuments($update, $expectedUpdate): void
{
if (is_array($expectedUpdate) && version_compare($this->getServerVersion(), '4.2.0', '<')) {
$this->markTestSkipped('Pipeline-style updates are not supported');
if (is_array($expectedUpdate)) {
$this->skipIfServerVersion('<', '4.2.0', 'Pipeline-style updates are not supported');
}

(new CommandObserver())->observe(
Expand Down Expand Up @@ -425,9 +424,7 @@ function (array $event): void {

public function testBulkWriteWithPipelineUpdates(): void
{
if (version_compare($this->getServerVersion(), '4.2.0', '<')) {
$this->markTestSkipped('Pipeline-style updates are not supported');
}
$this->skipIfServerVersion('<', '4.2.0', 'Pipeline-style updates are not supported');

$this->createFixtures(4);

Expand Down
5 changes: 1 addition & 4 deletions tests/Operation/CreateEncryptedCollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use function getenv;
use function is_executable;
use function is_readable;
use function version_compare;

use const DIRECTORY_SEPARATOR;
use const PATH_SEPARATOR;
Expand All @@ -42,9 +41,7 @@ public function setUp(): void
$this->markTestSkipped('Queryable Encryption requires replica sets');
}

if (version_compare($this->getServerVersion(), '6.0.0', '<')) {
$this->markTestSkipped('Queryable Encryption requires MongoDB 6.0 or later');
}
$this->skipIfServerVersion('<', '6.0.0', 'Queryable Encryption requires MongoDB 6.0 or later');

$client = static::createTestClient();

Expand Down
9 changes: 2 additions & 7 deletions tests/Operation/CreateIndexesFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use function call_user_func;
use function is_callable;
use function sprintf;
use function version_compare;

class CreateIndexesFunctionalTest extends FunctionalTestCase
{
Expand Down Expand Up @@ -189,9 +188,7 @@ function (array $event): void {

public function testCommitQuorumOption(): void
{
if (version_compare($this->getServerVersion(), '4.3.4', '<')) {
$this->markTestSkipped('commitQuorum is not supported');
}
$this->skipIfServerVersion('<', '4.3.4', 'commitQuorum is not supported');

if ($this->getPrimaryServer()->getType() !== Server::TYPE_RS_PRIMARY) {
$this->markTestSkipped('commitQuorum is only supported on replica sets');
Expand All @@ -216,9 +213,7 @@ function (array $event): void {

public function testCommitQuorumUnsupported(): void
{
if (version_compare($this->getServerVersion(), '4.3.4', '>=')) {
$this->markTestSkipped('commitQuorum is supported');
}
$this->skipIfServerVersion('>=', '4.3.4', 'commitQuorum is supported');

$operation = new CreateIndexes(
$this->getDatabaseName(),
Expand Down
6 changes: 1 addition & 5 deletions tests/Operation/DeleteFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use MongoDB\Tests\CommandObserver;
use stdClass;

use function version_compare;

class DeleteFunctionalTest extends FunctionalTestCase
{
/** @var Collection */
Expand Down Expand Up @@ -87,9 +85,7 @@ public function testDeleteMany(): void

public function testHintOptionAndUnacknowledgedWriteConcernUnsupportedClientSideError(): void
{
if (version_compare($this->getServerVersion(), '4.4.0', '>=')) {
$this->markTestSkipped('hint is supported');
}
$this->skipIfServerVersion('>=', '4.4.0', 'hint is supported');

$operation = new Delete(
$this->getDatabaseName(),
Expand Down
10 changes: 2 additions & 8 deletions tests/Operation/ExplainFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
use MongoDB\Operation\UpdateOne;
use MongoDB\Tests\CommandObserver;

use function version_compare;

class ExplainFunctionalTest extends FunctionalTestCase
{
/** @dataProvider provideVerbosityInformation */
Expand Down Expand Up @@ -329,9 +327,7 @@ public function testUpdateOne($verbosity, $executionStatsExpected, $allPlansExec

public function testAggregate(): void
{
if (version_compare($this->getServerVersion(), '4.0.0', '<')) {
$this->markTestSkipped('Explaining aggregate command requires server version >= 4.0');
}
$this->skipIfServerVersion('<', '4.0.0', 'Explaining aggregate command requires server version >= 4.0');

$this->createFixtures(3);

Expand All @@ -348,9 +344,7 @@ public function testAggregate(): void
/** @dataProvider provideVerbosityInformation */
public function testAggregateOptimizedToQuery($verbosity, $executionStatsExpected, $allPlansExecutionExpected): void
{
if (version_compare($this->getServerVersion(), '4.2.0', '<')) {
$this->markTestSkipped('MongoDB < 4.2 does not optimize simple aggregation pipelines');
}
$this->skipIfServerVersion('<', '4.2.0', 'MongoDB < 4.2 does not optimize simple aggregation pipelines');

$this->createFixtures(3);

Expand Down
10 changes: 2 additions & 8 deletions tests/Operation/FindAndModifyFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use MongoDB\Tests\CommandObserver;
use stdClass;

use function version_compare;

class FindAndModifyFunctionalTest extends FunctionalTestCase
{
/** @dataProvider provideQueryDocuments */
Expand Down Expand Up @@ -129,9 +127,7 @@ function (array $event): void {

public function testHintOptionUnsupportedClientSideError(): void
{
if (version_compare($this->getServerVersion(), '4.2.0', '>=')) {
$this->markTestSkipped('server reports error for unsupported findAndModify options');
}
$this->skipIfServerVersion('>=', '4.2.0', 'server reports error for unsupported findAndModify options');

$operation = new FindAndModify(
$this->getDatabaseName(),
Expand All @@ -147,9 +143,7 @@ public function testHintOptionUnsupportedClientSideError(): void

public function testHintOptionAndUnacknowledgedWriteConcernUnsupportedClientSideError(): void
{
if (version_compare($this->getServerVersion(), '4.4.0', '>=')) {
$this->markTestSkipped('hint is supported');
}
$this->skipIfServerVersion('>=', '4.4.0', 'hint is supported');

$operation = new FindAndModify(
$this->getDatabaseName(),
Expand Down
8 changes: 2 additions & 6 deletions tests/Operation/MapReduceFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ public function testResult(): void

public function testResultIncludesTimingWithVerboseOption(): void
{
if (version_compare($this->getServerVersion(), '4.3.0', '>=')) {
$this->markTestSkipped('mapReduce statistics are no longer exposed');
}
$this->skipIfServerVersion('>=', '4.3.0', 'mapReduce statistics are no longer exposed');

$this->createFixtures(3);

Expand All @@ -128,9 +126,7 @@ public function testResultIncludesTimingWithVerboseOption(): void

public function testResultDoesNotIncludeTimingWithoutVerboseOption(): void
{
if (version_compare($this->getServerVersion(), '4.3.0', '>=')) {
$this->markTestSkipped('mapReduce statistics are no longer exposed');
}
$this->skipIfServerVersion('>=', '4.3.0', 'mapReduce statistics are no longer exposed');

$this->createFixtures(3);

Expand Down
9 changes: 3 additions & 6 deletions tests/Operation/UpdateFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use stdClass;

use function is_array;
use function version_compare;

class UpdateFunctionalTest extends FunctionalTestCase
{
Expand Down Expand Up @@ -56,8 +55,8 @@ function (array $event) use ($expectedFilter): void {
*/
public function testUpdateDocuments($update, $expectedUpdate): void
{
if (is_array($expectedUpdate) && version_compare($this->getServerVersion(), '4.2.0', '<')) {
$this->markTestSkipped('Pipeline-style updates are not supported');
if (is_array($expectedUpdate)) {
$this->skipIfServerVersion('<', '4.2.0', 'Pipeline-style updates are not supported');
}

(new CommandObserver())->observe(
Expand Down Expand Up @@ -152,9 +151,7 @@ function (array $event): void {

public function testHintOptionAndUnacknowledgedWriteConcernUnsupportedClientSideError(): void
{
if (version_compare($this->getServerVersion(), '4.2.0', '>=')) {
$this->markTestSkipped('hint is supported');
}
$this->skipIfServerVersion('>=', '4.2.0', 'hint is supported');

$operation = new Update(
$this->getDatabaseName(),
Expand Down
Loading

0 comments on commit 5a14dee

Please sign in to comment.