Browse Source

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

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
ec2df146a8
  1. 4
      extensions/mongo/Connection.php
  2. 6
      tests/unit/extensions/mongo/ConnectionTest.php
  3. 2
      tests/unit/extensions/mongo/MongoTestCase.php

4
extensions/mongo/Connection.php

@ -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.");

6
tests/unit/extensions/mongo/ConnectionTest.php

@ -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!');

2
tests/unit/extensions/mongo/MongoTestCase.php

@ -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'];
} }

Loading…
Cancel
Save