Просмотр исходного кода

Use slaveOkay helper in classes

Note: Cursors will not use new helper since they behave differently
Andreas Braun 10 лет назад
Родитель
Сommit
569c3e6f0d

+ 1 - 19
lib/Mongo/MongoCollection.php

@@ -23,6 +23,7 @@ use Alcaeus\MongoDbAdapter\TypeConverter;
 class MongoCollection
 {
     use Helper\ReadPreference;
+    use Helper\SlaveOkay;
     use Helper\WriteConcern;
 
     const ASCENDING = 1;
@@ -186,25 +187,6 @@ class MongoCollection
     }
 
     /**
-     * @link http://www.php.net/manual/en/mongocollection.getslaveokay.php
-     * @return bool
-     */
-    public function getSlaveOkay()
-    {
-        $this->notImplemented();
-    }
-
-    /**
-     * @link http://www.php.net/manual/en/mongocollection.setslaveokay.php
-     * @param bool $ok
-     * @return bool
-     */
-    public function setSlaveOkay($ok = true)
-    {
-        $this->notImplemented();
-    }
-
-    /**
      * {@inheritdoc}
      */
     public function setReadPreference($readPreference, $tags = null)

+ 2 - 4
lib/Mongo/MongoCursor.php

@@ -326,12 +326,10 @@ class MongoCursor extends AbstractCursor implements Iterator
     public function slaveOkay($okay = true)
     {
         $this->errorIfOpened();
-        static::$slaveOkay = $okay;
 
-        $readPreferenceArray = $this->getReadPreference();
-        $readPreferenceArray['type'] = $okay ? \MongoClient::RP_SECONDARY_PREFERRED : \MongoClient::RP_PRIMARY;
+        $this->setReadPreferenceFromSlaveOkay($okay);
 
-        $this->setReadPreferenceFromArray($readPreferenceArray);
+        return $this;
     }
 
     /**

+ 1 - 26
lib/Mongo/MongoDB.php

@@ -23,6 +23,7 @@ use MongoDB\Model\CollectionInfo;
 class MongoDB
 {
     use Helper\ReadPreference;
+    use Helper\SlaveOkay;
     use Helper\WriteConcern;
 
     const PROFILING_OFF = 0;
@@ -168,17 +169,6 @@ class MongoDB
     }
 
     /**
-     * (PECL mongo &gt;= 1.1.0)<br/>
-     * Get slaveOkay setting for this database
-     * @link http://www.php.net/manual/en/mongodb.getslaveokay.php
-     * @return bool Returns the value of slaveOkay for this instance.
-     */
-    public function getSlaveOkay()
-    {
-        return false;
-    }
-
-    /**
      * (PECL mongo &gt;= 0.9.0)<br/>
      * Sets this database's profiling level
      * @link http://www.php.net/manual/en/mongodb.setprofilinglevel.php
@@ -229,21 +219,6 @@ class MongoDB
     }
 
     /**
-     * (PECL mongo &gt;= 1.1.0)<br/>
-     * Change slaveOkay setting for this database
-     * @link http://php.net/manual/en/mongodb.setslaveokay.php
-     * @param bool $ok [optional] <p>
-     * If reads should be sent to secondary members of a replica set for all
-     * possible queries using this {@link http://www.php.net/manual/en/class.mongodb.php MongoDB} instance.
-     * </p>
-     * @return bool Returns the former value of slaveOkay for this instance.
-     */
-    public function setSlaveOkay ($ok = true)
-    {
-        return false;
-    }
-
-    /**
      * Creates a collection
      * @link http://www.php.net/manual/en/mongodb.createcollection.php
      * @param string $name The name of the collection.

+ 8 - 0
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -141,15 +141,23 @@ class MongoCollectionTest extends TestCase
     {
         $collection = $this->getCollection();
         $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
+        $this->assertFalse($collection->getSlaveOkay());
 
         $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
         $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
+        $this->assertTrue($collection->getSlaveOkay());
 
         // Only way to check whether options are passed down is through debugInfo
         $writeConcern = $collection->getCollection()->__debugInfo()['readPreference'];
 
         $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
         $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
+
+        $this->assertTrue($collection->setSlaveOkay(true));
+        $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
+
+        $this->assertTrue($collection->setSlaveOkay(false));
+        $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
     }
 
     public function testReadPreferenceIsInherited()

+ 8 - 0
tests/Alcaeus/MongoDbAdapter/MongoDBTest.php

@@ -46,15 +46,23 @@ class MongoDBTest extends TestCase
     {
         $database = $this->getDatabase();
         $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
+        $this->assertFalse($database->getSlaveOkay());
 
         $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
         $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
+        $this->assertTrue($database->getSlaveOkay());
 
         // Only way to check whether options are passed down is through debugInfo
         $writeConcern = $database->getDb()->__debugInfo()['readPreference'];
 
         $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
         $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
+
+        $this->assertTrue($database->setSlaveOkay(true));
+        $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $database->getReadPreference());
+
+        $this->assertTrue($database->setSlaveOkay(false));
+        $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
     }
 
     public function testReadPreferenceIsInherited()