Skip to content

Commit f21efac

Browse files
committed
Handle readPreferenceTags in URI options
1 parent ea4a99b commit f21efac

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

tests/UnifiedSpecTests/Context.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MongoDB\Driver\Server;
1010
use stdClass;
1111
use function array_key_exists;
12+
use function array_map;
1213
use function assertArrayHasKey;
1314
use function assertContains;
1415
use function assertCount;
@@ -181,6 +182,26 @@ public function getEventObserverForClient(string $id) : EventObserver
181182
return $this->eventObserversByClient[$id];
182183
}
183184

185+
/** @param string|array $readPreferenceTags */
186+
private function convertReadPreferenceTags($readPreferenceTags) : array
187+
{
188+
return array_map(
189+
static function (string $readPreferenceTagSet) : array {
190+
$tags = explode(',', $readPreferenceTagSet);
191+
192+
return array_map(
193+
static function (string $tag) : array {
194+
list($key, $value) = explode(':', $tag);
195+
196+
return [$key => $value];
197+
},
198+
$tags
199+
);
200+
},
201+
(array) $readPreferenceTags
202+
);
203+
}
204+
184205
private function createClient(string $id, stdClass $o)
185206
{
186207
Util::assertHasOnlyKeys($o, ['id', 'uriOptions', 'useMultipleMongoses', 'observeEvents', 'ignoreCommandMonitoringEvents']);
@@ -205,10 +226,18 @@ private function createClient(string $id, stdClass $o)
205226

206227
if (isset($o->uriOptions)) {
207228
assertInternalType('object', $o->uriOptions);
208-
/* TODO: If readPreferenceTags is set, assert it is an array of
209-
* strings and convert to an array of documents expected by the
210-
* PHP driver. */
211229
$uriOptions = (array) $o->uriOptions;
230+
231+
if (! empty($uriOptions['readPreferenceTags'])) {
232+
/* readPreferenceTags may take the following form:
233+
*
234+
* 1. A string containing multiple tags: "dc:ny,rack:1".
235+
* Expected result: [["dc" => "ny", "rack" => "1"]]
236+
* 2. An array containing multiple strings as above: ["dc:ny,rack:1", "dc:la"].
237+
* Expected result: [["dc" => "ny", "rack" => "1"], ["dc" => "la"]]
238+
*/
239+
$uriOptions['readPreferenceTags'] = $this->convertReadPreferenceTags($uriOptions['readPreferenceTags']);
240+
}
212241
}
213242

214243
if (isset($observeEvents)) {

0 commit comments

Comments
 (0)