Example #1
0
 public function testSetReadPreference()
 {
     $mongoDB = $this->getMockMongoDB();
     $mongoDB->expects($this->at(0))->method('setReadPreference')->with(\MongoClient::RP_PRIMARY)->will($this->returnValue(true));
     $mongoDB->expects($this->at(1))->method('setReadPreference')->with(\MongoClient::RP_SECONDARY_PREFERRED, [['dc' => 'east']])->will($this->returnValue(true));
     $database = new Database($this->getMockConnection(), $mongoDB, $this->getMockEventManager());
     $this->assertTrue($database->setReadPreference(\MongoClient::RP_PRIMARY));
     $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, [['dc' => 'east']]));
 }
Example #2
0
 public function testSetReadPreference()
 {
     if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
         $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
     }
     $mongoDB = $this->getMockMongoDB();
     $mongoDB->expects($this->at(0))->method('setReadPreference')->with(\MongoClient::RP_PRIMARY)->will($this->returnValue(true));
     $mongoDB->expects($this->at(1))->method('setReadPreference')->with(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east')))->will($this->returnValue(true));
     $database = new Database($this->getMockConnection(), $mongoDB, $this->getMockEventManager());
     $this->assertTrue($database->setReadPreference(\MongoClient::RP_PRIMARY));
     $this->assertTrue($database->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, array(array('dc' => 'east'))));
 }
Example #3
0
 /**
  * Executes a closure with a temporary read preference on a database or
  * collection.
  *
  * @param Database|Collection $object
  * @param \Closure            $closure
  * @return mixed
  */
 private function withReadPreference($object, \Closure $closure)
 {
     if (!isset($this->query['readPreference'])) {
         return $closure();
     }
     $prevReadPref = $object->getReadPreference();
     $object->setReadPreference($this->query['readPreference'], $this->query['readPreferenceTags']);
     try {
         $result = $closure();
     } catch (\Exception $e) {
     }
     $prevTags = !empty($prevReadPref['tagsets']) ? $prevReadPref['tagsets'] : null;
     $object->setReadPreference($prevReadPref['type'], $prevTags);
     if (isset($e)) {
         throw $e;
     }
     return $result;
 }