Skip to content

Commit ae8b61c

Browse files
committed
Refactor isCryptSharedLibAvailable & isMongocryptdAvailable in the base FunctionalTestCase
1 parent 35c7bbe commit ae8b61c

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
@@ -1811,7 +1811,9 @@ public function testQueryableEncryption(): void
18111811

18121812
$this->skipIfServerVersion('<', '7.0.0', 'Explicit encryption tests require MongoDB 7.0 or later');
18131813

1814-
$this->skipIfClientSideEncryptionIsNotSupported();
1814+
if (! static::isCryptSharedLibAvailable() && ! static::isMongocryptdAvailable()) {
1815+
$this->markTestSkipped('Neither crypt_shared nor mongocryptd are available');
1816+
}
18151817

18161818
// Fetch names for the database and collection under test
18171819
$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
{
@@ -563,6 +567,40 @@ protected function skipIfTransactionsAreNotSupported(): void
563567
}
564568
}
565569

570+
/**
571+
* The Automatic Encryption Shared Library is installed with MongoDB Enterprise or Atlas (version 6.0 and later).
572+
*
573+
* @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/shared-library/
574+
*/
575+
protected static function isCryptSharedLibAvailable(): bool
576+
{
577+
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
578+
579+
if ($cryptSharedLibPath === false) {
580+
return false;
581+
}
582+
583+
return is_readable($cryptSharedLibPath);
584+
}
585+
586+
/**
587+
* mongocryptd is installed with MongoDB Enterprise Server (version 4.2 and later).
588+
*
589+
* @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/mongocryptd/
590+
*/
591+
protected static function isMongocryptdAvailable(): bool
592+
{
593+
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
594+
595+
foreach ($paths as $path) {
596+
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
597+
return true;
598+
}
599+
}
600+
601+
return false;
602+
}
603+
566604
private static function appendAuthenticationOptions(array $options): array
567605
{
568606
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,13 +12,7 @@
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;
19-
20-
use const DIRECTORY_SEPARATOR;
21-
use const PATH_SEPARATOR;
2216

2317
class CreateEncryptedCollectionFunctionalTest extends FunctionalTestCase
2418
{
@@ -217,28 +211,4 @@ public static function createTestClient(?string $uri = null, array $options = []
217211

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

tests/SpecTests/ClientSideEncryption/FunctionalTestCase.php

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

11-
use function explode;
1211
use function getenv;
13-
use function is_executable;
14-
use function is_readable;
1512
use function sprintf;
1613

17-
use const DIRECTORY_SEPARATOR;
18-
use const PATH_SEPARATOR;
19-
2014
/**
2115
* Base class for client-side encryption prose tests.
2216
*
@@ -92,28 +86,4 @@ private static function getEnv(string $name): string
9286

9387
return $value;
9488
}
95-
96-
private static function isCryptSharedLibAvailable(): bool
97-
{
98-
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
99-
100-
if ($cryptSharedLibPath === false) {
101-
return false;
102-
}
103-
104-
return is_readable($cryptSharedLibPath);
105-
}
106-
107-
private static function isMongocryptdAvailable(): bool
108-
{
109-
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
110-
111-
foreach ($paths as $path) {
112-
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
113-
return true;
114-
}
115-
}
116-
117-
return false;
118-
}
11989
}

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,16 @@
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;
4542
use function str_repeat;
4643
use function substr;
4744

48-
use const DIRECTORY_SEPARATOR;
49-
use const PATH_SEPARATOR;
50-
5145
/**
5246
* Client-side encryption spec tests.
5347
*
@@ -1988,28 +1982,4 @@ private function prepareCorpusData(string $fieldName, stdClass $data, ClientEncr
19881982

19891983
return $data->allowed ? $returnData : $data;
19901984
}
1991-
1992-
private static function isCryptSharedLibAvailable(): bool
1993-
{
1994-
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
1995-
1996-
if ($cryptSharedLibPath === false) {
1997-
return false;
1998-
}
1999-
2000-
return is_readable($cryptSharedLibPath);
2001-
}
2002-
2003-
private static function isMongocryptdAvailable(): bool
2004-
{
2005-
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
2006-
2007-
foreach ($paths as $path) {
2008-
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
2009-
return true;
2010-
}
2011-
}
2012-
2013-
return false;
2014-
}
20151985
}

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)