diff --git a/tests/DocumentationExamplesTest.php b/tests/DocumentationExamplesTest.php index 001057454..af336f19a 100644 --- a/tests/DocumentationExamplesTest.php +++ b/tests/DocumentationExamplesTest.php @@ -1810,10 +1810,7 @@ public function testQueryableEncryption(): void } $this->skipIfServerVersion('<', '7.0.0', 'Explicit encryption tests require MongoDB 7.0 or later'); - - if (! $this->isEnterprise()) { - $this->markTestSkipped('Automatic encryption requires MongoDB Enterprise'); - } + $this->skipIfClientSideEncryptionIsNotSupported(); // Fetch names for the database and collection under test $collectionName = $this->getCollectionName(); diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index b4d4fb5ab..cc4df8ef9 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -28,10 +28,11 @@ use function filter_var; use function getenv; use function implode; -use function in_array; use function is_array; use function is_callable; +use function is_executable; use function is_object; +use function is_readable; use function is_string; use function key; use function ob_get_clean; @@ -44,8 +45,10 @@ use function sprintf; use function version_compare; +use const DIRECTORY_SEPARATOR; use const FILTER_VALIDATE_BOOLEAN; use const INFO_MODULES; +use const PATH_SEPARATOR; abstract class FunctionalTestCase extends TestCase { @@ -397,20 +400,6 @@ protected function isApiVersionRequired(): bool return isset($document->requireApiVersion) && $document->requireApiVersion === true; } - protected function isEnterprise(): bool - { - $buildInfo = $this->getPrimaryServer()->executeCommand( - $this->getDatabaseName(), - new Command(['buildInfo' => 1]) - )->toArray()[0]; - - if (isset($buildInfo->modules) && is_array($buildInfo->modules)) { - return in_array('enterprise', $buildInfo->modules); - } - - throw new UnexpectedValueException('Could not determine server modules'); - } - protected function isLoadBalanced() { return $this->getPrimaryServer()->getType() == Server::TYPE_LOAD_BALANCER; @@ -542,6 +531,10 @@ protected function skipIfClientSideEncryptionIsNotSupported(): void if (static::getModuleInfo('libmongocrypt') === 'disabled') { $this->markTestSkipped('Client Side Encryption is not enabled in the MongoDB extension'); } + + if (! static::isCryptSharedLibAvailable() && ! static::isMongocryptdAvailable()) { + $this->markTestSkipped('Neither crypt_shared nor mongocryptd are available'); + } } protected function skipIfGeoHaystackIndexIsNotSupported(): void @@ -578,6 +571,32 @@ protected function skipIfTransactionsAreNotSupported(): void } } + /** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/shared-library/ */ + public static function isCryptSharedLibAvailable(): bool + { + $cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH'); + + if ($cryptSharedLibPath === false) { + return false; + } + + return is_readable($cryptSharedLibPath); + } + + /** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/mongocryptd/ */ + public static function isMongocryptdAvailable(): bool + { + $paths = explode(PATH_SEPARATOR, getenv("PATH")); + + foreach ($paths as $path) { + if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) { + return true; + } + } + + return false; + } + private static function appendAuthenticationOptions(array $options): array { if (isset($options['username']) || isset($options['password'])) { diff --git a/tests/Operation/CreateEncryptedCollectionFunctionalTest.php b/tests/Operation/CreateEncryptedCollectionFunctionalTest.php index b6d920ce4..b80258915 100644 --- a/tests/Operation/CreateEncryptedCollectionFunctionalTest.php +++ b/tests/Operation/CreateEncryptedCollectionFunctionalTest.php @@ -12,13 +12,7 @@ use MongoDB\Operation\CreateEncryptedCollection; use function base64_decode; -use function explode; use function getenv; -use function is_executable; -use function is_readable; - -use const DIRECTORY_SEPARATOR; -use const PATH_SEPARATOR; class CreateEncryptedCollectionFunctionalTest extends FunctionalTestCase { @@ -217,28 +211,4 @@ public static function createTestClient(?string $uri = null, array $options = [] return parent::createTestClient($uri, $options, $driverOptions); } - - private static function isCryptSharedLibAvailable(): bool - { - $cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH'); - - if ($cryptSharedLibPath === false) { - return false; - } - - return is_readable($cryptSharedLibPath); - } - - private static function isMongocryptdAvailable(): bool - { - $paths = explode(PATH_SEPARATOR, getenv("PATH")); - - foreach ($paths as $path) { - if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) { - return true; - } - } - - return false; - } } diff --git a/tests/SpecTests/ClientSideEncryption/FunctionalTestCase.php b/tests/SpecTests/ClientSideEncryption/FunctionalTestCase.php index a48ea03ad..8290e8e8b 100644 --- a/tests/SpecTests/ClientSideEncryption/FunctionalTestCase.php +++ b/tests/SpecTests/ClientSideEncryption/FunctionalTestCase.php @@ -8,15 +8,9 @@ use PHPUnit\Framework\Assert; use stdClass; -use function explode; use function getenv; -use function is_executable; -use function is_readable; use function sprintf; -use const DIRECTORY_SEPARATOR; -use const PATH_SEPARATOR; - /** * Base class for client-side encryption prose tests. * @@ -92,28 +86,4 @@ private static function getEnv(string $name): string return $value; } - - private static function isCryptSharedLibAvailable(): bool - { - $cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH'); - - if ($cryptSharedLibPath === false) { - return false; - } - - return is_readable($cryptSharedLibPath); - } - - private static function isMongocryptdAvailable(): bool - { - $paths = explode(PATH_SEPARATOR, getenv("PATH")); - - foreach ($paths as $path) { - if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) { - return true; - } - } - - return false; - } } diff --git a/tests/SpecTests/ClientSideEncryptionSpecTest.php b/tests/SpecTests/ClientSideEncryptionSpecTest.php index 6d30a526e..6d39a83e1 100644 --- a/tests/SpecTests/ClientSideEncryptionSpecTest.php +++ b/tests/SpecTests/ClientSideEncryptionSpecTest.php @@ -32,22 +32,16 @@ use function base64_decode; use function basename; use function count; -use function explode; use function file_get_contents; use function getenv; use function glob; use function in_array; -use function is_executable; -use function is_readable; use function iterator_to_array; use function json_decode; use function sprintf; use function str_repeat; use function substr; -use const DIRECTORY_SEPARATOR; -use const PATH_SEPARATOR; - /** * Client-side encryption spec tests. * @@ -123,10 +117,6 @@ public function setUp(): void parent::setUp(); $this->skipIfClientSideEncryptionIsNotSupported(); - - if (! static::isCryptSharedLibAvailable() && ! static::isMongocryptdAvailable()) { - $this->markTestSkipped('Neither crypt_shared nor mongocryptd are available'); - } } /** @@ -1988,28 +1978,4 @@ private function prepareCorpusData(string $fieldName, stdClass $data, ClientEncr return $data->allowed ? $returnData : $data; } - - private static function isCryptSharedLibAvailable(): bool - { - $cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH'); - - if ($cryptSharedLibPath === false) { - return false; - } - - return is_readable($cryptSharedLibPath); - } - - private static function isMongocryptdAvailable(): bool - { - $paths = explode(PATH_SEPARATOR, getenv("PATH")); - - foreach ($paths as $path) { - if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) { - return true; - } - } - - return false; - } } diff --git a/tests/UnifiedSpecTests/UnifiedTestRunner.php b/tests/UnifiedSpecTests/UnifiedTestRunner.php index 946ff32b2..40f2c3046 100644 --- a/tests/UnifiedSpecTests/UnifiedTestRunner.php +++ b/tests/UnifiedSpecTests/UnifiedTestRunner.php @@ -24,8 +24,6 @@ use function getenv; use function implode; use function in_array; -use function is_executable; -use function is_readable; use function is_string; use function parse_url; use function PHPUnit\Framework\assertContainsOnly; @@ -42,9 +40,7 @@ use function substr_replace; use function version_compare; -use const DIRECTORY_SEPARATOR; use const FILTER_VALIDATE_BOOLEAN; -use const PATH_SEPARATOR; /** * Unified test runner. @@ -341,6 +337,8 @@ private function isAuthenticated(): bool /** * Return whether client-side encryption is supported. + * + * @see FunctionalTestCase::skipIfClientSideEncryptionIsNotSupported() */ private function isClientSideEncryptionSupported(): bool { @@ -354,31 +352,7 @@ private function isClientSideEncryptionSupported(): bool return false; } - return static::isCryptSharedLibAvailable() || static::isMongocryptdAvailable(); - } - - private static function isCryptSharedLibAvailable(): bool - { - $cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH'); - - if ($cryptSharedLibPath === false) { - return false; - } - - return is_readable($cryptSharedLibPath); - } - - private static function isMongocryptdAvailable(): bool - { - $paths = explode(PATH_SEPARATOR, getenv("PATH")); - - foreach ($paths as $path) { - if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) { - return true; - } - } - - return false; + return FunctionalTestCase::isCryptSharedLibAvailable() || FunctionalTestCase::isMongocryptdAvailable(); } /**