Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement get/setReadPreference() methods on core classes

  • Loading branch information...
commit 257fb456bcbba16ddfec446527ebca92ebd84d63 1 parent f423a9f
Jeremy Mikola jmikola authored
14 lib/Doctrine/MongoDB/Collection.php
View
@@ -666,6 +666,20 @@ public function getSlaveOkay()
return \MongoClient::RP_PRIMARY !== $readPref['type'];
}
+ public function getReadPreference()
+ {
+ return $this->getMongoCollection()->getReadPreference();
+ }
+
+ public function setReadPreference($readPreference, array $tags = null)
+ {
+ if (isset($tags)) {
+ return $this->getMongoCollection()->setReadPreference($readPreference, $tags);
+ }
+
+ return $this->getMongoCollection()->setReadPreference($readPreference);
+ }
+
public function validate($scanData = false)
{
return $this->getMongoCollection()->validate($scanData);
14 lib/Doctrine/MongoDB/Connection.php
View
@@ -258,6 +258,20 @@ public function selectDatabase($name)
return $database;
}
+ public function getReadPreference()
+ {
+ return $this->mongo->getReadPreference();
+ }
+
+ public function setReadPreference($readPreference, array $tags = null)
+ {
+ if (isset($tags)) {
+ return $this->mongo->setReadPreference($readPreference, $tags);
+ }
+
+ return $this->mongo->setReadPreference($readPreference);
+ }
+
/**
* Method which creates a Doctrine\MongoDB\Database instance.
*
14 lib/Doctrine/MongoDB/Cursor.php
View
@@ -387,6 +387,20 @@ public function getSingleResult()
return $result;
}
+ public function getReadPreference()
+ {
+ return $this->getMongoDB()->getReadPreference();
+ }
+
+ public function setReadPreference($readPreference, array $tags = null)
+ {
+ if (isset($tags)) {
+ return $this->mongoCursor->setReadPreference($readPreference, $tags);
+ }
+
+ return $this->mongoCursor->setReadPreference($readPreference);
+ }
+
protected function retry(\Closure $retry, $recreate = false)
{
if ($this->numRetries) {
14 lib/Doctrine/MongoDB/Database.php
View
@@ -250,6 +250,20 @@ public function getSlaveOkay()
return \MongoClient::RP_PRIMARY !== $readPref['type'];
}
+ public function getReadPreference()
+ {
+ return $this->getMongoDB()->getReadPreference();
+ }
+
+ public function setReadPreference($readPreference, array $tags = null)
+ {
+ if (isset($tags)) {
+ return $this->getMongoDB()->setReadPreference($readPreference, $tags);
+ }
+
+ return $this->getMongoDB()->setReadPreference($readPreference);
+ }
+
public function getProfilingLevel()
{
return $this->getMongoDB()->getProfilingLevel();
24 tests/Doctrine/MongoDB/Tests/CollectionTest.php
View
@@ -490,6 +490,30 @@ public function testSetSlaveOkayPreservesReadPreferenceTags()
$this->assertEquals(true, $collection->setSlaveOkay(true));
}
+ public function testSetReadPreference()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
+ $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
+ }
+
+ $mongoCollection = $this->getMockMongoCollection();
+
+ $mongoCollection->expects($this->at(1))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_PRIMARY)
+ ->will($this->returnValue(true));
+
+ $mongoCollection->expects($this->at(2))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))
+ ->will($this->returnValue(true));
+
+ $collection = $this->getTestCollection($this->getMockConnection(), $mongoCollection, $this->getMockDatabase());
+
+ $this->assertTrue($collection->setReadPreference(\MongoClient::RP_PRIMARY));
+ $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east'))));
+ }
+
public function testValidate()
{
$mongoCollection = $this->getMockMongoCollection();
35 tests/Doctrine/MongoDB/Tests/ConnectionTest.php
View
@@ -167,6 +167,30 @@ public function testSelectDatabase()
$this->assertSame($mockMongoDB, $result->getMongoDB());
}
+ public function testSetReadPreference()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
+ $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
+ }
+
+ $mongoClient = $this->getMockMongoClient();
+
+ $mongoClient->expects($this->at(0))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_PRIMARY)
+ ->will($this->returnValue(true));
+
+ $mongoClient->expects($this->at(1))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))
+ ->will($this->returnValue(true));
+
+ $conn = $this->getTestConnection($mongoClient);
+
+ $this->assertTrue($conn->setReadPreference(\MongoClient::RP_PRIMARY));
+ $this->assertTrue($conn->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east'))));
+ }
+
public function testToString()
{
$mockMongo = $this->getMockMongo();
@@ -178,9 +202,9 @@ public function testToString()
$this->assertEquals('Test', (string) $conn);
}
- private function getTestConnection(Mongo $mongo)
+ private function getTestConnection($mongo)
{
- return new \Doctrine\MongoDB\Connection($mongo);
+ return new Connection($mongo);
}
private function getMockMongo()
@@ -188,6 +212,13 @@ private function getMockMongo()
return $this->getMock('Mongo', array(), array(), '', false, false);
}
+ private function getMockMongoClient()
+ {
+ return $this->getMockBuilder('MongoClient')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
private function getMockMongoDB()
{
return $this->getMock('MongoDB', array(), array(), '', false, false);
24 tests/Doctrine/MongoDB/Tests/CursorTest.php
View
@@ -184,6 +184,30 @@ public function testSlaveOkayPreservesReadPreferenceTags()
$cursor->slaveOkay(true);
}
+ public function testSetReadPreference()
+ {
+ if (!method_exists('MongoCursor', 'setReadPreference')) {
+ $this->markTestSkipped('This test is not applicable to drivers without MongoCursor::setReadPreference()');
+ }
+
+ $mongoCursor = $this->getMockMongoCursor();
+
+ $mongoCursor->expects($this->at(0))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_PRIMARY)
+ ->will($this->returnValue(true));
+
+ $mongoCursor->expects($this->at(1))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))
+ ->will($this->returnValue(true));
+
+ $cursor = $this->getTestCursor($this->getMockConnection(), $this->getMockCollection(), $mongoCursor);
+
+ $this->assertTrue($cursor->setReadPreference(\MongoClient::RP_PRIMARY));
+ $this->assertTrue($cursor->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east'))));
+ }
+
private function getMockMongoCursor()
{
return $this->getMockBuilder('MongoCursor')
22 tests/Doctrine/MongoDB/Tests/DatabaseTest.php
View
@@ -100,6 +100,28 @@ public function testSetSlaveOkayPreservesReadPreferenceTags()
$this->assertEquals(true, $database->setSlaveOkay(true));
}
+ public function testSetReadPreference()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
+ $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
+ }
+
+ $this->mongodb->expects($this->at(0))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_PRIMARY)
+ ->will($this->returnValue(true));
+
+ $this->mongodb->expects($this->at(1))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))
+ ->will($this->returnValue(true));
+
+ $database = new Database($this->connection, 'test', $this->getMockEventManager(), '$');
+
+ $this->assertTrue($database->setReadPreference(\MongoClient::RP_PRIMARY));
+ $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east'))));
+ }
+
private function getMockConnection()
{
return $this->getMockBuilder('Doctrine\MongoDB\Connection')
Please sign in to comment.
Something went wrong with that request. Please try again.