Skip to content

Commit 6260b47

Browse files
authored
Fix database name extraction from DSN containing a CA file path (#3381)
1 parent 2815f1b commit 6260b47

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ protected function withFreshQueryLog($callback)
214214
protected function getDefaultDatabaseName(string $dsn, array $config): string
215215
{
216216
if (empty($config['database'])) {
217-
if (! preg_match('/^mongodb(?:[+]srv)?:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) {
217+
if (! preg_match('/^mongodb(?:[+]srv)?:\\/\\/.+?\\/([^?&]+)/s', $dsn, $matches)) {
218218
throw new InvalidArgumentException('Database is not properly configured.');
219219
}
220220

tests/ConnectionTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ public static function dataConnectionConfig(): Generator
190190
'expectedDatabaseName' => 'tests',
191191
'config' => ['dsn' => 'mongodb://some-host:12345/tests'],
192192
];
193+
194+
yield 'Database is extracted from DSN with CA path in options' => [
195+
'expectedUri' => 'mongodb://some-host:12345/tests?tls=true&tlsCAFile=/path/to/ca.pem&retryWrites=false',
196+
'expectedDatabaseName' => 'tests',
197+
'config' => ['dsn' => 'mongodb://some-host:12345/tests?tls=true&tlsCAFile=/path/to/ca.pem&retryWrites=false'],
198+
];
193199
}
194200

195201
#[DataProvider('dataConnectionConfig')]

0 commit comments

Comments
 (0)