From 0ab6f72a3d82314a9238d2d5d83e4503d41e3a2d Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Tue, 9 Dec 2014 19:21:55 -0500 Subject: [PATCH] Convert deprecated "timeout" option for Database::command() --- lib/Doctrine/MongoDB/Database.php | 23 +++++++++++++ tests/Doctrine/MongoDB/Tests/DatabaseTest.php | 32 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/lib/Doctrine/MongoDB/Database.php b/lib/Doctrine/MongoDB/Database.php index d5a2de45..077375ed 100644 --- a/lib/Doctrine/MongoDB/Database.php +++ b/lib/Doctrine/MongoDB/Database.php @@ -103,6 +103,8 @@ public function authenticate($username, $password) */ public function command(array $data, array $options = array()) { + $options = isset($options['timeout']) ? $this->convertSocketTimeout($options) : $options; + return $this->mongoDB->command($data, $options); } @@ -605,4 +607,25 @@ protected function retry(\Closure $retry) } } } + + /** + * Convert "timeout" write option to "socketTimeoutMS" for driver version + * 1.5.0+. + * + * @param array $options + * @return array + */ + protected function convertSocketTimeout(array $options) + { + if (version_compare(phpversion('mongo'), '1.5.0', '<')) { + return $options; + } + + if (isset($options['timeout']) && ! isset($options['socketTimeoutMS'])) { + $options['socketTimeoutMS'] = $options['timeout']; + unset($options['timeout']); + } + + return $options; + } } diff --git a/tests/Doctrine/MongoDB/Tests/DatabaseTest.php b/tests/Doctrine/MongoDB/Tests/DatabaseTest.php index 89ec70c8..90635bf0 100644 --- a/tests/Doctrine/MongoDB/Tests/DatabaseTest.php +++ b/tests/Doctrine/MongoDB/Tests/DatabaseTest.php @@ -186,6 +186,38 @@ public function testSetReadPreference() $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))); } + public function testSocketTimeoutOptionIsConverted() + { + if (version_compare(phpversion('mongo'), '1.5.0', '<')) { + $this->markTestSkipped('This test is not applicable to driver versions < 1.5.0'); + } + + $mongoDB = $this->getMockMongoDB(); + $mongoDB->expects($this->any()) + ->method('command') + ->with(array('count' => 'foo'), array('socketTimeoutMS' => 1000)); + + $database = new Database($this->getMockConnection(), $mongoDB, $this->getMockEventManager()); + + $database->command(array('count' => 'foo'), array('timeout' => 1000)); + } + + public function testSocketTimeoutOptionIsNotConvertedForOlderDrivers() + { + if (version_compare(phpversion('mongo'), '1.5.0', '>=')) { + $this->markTestSkipped('This test is not applicable to driver versions >= 1.5.0'); + } + + $mongoDB = $this->getMockMongoDB(); + $mongoDB->expects($this->any()) + ->method('command') + ->with(array('count' => 'foo'), array('timeout' => 1000)); + + $database = new Database($this->getMockConnection(), $mongoDB, $this->getMockEventManager()); + + $database->command(array('count' => 'foo'), array('timeout' => 1000)); + } + private function getMockConnection() { return $this->getMockBuilder('Doctrine\MongoDB\Connection')