diff --git a/extensions/mongo/Connection.php b/extensions/mongo/Connection.php index ce99ee4..725c757 100644 --- a/extensions/mongo/Connection.php +++ b/extensions/mongo/Connection.php @@ -85,7 +85,9 @@ class Connection extends Component protected function fetchDefaultDatabaseName() { if ($this->defaultDatabaseName === null) { - if (preg_match('/^mongodb:\\/\\/.+\\/(.+)$/s', $this->dsn, $matches)) { + if (isset($this->options['db'])) { + $this->defaultDatabaseName = $this->options['db']; + } elseif (preg_match('/^mongodb:\\/\\/.+\\/(.+)$/s', $this->dsn, $matches)) { $this->defaultDatabaseName = $matches[1]; } else { throw new InvalidConfigException("Unable to determine default database name from dsn."); diff --git a/tests/unit/extensions/mongo/ConnectionTest.php b/tests/unit/extensions/mongo/ConnectionTest.php index 3b8a1a2..b3252b9 100644 --- a/tests/unit/extensions/mongo/ConnectionTest.php +++ b/tests/unit/extensions/mongo/ConnectionTest.php @@ -71,6 +71,12 @@ class ConnectionTest extends MongoTestCase $this->assertTrue($database instanceof Database, 'Unable to get default database!'); $connection = new Connection(); + $connection->dsn = $this->mongoConfig['dsn']; + $connection->options = ['db' => $this->mongoConfig['defaultDatabaseName']]; + $database = $connection->getDatabase(); + $this->assertTrue($database instanceof Database, 'Unable to determine default database from options!'); + + $connection = new Connection(); $connection->dsn = $this->mongoConfig['dsn'] . '/' . $this->mongoConfig['defaultDatabaseName']; $database = $connection->getDatabase(); $this->assertTrue($database instanceof Database, 'Unable to determine default database from dsn!'); diff --git a/tests/unit/extensions/mongo/MongoTestCase.php b/tests/unit/extensions/mongo/MongoTestCase.php index 439afb2..0bdcd74 100644 --- a/tests/unit/extensions/mongo/MongoTestCase.php +++ b/tests/unit/extensions/mongo/MongoTestCase.php @@ -77,9 +77,7 @@ class MongoTestCase extends TestCase } $db = new Connection; $db->dsn = $this->mongoConfig['dsn']; - if (isset($this->mongoConfig['defaultDatabaseName'])) { - $db->defaultDatabaseName = $this->mongoConfig['defaultDatabaseName']; - } + $db->defaultDatabaseName = $this->mongoConfig['defaultDatabaseName']; if (isset($this->mongoConfig['options'])) { $db->options = $this->mongoConfig['options']; }