Commit ec2df146 by Paul Klimov

Mongo Connection updated allowing to fetch default database name from options.

parent 6eeeb6d1
...@@ -85,7 +85,9 @@ class Connection extends Component ...@@ -85,7 +85,9 @@ class Connection extends Component
protected function fetchDefaultDatabaseName() protected function fetchDefaultDatabaseName()
{ {
if ($this->defaultDatabaseName === null) { 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]; $this->defaultDatabaseName = $matches[1];
} else { } else {
throw new InvalidConfigException("Unable to determine default database name from dsn."); throw new InvalidConfigException("Unable to determine default database name from dsn.");
......
...@@ -71,6 +71,12 @@ class ConnectionTest extends MongoTestCase ...@@ -71,6 +71,12 @@ class ConnectionTest extends MongoTestCase
$this->assertTrue($database instanceof Database, 'Unable to get default database!'); $this->assertTrue($database instanceof Database, 'Unable to get default database!');
$connection = new Connection(); $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']; $connection->dsn = $this->mongoConfig['dsn'] . '/' . $this->mongoConfig['defaultDatabaseName'];
$database = $connection->getDatabase(); $database = $connection->getDatabase();
$this->assertTrue($database instanceof Database, 'Unable to determine default database from dsn!'); $this->assertTrue($database instanceof Database, 'Unable to determine default database from dsn!');
......
...@@ -77,9 +77,7 @@ class MongoTestCase extends TestCase ...@@ -77,9 +77,7 @@ class MongoTestCase extends TestCase
} }
$db = new Connection; $db = new Connection;
$db->dsn = $this->mongoConfig['dsn']; $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'])) { if (isset($this->mongoConfig['options'])) {
$db->options = $this->mongoConfig['options']; $db->options = $this->mongoConfig['options'];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment