Commit 3abefac6 by Klimov Paul

Fixed `yii\mongodb\Collection` unable to fetch default database name from DSN with parameters

parent b9520596
...@@ -4,7 +4,7 @@ Yii Framework 2 mongodb extension Change Log ...@@ -4,7 +4,7 @@ Yii Framework 2 mongodb extension Change Log
2.0.0 under development 2.0.0 under development
----------------------- -----------------------
- no changes in this release. - Bug #5303: Fixed `yii\mongodb\Collection` unable to fetch default database name from DSN with parameters (klimov-paul)
2.0.0-rc September 27, 2014 2.0.0-rc September 27, 2014
......
...@@ -148,7 +148,7 @@ class Connection extends Component ...@@ -148,7 +148,7 @@ class Connection extends Component
if ($this->defaultDatabaseName === null) { if ($this->defaultDatabaseName === null) {
if (isset($this->options['db'])) { if (isset($this->options['db'])) {
$this->defaultDatabaseName = $this->options['db']; $this->defaultDatabaseName = $this->options['db'];
} elseif (preg_match('/^mongodb:\\/\\/.+\\/(.+)$/s', $this->dsn, $matches)) { } 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.");
......
...@@ -61,7 +61,45 @@ class ConnectionTest extends MongoDbTestCase ...@@ -61,7 +61,45 @@ class ConnectionTest extends MongoDbTestCase
} }
/** /**
* Data provider for [[testFetchDefaultDatabaseName()]]
* @return array test data
*/
public function dataProviderFetchDefaultDatabaseName()
{
return [
[
'mongodb://travis:test@localhost:27017/dbname',
'dbname',
],
[
'mongodb://travis:test@localhost:27017/dbname?replicaSet=test&connectTimeoutMS=300000',
'dbname',
],
];
}
/**
* @dataProvider dataProviderFetchDefaultDatabaseName
*
* @param string $dsn
* @param string $databaseName
*/
public function testFetchDefaultDatabaseName($dsn, $databaseName)
{
$connection = new Connection();
$connection->dsn = $dsn;
$reflection = new \ReflectionObject($connection);
$method = $reflection->getMethod('fetchDefaultDatabaseName');
$method->setAccessible(true);
$method->invoke($connection);
$this->assertEquals($databaseName, $connection->defaultDatabaseName);
}
/**
* @depends testGetDatabase * @depends testGetDatabase
* @depends testFetchDefaultDatabaseName
*/ */
public function testGetDefaultDatabase() public function testGetDefaultDatabase()
{ {
......
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