瀏覽代碼

Fix readPreference and writeConcern in MongoClient

Andreas Braun 10 年之前
父節點
當前提交
0f7935c735
共有 2 個文件被更改,包括 32 次插入26 次删除
  1. 14 26
      lib/Mongo/MongoClient.php
  2. 18 0
      tests/Alcaeus/MongoDbAdapter/MongoClientTest.php

+ 14 - 26
lib/Mongo/MongoClient.php

@@ -13,6 +13,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
  */
 
 
+use Alcaeus\MongoDbAdapter\Helper;
 use MongoDB\Client;
 use MongoDB\Client;
 
 
 /**
 /**
@@ -22,6 +23,9 @@ use MongoDB\Client;
  */
  */
 class MongoClient
 class MongoClient
 {
 {
+    use Helper\ReadPreference;
+    use Helper\WriteConcern;
+
     const VERSION = '1.6.12';
     const VERSION = '1.6.12';
     const DEFAULT_HOST = "localhost" ;
     const DEFAULT_HOST = "localhost" ;
     const DEFAULT_PORT = 27017 ;
     const DEFAULT_PORT = 27017 ;
@@ -172,26 +176,6 @@ class MongoClient
     }
     }
 
 
     /**
     /**
-     * Get the read preference for this connection
-     *
-     * @return array
-     */
-    public function getReadPreference()
-    {
-        return [];
-    }
-
-    /**
-     * Get the write concern for this connection
-     *
-     * @return array Returns an array describing the write concern.
-     */
-    public function getWriteConcern()
-    {
-        return [];
-    }
-
-    /**
      * Kills a specific cursor on the server
      * Kills a specific cursor on the server
      *
      *
      * @link http://www.php.net/manual/en/mongoclient.killcursor.php
      * @link http://www.php.net/manual/en/mongoclient.killcursor.php
@@ -246,15 +230,19 @@ class MongoClient
     }
     }
 
 
     /**
     /**
-     * Set read preference
-     *
-     * @param string $readPreference
-     * @param array $tags
-     * @return bool
+     * {@inheritdoc}
      */
      */
     public function setReadPreference($readPreference, $tags = null)
     public function setReadPreference($readPreference, $tags = null)
     {
     {
-        return false;
+        return $this->setReadPreferenceFromParameters($readPreference, $tags);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setWriteConcern($wstring, $wtimeout = 0)
+    {
+        return $this->setWriteConcernFromParameters($wstring, $wtimeout);
     }
     }
 
 
     /**
     /**

+ 18 - 0
tests/Alcaeus/MongoDbAdapter/MongoClientTest.php

@@ -46,6 +46,24 @@ class MongoClientTest extends TestCase
         $this->assertSame('mongo-php-adapter.test', (string) $collection);
         $this->assertSame('mongo-php-adapter.test', (string) $collection);
     }
     }
 
 
+    public function testReadPreference()
+    {
+        $client = $this->getClient();
+        $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $client->getReadPreference());
+
+        $this->assertTrue($client->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
+        $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $client->getReadPreference());
+    }
+
+    public function testWriteConcern()
+    {
+        $client = $this->getClient();
+        $this->assertSame(['w' => 1, 'wtimeout' => 0], $client->getWriteConcern());
+
+        $this->assertTrue($client->setWriteConcern('majority', 100));
+        $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $client->getWriteConcern());
+    }
+
     /**
     /**
      * @param array|null $options
      * @param array|null $options
      * @return \MongoClient
      * @return \MongoClient