Skip to content

Commit

Permalink
Merge pull request #103 from koubas/master
Browse files Browse the repository at this point in the history
Connection's 'dbname' option sets (but does not override) 'defaultDB' co...
  • Loading branch information
Ocramius committed Apr 17, 2014
2 parents b209bab + d2a28f8 commit 704c2c4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ Either `server` or `connectionString` option has to be set.
| connectionString | null | If the connection string is specified, it will overwrite other connection options (`server`,`port`,`user`,`password`,`dbname`). Still, the connection will respect the settings passed in `options` array.
| user | null | If set, the client will try to authenticate with given username and password
| password | null | If set, the client will try to authenticate with given username and password
| dbname | null | If dbname is not specified, "admin" will be used.
| dbname | null | If dbname is not specified, "admin" will be used for authentication. Also, specifiing dbname affecs the defaultDB configuration option, if that's not specified explicitly.
| options | array() | Array with connection options. More detailed description in http://www.php.net/manual/en/mongoclient.construct.php
25 changes: 22 additions & 3 deletions src/DoctrineMongoODMModule/Service/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function createService(ServiceLocatorInterface $serviceLocator)
$options = $this->getOptions($serviceLocator, 'connection');

$connectionString = $options->getConnectionString();
$dbName = null;

if (empty($connectionString)) {
$connectionString = 'mongodb://';
Expand All @@ -61,11 +62,29 @@ public function createService(ServiceLocatorInterface $serviceLocator)
if ($dbName) {
$connectionString .= '/' . $dbName;
}

} else {
// parse dbName from the connectionString
$dbStart = strpos($connectionString, '/', 11);
if (false !== $dbStart) {
$dbEnd = strpos($connectionString, '?');
$dbName = substr(
$connectionString,
$dbStart + 1,
$dbEnd ? ($dbEnd - $dbStart - 1) : PHP_INT_MAX
);
}
}

/** @var $configuration \Doctrine\ODM\MongoDB\Configuration */
$configuration = $serviceLocator->get('doctrine.configuration.' . $this->getName());

// Set defaultDB to $dbName, if it's not defined in configuration
if (null === $configuration->getDefaultDB()) {
$configuration->setDefaultDB($dbName);
}

return new Connection($connectionString, $options->getOptions(), $serviceLocator->get(
'doctrine.configuration.' . $this->getName()
));
return new Connection($connectionString, $options->getOptions(), $configuration);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,82 @@ public function testConnectionStringShouldAllowUnixSockets()

$this->assertEquals($connectionString, $connection->getServer());
}

public function testDbNameShouldSetDefaultDB()
{
$dbName = 'foo_db';
$connectionConfig = array(
'odm_default' => array(
'dbname' => $dbName,
)
);

$this->configuration['doctrine']['connection'] = $connectionConfig;
$this->serviceManager->setService('Configuration', $this->configuration);

$configuration = $this->serviceManager->get('doctrine.configuration.odm_default');
$configuration->setDefaultDB(null);
$this->connectionFactory->createService($this->serviceManager);

$this->assertEquals($dbName, $configuration->getDefaultDB());
}

public function testDbNameShouldNotOverrideExplicitDefaultDB()
{
$defaultDB = 'foo_db';
$connectionConfig = array(
'odm_default' => array(
'dbname' => 'test fails if this is defaultDB',
)
);

$this->configuration['doctrine']['connection'] = $connectionConfig;
$this->serviceManager->setService('Configuration', $this->configuration);

$configuration = $this->serviceManager->get('doctrine.configuration.odm_default');
$configuration->setDefaultDB($defaultDB);
$this->connectionFactory->createService($this->serviceManager);

$this->assertEquals($defaultDB, $configuration->getDefaultDB());
}

public function testConnectionStringShouldSetDefaultDB()
{
$dbName = 'foo_db';
$connectionString = "mongodb://localhost:27017/$dbName";
$connectionConfig = array(
'odm_default' => array(
'connectionString' => $connectionString,
)
);

$this->configuration['doctrine']['connection'] = $connectionConfig;
$this->serviceManager->setService('Configuration', $this->configuration);

$configuration = $this->serviceManager->get('doctrine.configuration.odm_default');
$configuration->setDefaultDB(null);
$this->connectionFactory->createService($this->serviceManager);

$this->assertEquals($dbName, $configuration->getDefaultDB());
}

public function testConnectionStringWithOptionsShouldSetDefaultDB()
{
$dbName = 'foo_db';
$connectionString = "mongodb://localhost:27017/$dbName?bar=baz";
$connectionConfig = array(
'odm_default' => array(
'connectionString' => $connectionString,
)
);

$this->configuration['doctrine']['connection'] = $connectionConfig;
$this->serviceManager->setService('Configuration', $this->configuration);

$configuration = $this->serviceManager->get('doctrine.configuration.odm_default');
$configuration->setDefaultDB(null);
$this->connectionFactory->createService($this->serviceManager);

$this->assertEquals($dbName, $configuration->getDefaultDB());
}
}

0 comments on commit 704c2c4

Please sign in to comment.