From 53402e417e5666ae97b06f8adde56e800c01ba1f Mon Sep 17 00:00:00 2001 From: Ernando Souza Date: Thu, 23 Aug 2018 12:00:34 -0300 Subject: [PATCH 1/3] Allows Cursor to choose a member of the ReplicaSet --- src/Mongolid/Cursor/Cursor.php | 19 +++++++++++++++++++ tests/Mongolid/Cursor/CursorTest.php | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Mongolid/Cursor/Cursor.php b/src/Mongolid/Cursor/Cursor.php index fb83503e..2409b8f3 100644 --- a/src/Mongolid/Cursor/Cursor.php +++ b/src/Mongolid/Cursor/Cursor.php @@ -147,6 +147,25 @@ public function disableTimeout(bool $flag = true) return $this; } + /** + * + * This describes how the Cursor route the future read operations to the members of a replica set. + * + * @link http://php.net/manual/pt_BR/class.mongodb-driver-readpreference.php + * + * @param int $mode Preference mode that the Cursor will use. + * + * @see ReadPreference::class To get a glance of the constants available + * + * @return $this + */ + public function setReadPreference(int $mode) + { + $this->params[1]['readPreference'] = new ReadPreference($mode); + + return $this; + } + /** * Counts the number of results for this cursor. * diff --git a/tests/Mongolid/Cursor/CursorTest.php b/tests/Mongolid/Cursor/CursorTest.php index 48539d7a..44c35453 100644 --- a/tests/Mongolid/Cursor/CursorTest.php +++ b/tests/Mongolid/Cursor/CursorTest.php @@ -82,6 +82,19 @@ public function testShouldSetNoCursorTimeoutToTrue() ); } + public function testShouldSetReadPreferenceParameterAccordingly() + { + // Arrange + $cursor = $this->getCursor(); + $mode = ReadPreference::RP_SECONDARY; + $cursor->setReadPreference($mode); + $readPreferenceParameter = $this->getProtected($cursor, 'params')[1]['readPreference']; + + // Assert + $this->assertInstanceOf(ReadPreference::class, $readPreferenceParameter); + $this->assertSame($readPreferenceParameter->getMode(), $mode); + } + public function testShouldCountDocuments() { // Arrange From 1a14efbf9a120c79024611c7580aef6ce17a2001 Mon Sep 17 00:00:00 2001 From: Ernando Souza Date: Thu, 23 Aug 2018 12:10:25 -0300 Subject: [PATCH 2/3] Minor adjustments --- src/Mongolid/Cursor/Cursor.php | 5 +++-- tests/Mongolid/Cursor/CursorTest.php | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Mongolid/Cursor/Cursor.php b/src/Mongolid/Cursor/Cursor.php index 2409b8f3..59444b03 100644 --- a/src/Mongolid/Cursor/Cursor.php +++ b/src/Mongolid/Cursor/Cursor.php @@ -3,6 +3,7 @@ namespace Mongolid\Cursor; use IteratorIterator; +use MongoDB\Driver\ReadPreference; use Serializable; use Traversable; use MongoDB\Collection; @@ -151,9 +152,9 @@ public function disableTimeout(bool $flag = true) * * This describes how the Cursor route the future read operations to the members of a replica set. * - * @link http://php.net/manual/pt_BR/class.mongodb-driver-readpreference.php + * @see http://php.net/manual/pt_BR/class.mongodb-driver-readpreference.php * - * @param int $mode Preference mode that the Cursor will use. + * @param int $mode preference mode that the Cursor will use. * * @see ReadPreference::class To get a glance of the constants available * diff --git a/tests/Mongolid/Cursor/CursorTest.php b/tests/Mongolid/Cursor/CursorTest.php index 44c35453..6ff26933 100644 --- a/tests/Mongolid/Cursor/CursorTest.php +++ b/tests/Mongolid/Cursor/CursorTest.php @@ -8,6 +8,7 @@ use Mockery as m; use MongoDB\Collection; use MongoDB\Driver\Exception\LogicException; +use MongoDB\Driver\ReadPreference; use Mongolid\ActiveRecord; use Mongolid\Connection\Connection; use Mongolid\Connection\Pool; From b4953ce411e34d99a46d31b96ad412eb7055e179 Mon Sep 17 00:00:00 2001 From: Ernando Souza Date: Thu, 23 Aug 2018 12:13:24 -0300 Subject: [PATCH 3/3] Apply fixes from StyleCI --- src/Mongolid/Cursor/Cursor.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Mongolid/Cursor/Cursor.php b/src/Mongolid/Cursor/Cursor.php index 59444b03..16eb42d0 100644 --- a/src/Mongolid/Cursor/Cursor.php +++ b/src/Mongolid/Cursor/Cursor.php @@ -149,12 +149,11 @@ public function disableTimeout(bool $flag = true) } /** - * * This describes how the Cursor route the future read operations to the members of a replica set. * * @see http://php.net/manual/pt_BR/class.mongodb-driver-readpreference.php * - * @param int $mode preference mode that the Cursor will use. + * @param int $mode preference mode that the Cursor will use * * @see ReadPreference::class To get a glance of the constants available *