Permalink
Browse files

Merge branch '1.1.x'

  • Loading branch information...
2 parents 36d8f3c + a104f87 commit fd76cd00d3cead2db3f448940688a12eb8c6d3f0 @jmikola jmikola committed Apr 21, 2014
Showing with 67 additions and 0 deletions.
  1. +51 −0 lib/Doctrine/MongoDB/Connection.php
  2. +16 −0 tests/Doctrine/MongoDB/Tests/ConnectionTest.php
View
51 lib/Doctrine/MongoDB/Connection.php
@@ -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)
@@ -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;
+ }
}
View
16 tests/Doctrine/MongoDB/Tests/ConnectionTest.php
@@ -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);

0 comments on commit fd76cd0

Please sign in to comment.