Skip to content

Commit d5b1156

Browse files
committed
Update getUri helper to extract a single mongos node
For most tests, we only want to expose a single mongos node to avoid issues with fail points. This change parses and reassembles the connection URI to only return a single mongos node in the URI.
1 parent 6cfc674 commit d5b1156

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tests/FunctionalTestCase.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,68 @@ public function tearDown()
3838
parent::tearDown();
3939
}
4040

41+
public static function getUri($allowMultipleMongoses = false)
42+
{
43+
$uri = parent::getUri();
44+
45+
if ($allowMultipleMongoses) {
46+
return $uri;
47+
}
48+
49+
$urlParts = parse_url($uri);
50+
if ($urlParts === false) {
51+
return $uri;
52+
}
53+
54+
// Only modify URIs using the mongodb scheme
55+
if ($urlParts['scheme'] !== 'mongodb') {
56+
return $uri;
57+
}
58+
59+
$hosts = explode(',', $urlParts['host']);
60+
$numHosts = count($hosts);
61+
if ($numHosts === 1) {
62+
return $uri;
63+
}
64+
65+
$manager = new Manager($uri);
66+
if ($manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY))->getType() !== Server::TYPE_MONGOS) {
67+
return $uri;
68+
}
69+
70+
// Re-append port to last host
71+
if (isset($urlParts['port'])) {
72+
$hosts[$numHosts-1] .= ':' . $urlParts['port'];
73+
}
74+
75+
$parts = [
76+
'mongodb://'
77+
];
78+
79+
if (isset($urlParts['user'], $urlParts['pass'])) {
80+
$parts += [
81+
$urlParts['user'],
82+
':',
83+
$urlParts['pass'],
84+
'@',
85+
];
86+
}
87+
88+
$parts[] = $hosts[0];
89+
90+
if (isset($urlParts['path'])) {
91+
$parts[] = $urlParts['path'];
92+
}
93+
if (isset($urlParts['query'])) {
94+
$parts += [
95+
'?',
96+
$urlParts['path']
97+
];
98+
}
99+
100+
return implode('', $parts);
101+
}
102+
41103
protected function assertCollectionCount($namespace, $count)
42104
{
43105
list($databaseName, $collectionName) = explode('.', $namespace, 2);

0 commit comments

Comments
 (0)