Skip to content

Commit 0e55a07

Browse files
committed
Refactor code for less duplication
1 parent 033ce9c commit 0e55a07

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

tests/atlas.phpt

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,47 @@ Atlas Connectivity Tests
77
<?php
88
require_once __DIR__ . "/utils/basic.inc";
99

10+
function extractUri(string $env): ?string
11+
{
12+
return getenv($env) ?: null;
13+
}
14+
15+
function extractUriWithCertificate(string $env): ?string
16+
{
17+
$uri = getenv($env);
18+
if (! is_string($uri)) {
19+
return null;
20+
}
21+
22+
$cert = getenv($env . '_CERT_BASE64');
23+
if (! is_string($cert)) {
24+
return null;
25+
}
26+
27+
$certPath = '/tmp/cert.pem';
28+
file_put_contents($certPath, base64_decode($cert));
29+
30+
return $uri . '&tlsCertificateKeyFile=' . $certPath;
31+
}
32+
33+
function testConnection(string $uri): void
34+
{
35+
try {
36+
$m = new \MongoDB\Driver\Manager($uri);
37+
$m->executeCommand(
38+
'admin',
39+
new \MongoDB\Driver\Command(['ping' => 1]),
40+
);
41+
iterator_to_array($m->executeQuery(
42+
'test.test',
43+
new \MongoDB\Driver\Query([]),
44+
));
45+
echo "PASS\n";
46+
} catch(Exception $e) {
47+
echo "FAIL: ", $e->getMessage(), "\n";
48+
}
49+
}
50+
1051
$envs = [
1152
'ATLAS_SERVERLESS',
1253
'ATLAS_SRV_SERVERLESS',
@@ -22,57 +63,32 @@ $envs = [
2263
'ATLAS_SRV_TLS12',
2364
];
2465
$x509Envs = [
25-
'ATLAS_X509',
26-
'ATLAS_X509_DEV',
66+
'ATLAS_X509',
67+
'ATLAS_X509_DEV',
2768
];
2869

29-
$command = new \MongoDB\Driver\Command(['ping' => 1]);
30-
$query = new \MongoDB\Driver\Query([]);
31-
3270
foreach ($envs as $env) {
3371
echo $env, ': ';
34-
$uri = getenv($env);
72+
$uri = extractUri($env);
3573

3674
if (! is_string($uri)) {
3775
echo "FAIL: env var is undefined\n";
3876
continue;
3977
}
4078

41-
try {
42-
$m = new \MongoDB\Driver\Manager($uri);
43-
$m->executeCommand('admin', $command);
44-
iterator_to_array($m->executeQuery('test.test', $query));
45-
echo "PASS\n";
46-
} catch(Exception $e) {
47-
echo "FAIL: ", $e->getMessage(), "\n";
48-
}
79+
testConnection($uri);
4980
}
5081

5182
foreach ($x509Envs as $env) {
5283
echo $env, ': ';
53-
$uri = getenv($env);
54-
$cert = getenv($env . '_CERT_BASE64');
84+
$uri = extractUriWithCertificate($env);
5585

5686
if (! is_string($uri)) {
5787
echo "FAIL: env var is undefined\n";
5888
continue;
5989
}
6090

61-
if (! is_string($cert)) {
62-
echo "FAIL: cert env var is undefined\n";
63-
continue;
64-
}
65-
66-
file_put_contents('/tmp/cert.pem', base64_decode($cert));
67-
68-
try {
69-
$m = new \MongoDB\Driver\Manager($uri . '&tlsCertificateKeyFile=/tmp/cert.pem');
70-
$m->executeCommand('admin', $command);
71-
iterator_to_array($m->executeQuery('test.test', $query));
72-
echo "PASS\n";
73-
} catch(Exception $e) {
74-
echo "FAIL: ", $e->getMessage(), "\n";
75-
}
91+
testConnection($uri);
7692
}
7793

7894
?>

0 commit comments

Comments
 (0)