Jelajahi Sumber

Stabilize MongoDB class

Andreas Braun 10 tahun lalu
induk
melakukan
6301ae8176
2 mengubah file dengan 20 tambahan dan 31 penghapusan
  1. 7 8
      lib/Mongo/MongoDB.php
  2. 13 23
      tests/Alcaeus/MongoDbAdapter/MongoDBTest.php

+ 7 - 8
lib/Mongo/MongoDB.php

@@ -112,8 +112,7 @@ class MongoDB
     public function __set($name, $value)
     {
         if ($name === 'w' || $name === 'wtimeout') {
-            $this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern());
-            $this->createDatabaseObject();
+            trigger_error("The '{$name}' property is read-only", E_DEPRECATED);
         }
     }
 
@@ -270,6 +269,10 @@ class MongoDB
     public function createCollection($name, $options)
     {
         try {
+            if (isset($options['capped'])) {
+                $options['capped'] = (bool) $options['capped'];
+            }
+
             $this->db->createCollection($name, $options);
         } catch (\MongoDB\Driver\Exception\Exception $e) {
             return false;
@@ -380,16 +383,12 @@ class MongoDB
             $cursor->setReadPreference($this->getReadPreference());
 
             return iterator_to_array($cursor)[0];
-        } catch (\MongoDB\Driver\Exception\ExecutionTimeoutException $e) {
-            throw new MongoCursorTimeoutException($e->getMessage(), $e->getCode(), $e);
-        } catch (\MongoDB\Driver\Exception\RuntimeException $e) {
+        } catch (\MongoDB\Driver\Exception\Exception $e) {
             return [
-                'ok' => 0,
+                'ok' => 0.0,
                 'errmsg' => $e->getMessage(),
                 'code' => $e->getCode(),
             ];
-        } catch (\MongoDB\Driver\Exception\Exception $e) {
-            throw ExceptionConverter::toLegacy($e);
         }
     }
 

+ 13 - 23
tests/Alcaeus/MongoDbAdapter/MongoDBTest.php

@@ -71,7 +71,7 @@ class MongoDBTest extends TestCase
     {
         $database = $this->getDatabase();
 
-        $this->assertFalse($database->createCollection('test', ['capped' => 2, 'size' => 100]));
+        $this->assertInstanceOf('MongoCollection', $database->createCollection('test', ['capped' => 2, 'size' => 100]));
     }
 
     public function testGetCollectionProperty()
@@ -106,13 +106,17 @@ class MongoDBTest extends TestCase
 
         $this->failMaxTimeMS();
 
-        $this->setExpectedException('MongoCursorTimeoutException');
-
-        $database->command([
+        $result = $database->command([
             "count" => "test",
             "query" => array("a" => 1),
             "maxTimeMS" => 100,
         ]);
+
+        $this->assertSame([
+            'ok' => 0.0,
+            'errmsg' => 'operation exceeded time limit',
+            'code' => 50,
+        ], $result);
     }
 
     public function testReadPreference()
@@ -129,7 +133,8 @@ class MongoDBTest extends TestCase
         $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());
+        // Only test a subset since we don't keep tagsets around for RP_PRIMARY
+        $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $database->getReadPreference());
     }
 
     public function testReadPreferenceIsSetInDriver()
@@ -160,24 +165,9 @@ class MongoDBTest extends TestCase
     public function testWriteConcern()
     {
         $database = $this->getDatabase();
-        $this->assertSame(['w' => 1, 'wtimeout' => 0], $database->getWriteConcern());
-        $this->assertSame(1, $database->w);
-        $this->assertSame(0, $database->wtimeout);
 
         $this->assertTrue($database->setWriteConcern('majority', 100));
         $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
-
-        $database->w = 2;
-        $this->assertSame(['w' => 2, 'wtimeout' => 100], $database->getWriteConcern());
-
-        $database->wtimeout = -1;
-        $this->assertSame(['w' => 2, 'wtimeout' => 0], $database->getWriteConcern());
-
-        // Only way to check whether options are passed down is through debugInfo
-        $writeConcern = $database->getDb()->__debugInfo()['writeConcern'];
-
-        $this->assertSame(2, $writeConcern->getW());
-        $this->assertSame(0, $writeConcern->getWtimeout());
     }
 
     public function testWriteConcernIsSetInDriver()
@@ -197,10 +187,10 @@ class MongoDBTest extends TestCase
     public function testWriteConcernIsInherited()
     {
         $client = $this->getClient();
-        $client->setWriteConcern('majority', 100);
+        $client->setWriteConcern(2, 100);
 
         $database = $client->selectDB('test');
-        $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $database->getWriteConcern());
+        $this->assertSame(['w' => 2, 'wtimeout' => 100], $database->getWriteConcern());
     }
 
     public function testProfilingLevel()
@@ -216,7 +206,7 @@ class MongoDBTest extends TestCase
     public function testForceError()
     {
         $result = $this->getDatabase()->forceError();
-        $this->assertSame(0, $result['ok']);
+        $this->assertSame(0.0, $result['ok']);
     }
 
     public function testExecute()