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

Convert deprecated "timeout" option for Database::command() #198

Merged
merged 1 commit into from
Dec 10, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/Doctrine/MongoDB/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
32 changes: 32 additions & 0 deletions tests/Doctrine/MongoDB/Tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down