Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test MongoCursor read preferences via slaveOkay()

Also made collection and database tests a bit stricter, to ensure deprecated methods are never called.
  • Loading branch information...
commit ec10f4d32500efe8b4115a78974e7cc089294ddf 1 parent 1592926
@jmikola jmikola authored
View
3  tests/Doctrine/MongoDB/Tests/CollectionTest.php
@@ -425,6 +425,9 @@ public function testGetSetSlaveOkayReadPreferences()
$mongoCollection = $this->getMockMongoCollection();
+ $mongoCollection->expects($this->never())->method('getSlaveOkay');
+ $mongoCollection->expects($this->never())->method('setSlaveOkay');
+
$mongoCollection->expects($this->exactly(2))
->method('getReadPreference')
->will($this->returnValue(array(
View
133 tests/Doctrine/MongoDB/Tests/CursorTest.php
@@ -2,6 +2,10 @@
namespace Doctrine\MongoDB\Tests;
+use Doctrine\MongoDB\Collection;
+use Doctrine\MongoDB\Connection;
+use Doctrine\MongoDB\Cursor;
+
class CursorTest extends BaseTest
{
private $doc1;
@@ -78,4 +82,131 @@ public function testToArrayWithoutKeys()
{
$this->assertEquals(array($this->doc1, $this->doc2, $this->doc3), $this->cursor->toArray(false));
}
-}
+
+ public function testSlaveOkay()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '>=')) {
+ $this->markTestSkipped('This test is not applicable to driver versions >= 1.3.0');
+ }
+
+ $mongoCursor = $this->getMockMongoCursor();
+
+ $mongoCursor->expects($this->at(0))
+ ->method('slaveOkay')
+ ->with(true);
+
+ $mongoCursor->expects($this->at(1))
+ ->method('slaveOkay')
+ ->with(false);
+
+ $cursor = $this->getTestCursor($this->getMockConnection(), $this->getMockCollection(), $mongoCursor);
+
+ $cursor->slaveOkay(true);
+ $cursor->slaveOkay(false);
+ }
+
+ public function testSlaveOkayNoopWithoutReadPreferences()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
+ $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
+ }
+
+ if (method_exists('MongoCursor', 'setReadPreference')) {
+ $this->markTestSkipped('This test is not applicable to drivers with MongoCursor::setReadPreference()');
+ }
+
+ $mongoCursor = $this->getMockMongoCursor();
+
+ $mongoCursor->expects($this->never())
+ ->method('slaveOkay');
+
+ $mongoCursor->expects($this->never())
+ ->method('setReadPreference');
+
+ $cursor = $this->getTestCursor($this->getMockConnection(), $this->getMockCollection(), $mongoCursor);
+
+ $cursor->slaveOkay(true);
+ $cursor->slaveOkay(false);
+ }
+
+ public function testSlaveOkayReadPreferences()
+ {
+ if (!method_exists('MongoCursor', 'setReadPreference')) {
+ $this->markTestSkipped('This test is not applicable to drivers without MongoCursor::setReadPreference()');
+ }
+
+ $mongoCursor = $this->getMockMongoCursor();
+
+ $mongoCursor->expects($this->never())->method('slaveOkay');
+
+ $mongoCursor->expects($this->once())
+ ->method('getReadPreference')
+ ->will($this->returnValue(array(
+ 'type' => \MongoClient::RP_PRIMARY,
+ )));
+
+ $mongoCursor->expects($this->at(1))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_SECONDARY_PREFERRED, array());
+
+ $mongoCursor->expects($this->at(2))
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_PRIMARY);
+
+ $cursor = $this->getTestCursor($this->getMockConnection(), $this->getMockCollection(), $mongoCursor);
+
+ $cursor->slaveOkay(true);
+ $cursor->slaveOkay(false);
+ }
+
+ public function testSlaveOkayPreservesReadPreferenceTags()
+ {
+ if (!method_exists('MongoCursor', 'setReadPreference')) {
+ $this->markTestSkipped('This test is not applicable to drivers without MongoCursor::setReadPreference()');
+ }
+
+ $mongoCursor = $this->getMockMongoCursor();
+
+ $mongoCursor->expects($this->once())
+ ->method('getReadPreference')
+ ->will($this->returnValue(array(
+ 'type' => \MongoClient::RP_PRIMARY_PREFERRED,
+ 'tagsets' => array(array('dc' => 'east')),
+ )));
+
+ $mongoCursor->expects($this->once())
+ ->method('setReadPreference')
+ ->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))
+ ->will($this->returnValue(false));
+
+ $cursor = $this->getTestCursor($this->getMockConnection(), $this->getMockCollection(), $mongoCursor);
+
+ $cursor->slaveOkay(true);
+ }
+
+ private function getMockMongoCursor()
+ {
+ return $this->getMockBuilder('MongoCursor')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ private function getMockCollection()
+ {
+ return $this->getMockBuilder('Doctrine\MongoDB\Collection')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ private function getMockConnection()
+ {
+ return $this->getMockBuilder('Doctrine\MongoDB\Connection')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ private function getTestCursor(Connection $connection, Collection $collection, \MongoCursor $mongoCursor)
+ {
+ return new Cursor($connection, $collection, $mongoCursor);
+ }
+}
View
3  tests/Doctrine/MongoDB/Tests/DatabaseTest.php
@@ -56,6 +56,9 @@ public function testGetSetSlaveOkayReadPreferences()
$this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
}
+ $this->mongodb->expects($this->never())->method('getSlaveOkay');
+ $this->mongodb->expects($this->never())->method('setSlaveOkay');
+
$this->mongodb->expects($this->exactly(2))
->method('getReadPreference')
->will($this->returnValue(array(
Please sign in to comment.
Something went wrong with that request. Please try again.