Skip to content

Commit 9202f0c

Browse files
committed
Refactor isCryptSharedLibAvailable & isMongocryptdAvailable in the base FunctionalTestCase
1 parent 492e9a2 commit 9202f0c

File tree

6 files changed

+41
-119
lines changed

6 files changed

+41
-119
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,9 @@ public function testQueryableEncryption(): void
18211821
$this->markTestSkipped('Explicit encryption tests require MongoDB 7.0 or later');
18221822
}
18231823

1824-
$this->skipIfClientSideEncryptionIsNotSupported();
1824+
if (! static::isCryptSharedLibAvailable() && ! static::isMongocryptdAvailable()) {
1825+
$this->markTestSkipped('Neither crypt_shared nor mongocryptd are available');
1826+
}
18251827

18261828
// Fetch names for the database and collection under test
18271829
$collectionName = $this->getCollectionName();

tests/FunctionalTestCase.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
use function implode;
3131
use function is_array;
3232
use function is_callable;
33+
use function is_executable;
3334
use function is_object;
35+
use function is_readable;
3436
use function is_string;
3537
use function key;
3638
use function ob_get_clean;
@@ -43,8 +45,10 @@
4345
use function sprintf;
4446
use function version_compare;
4547

48+
use const DIRECTORY_SEPARATOR;
4649
use const FILTER_VALIDATE_BOOLEAN;
4750
use const INFO_MODULES;
51+
use const PATH_SEPARATOR;
4852

4953
abstract class FunctionalTestCase extends TestCase
5054
{
@@ -556,6 +560,40 @@ protected function skipIfTransactionsAreNotSupported(): void
556560
}
557561
}
558562

