@@ -7,6 +7,47 @@ Atlas Connectivity Tests
7
7
<?php
8
8
require_once __DIR__ . "/utils/basic.inc " ;
9
9
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
+
10
51
$ envs = [
11
52
'ATLAS_SERVERLESS ' ,
12
53
'ATLAS_SRV_SERVERLESS ' ,
@@ -22,57 +63,32 @@ $envs = [
22
63
'ATLAS_SRV_TLS12 ' ,
23
64
];
24
65
$ x509Envs = [
25
- 'ATLAS_X509 ' ,
26
- 'ATLAS_X509_DEV ' ,
66
+ 'ATLAS_X509 ' ,
67
+ 'ATLAS_X509_DEV ' ,
27
68
];
28
69
29
- $ command = new \MongoDB \Driver \Command (['ping ' => 1 ]);
30
- $ query = new \MongoDB \Driver \Query ([]);
31
-
32
70
foreach ($ envs as $ env ) {
33
71
echo $ env , ': ' ;
34
- $ uri = getenv ($ env );
72
+ $ uri = extractUri ($ env );
35
73
36
74
if (! is_string ($ uri )) {
37
75
echo "FAIL: env var is undefined \n" ;
38
76
continue ;
39
77
}
40
78
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 );
49
80
}
50
81
51
82
foreach ($ x509Envs as $ env ) {
52
83
echo $ env , ': ' ;
53
- $ uri = getenv ($ env );
54
- $ cert = getenv ($ env . '_CERT_BASE64 ' );
84
+ $ uri = extractUriWithCertificate ($ env );
55
85
56
86
if (! is_string ($ uri )) {
57
87
echo "FAIL: env var is undefined \n" ;
58
88
continue ;
59
89
}
60
90
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 );
76
92
}
77
93
78
94
?>
0 commit comments