Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

Convert deprecated MongoClient constructor options for driver >= 1.4.0 #178

Merged
merged 2 commits into from Apr 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions lib/Doctrine/MongoDB/Connection.php
Expand Up @@ -276,6 +276,9 @@ public function initialize()
$server = $this->server ?: 'mongodb://localhost:27017';
$options = $this->options;

$options = isset($options['timeout']) ? $this->convertConnectTimeout($options) : $options;
$options = isset($options['wTimeout']) ? $this->convertWriteTimeout($options) : $options;

$this->mongoClient = $this->retry(function() use ($server, $options) {
return version_compare(phpversion('mongo'), '1.3.0', '<')
? new \Mongo($server, $options)
Expand Down Expand Up @@ -442,4 +445,52 @@ protected function retry(\Closure $retry)
}
}
}

/**
* Converts "timeout" MongoClient constructor option to "connectTimeoutMS"
* for driver versions 1.4.0+.
*
* Note: MongoClient actually allows case-insensitive option names, but
* we'll only process the canonical version here.
*
* @param array $options
* @return array
*/
protected function convertConnectTimeout(array $options)
{
if (version_compare(phpversion('mongo'), '1.4.0', '<')) {
return $options;
}

if (isset($options['timeout']) && ! isset($options['connectTimeoutMS'])) {
$options['connectTimeoutMS'] = $options['timeout'];
unset($options['timeout']);
}

return $options;
}

/**
* Converts "timeout" MongoClient constructor option to "connectTimeoutMS"
* for driver versions 1.4.0+.
*
* Note: MongoClient actually allows case-insensitive option names, but
* we'll only process the canonical version here.
*
* @param array $options
* @return array
*/
protected function convertWriteTimeout(array $options)
{
if (version_compare(phpversion('mongo'), '1.4.0', '<')) {
return $options;
}

if (isset($options['wTimeout']) && ! isset($options['wTimeoutMS'])) {
$options['wTimeoutMS'] = $options['wTimeout'];
unset($options['wTimeout']);
}

return $options;
}
}
16 changes: 16 additions & 0 deletions tests/Doctrine/MongoDB/Tests/ConnectionTest.php
Expand Up @@ -206,6 +206,22 @@ public function testToString()
$this->assertEquals('Test', (string) $conn);
}

public function testConnectTimeoutOptionIsConverted()
{
if (version_compare(phpversion('mongo'), '1.4.0', '<')) {
$this->markTestSkipped('This test is not applicable to driver versions < 1.4.0');
}

/* Since initialize() creates MongoClient directly, we cannot examine
* the options passed to its constructor.
*
* Note: we do not test "wTimeout" conversion, since the driver does not
* raise a deprecation notice for its usage (see: PHP-1079).
*/
$conn = new Connection(null, array('timeout' => 10000));
$conn->initialize();
}

private function getTestConnection($mongo)
{
return new Connection($mongo);
Expand Down