563+
/**
564+
* The Automatic Encryption Shared Library is installed with MongoDB Enterprise or Atlas (version 6.0 and later).
565+
*
566+
* @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/shared-library/
567+
*/
568+
protected static function isCryptSharedLibAvailable(): bool
569+
{
570+
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
571+
572+
if ($cryptSharedLibPath === false) {
573+
return false;
574+
}
575+
576+
return is_readable($cryptSharedLibPath);
577+
}
578+
579+
/**
580+
* mongocryptd is installed with MongoDB Enterprise Server (version 4.2 and later).
581+
*
582+
* @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/mongocryptd/
583+
*/
584+
protected static function isMongocryptdAvailable(): bool
585+
{
586+
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
587+
588+
foreach ($paths as $path) {
589+
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
590+
return true;
591+
}
592+
}
593+
594+
return false;
595+
}
596+
559597
private static function appendAuthenticationOptions(array $options): array
560598
{
561599
if (isset($options['username']) || isset($options['password'])) {

tests/Operation/CreateEncryptedCollectionFunctionalTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,9 @@
1212
use MongoDB\Operation\CreateEncryptedCollection;
1313

1414
use function base64_decode;
15-
use function explode;
1615
use function getenv;
17-
use function is_executable;
18-
use function is_readable;
1916
use function version_compare;
2017

21-
use const DIRECTORY_SEPARATOR;
22-
use const PATH_SEPARATOR;
23-
2418
class CreateEncryptedCollectionFunctionalTest extends FunctionalTestCase
2519
{
2620
public const LOCAL_MASTERKEY = 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk';
@@ -220,28 +214,4 @@ public static function createTestClient(?string $uri = null, array $options = []
220214

221215
return parent::createTestClient($uri, $options, $driverOptions);
222216
}
223-
224-
private static function isCryptSharedLibAvailable(): bool
225-
{
226-
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
227-
228-
if ($cryptSharedLibPath === false) {
229-
return false;
230-
}
231-
232-
return is_readable($cryptSharedLibPath);
233-
}
234-
235-
private static function isMongocryptdAvailable(): bool
236-
{
237-
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
238-
239-
foreach ($paths as $path) {
240-
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
241-
return true;
242-
}
243-
}
244-
245-
return false;
246-
}
247217
}

tests/SpecTests/ClientSideEncryption/FunctionalTestCase.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@
99
use PHPUnit\Framework\Assert;
1010
use stdClass;
1111

12-
use function explode;
1312
use function getenv;
14-
use function is_executable;
15-
use function is_readable;
1613
use function sprintf;
1714
use function strlen;
1815
use function unserialize;
1916

20-
use const DIRECTORY_SEPARATOR;
21-
use const PATH_SEPARATOR;
22-
2317
/**
2418
* Base class for client-side encryption prose tests.
2519
*
@@ -103,28 +97,4 @@ private static function getEnv(string $name): string
10397

10498
return $value;
10599
}
106-
107-
private static function isCryptSharedLibAvailable(): bool
108-
{
109-
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
110-
111-
if ($cryptSharedLibPath === false) {
112-
return false;
113-
}
114-
115-
return is_readable($cryptSharedLibPath);
116-
}
117-
118-
private static function isMongocryptdAvailable(): bool
119-
{
120-
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
121-
122-
foreach ($paths as $path) {
123-
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
124-
return true;
125-
}
126-
}
127-
128-
return false;
129-
}
130100
}

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@
3232
use function base64_decode;
3333
use function basename;
3434
use function count;
35-
use function explode;
3635
use function file_get_contents;
3736
use function getenv;
3837
use function glob;
3938
use function in_array;
40-
use function is_executable;
41-
use function is_readable;
4239
use function iterator_to_array;
4340
use function json_decode;
4441
use function sprintf;
@@ -48,9 +45,6 @@
4845
use function unserialize;
4946
use function version_compare;
5047

51-
use const DIRECTORY_SEPARATOR;
52-
use const PATH_SEPARATOR;
53-
5448
/**
5549
* Client-side encryption spec tests.
5650
*
@@ -2001,28 +1995,4 @@ private function prepareCorpusData(string $fieldName, stdClass $data, ClientEncr
20011995

20021996
return $data->allowed ? $returnData : $data;
20031997
}
2004-
2005-
private static function isCryptSharedLibAvailable(): bool
2006-
{
2007-
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
2008-
2009-
if ($cryptSharedLibPath === false) {
2010-
return false;
2011-
}
2012-
2013-
return is_readable($cryptSharedLibPath);
2014-
}
2015-
2016-
private static function isMongocryptdAvailable(): bool
2017-
{
2018-
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
2019-
2020-
foreach ($paths as $path) {
2021-
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
2022-
return true;
2023-
}
2024-
}
2025-
2026-
return false;
2027-
}
20281998
}

tests/UnifiedSpecTests/UnifiedTestRunner.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
use function getenv;
2525
use function implode;
2626
use function in_array;
27-
use function is_executable;
28-
use function is_readable;
2927
use function is_string;
3028
use function parse_url;
3129
use function PHPUnit\Framework\assertContainsOnly;
@@ -42,9 +40,7 @@
4240
use function substr_replace;
4341
use function version_compare;
4442

45-
use const DIRECTORY_SEPARATOR;
4643
use const FILTER_VALIDATE_BOOLEAN;
47-
use const PATH_SEPARATOR;
4844

4945
/**
5046
* Unified test runner.
@@ -357,30 +353,6 @@ private function isClientSideEncryptionSupported(): bool
357353
return static::isCryptSharedLibAvailable() || static::isMongocryptdAvailable();
358354
}
359355

360-
private static function isCryptSharedLibAvailable(): bool
361-
{
362-
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
363-
364-
if ($cryptSharedLibPath === false) {
365-
return false;
366-
}
367-
368-
return is_readable($cryptSharedLibPath);
369-
}
370-
371-
private static function isMongocryptdAvailable(): bool
372-
{
373-
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
374-
375-
foreach ($paths as $path) {
376-
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
377-
return true;
378-
}
379-
}
380-
381-
return false;
382-
}
383-
384356
/**
385357
* Return whether serverless (i.e. proxy as mongos) is being utilized.
386358
*/

0 commit comments

Comments
 (0)