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

Merge pull request #186 from alcaeus/test-beta1

Test against mongodb-1.3.0beta1
Andreas 8 лет назад
Родитель
Сommit
6b489a77c0

+ 21 - 36
.travis.yml

@@ -1,9 +1,28 @@
+dist: trusty
 sudo: false
 language: php
 
 services:
   - mongodb
 
+php:
+  - 5.6
+  - 7.0
+  - 7.1
+  - 7.2
+
+env:
+  global:
+    - DRIVER_VERSION="stable"
+
+addons:
+  apt:
+    sources:
+      - sourceline: "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse"
+        key_url: "https://www.mongodb.org/static/pgp/server-3.4.asc"
+      - "mongodb-upstart"
+    packages: ['mongodb-org-server']
+
 matrix:
   fast_finish: true
   include:
@@ -12,46 +31,12 @@ matrix:
       addons:
         apt:
           sources:
-            - sourceline: "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.0 multiverse"
+            - sourceline: "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse"
               key_url: "https://www.mongodb.org/static/pgp/server-3.0.asc"
             - "mongodb-upstart"
           packages: ['mongodb-org-server']
-    - php: 5.6
-      env: DRIVER_VERSION=stable
-      addons:
-        apt:
-          sources:
-            - sourceline: "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.4 multiverse"
-              key_url: "https://www.mongodb.org/static/pgp/server-3.4.asc"
-            - "mongodb-upstart"
-          packages: ['mongodb-org-server']
-    - php: 7.0
-      env: DRIVER_VERSION=stable
-      addons:
-        apt:
-          sources:
-            - sourceline: "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.4 multiverse"
-              key_url: "https://www.mongodb.org/static/pgp/server-3.4.asc"
-            - "mongodb-upstart"
-          packages: ['mongodb-org-server']
     - php: 7.1
-      env: DRIVER_VERSION=stable
-      addons:
-        apt:
-          sources:
-            - sourceline: "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.4 multiverse"
-              key_url: "https://www.mongodb.org/static/pgp/server-3.4.asc"
-            - "mongodb-upstart"
-          packages: ['mongodb-org-server']
-    - php: 7.2
-      env: DRIVER_VERSION=stable
-      addons:
-        apt:
-          sources:
-            - sourceline: "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.4 multiverse"
-              key_url: "https://www.mongodb.org/static/pgp/server-3.4.asc"
-            - "mongodb-upstart"
-          packages: ['mongodb-org-server']
+      env: DRIVER_VERSION="1.3.0beta1"
 
 before_install:
   - pecl install -f mongodb-${DRIVER_VERSION}

+ 11 - 0
CHANGELOG-1.1.md

@@ -3,6 +3,17 @@ CHANGELOG for 1.1.x
 
 This changelog references the relevant changes done in minor version updates.
 
+1.1.3 (xxxx-xx-xx)
+------------------
+
+All issues and pull requests under this release may be found under the
+[1.1.3](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.1.3)
+milestone.
+
+ * [#186](https://github.com/alcaeus/mongo-php-adapter/pull/186) fixes errors when
+ using the 1.3 beta version of `ext-mongodb`. It also fixes an issue where new
+ fields in `MongoDB::listCollections` were not properly reported.
+
 1.1.2 (2017-08-04)
 ------------------
 

+ 8 - 7
lib/Mongo/MongoClient.php

@@ -86,6 +86,10 @@ class MongoClient
             $server = 'mongodb://' . self::DEFAULT_HOST . ':' . self::DEFAULT_PORT;
         }
 
+        if (isset($options['readPreferenceTags'])) {
+            $options['readPreferenceTags'] = [$this->getReadPreferenceTags($options['readPreferenceTags'])];
+        }
+
         $this->applyConnectionOptions($server, $options);
 
         $this->server = $server;
@@ -409,13 +413,10 @@ class MongoClient
             unset($options['wTimeout']);
         }
 
-        if (isset($options['readPreferenceTags'])) {
-            $options['readPreferenceTags'] = [$this->getReadPreferenceTags($options['readPreferenceTags'])];
-
-            // Special handling for readPreferenceTags which are merged
-            if (isset($urlOptions['readPreferenceTags'])) {
-                $options['readPreferenceTags'] = array_merge($urlOptions['readPreferenceTags'], $options['readPreferenceTags']);
-            }
+        // Special handling for readPreferenceTags which are merged
+        if (isset($options['readPreferenceTags']) && isset($urlOptions['readPreferenceTags'])) {
+            $options['readPreferenceTags'] = array_merge($urlOptions['readPreferenceTags'], $options['readPreferenceTags']);
+            unset($urlOptions['readPreferenceTags']);
         }
 
         $urlOptions = array_merge($urlOptions, $options);

+ 13 - 4
lib/Mongo/MongoDB.php

@@ -143,10 +143,19 @@ class MongoDB
         }
 
         $getCollectionInfo = function (CollectionInfo $collectionInfo) {
-            return [
-                'name' => $collectionInfo->getName(),
-                'options' => $collectionInfo->getOptions(),
-            ];
+            // @todo do away with __debugInfo once https://jira.mongodb.org/browse/PHPLIB-226 is fixed
+            $info = $collectionInfo->__debugInfo();
+
+            return array_filter(
+                [
+                    'name' => $collectionInfo->getName(),
+                    'type' => isset($info['type']) ? $info['type'] : null,
+                    'options' => $collectionInfo->getOptions(),
+                    'info' => isset($info['info']) ? (array) $info['info'] : null,
+                    'idIndex' => isset($info['idIndex']) ? (array) $info['idIndex'] : null,
+                ],
+                function ($item) { return $item !== null; }
+            );
         };
 
         $eligibleCollections = array_filter(

+ 7 - 5
tests/Alcaeus/MongoDbAdapter/Mongo/MongoBinDataTest.php

@@ -10,10 +10,12 @@ use Alcaeus\MongoDbAdapter\TypeInterface;
  */
 class MongoBinDataTest extends TestCase
 {
+    const GUID = '0123456789abcdef';
+
     public function testCreate()
     {
-        $bin = new \MongoBinData('foo', \MongoBinData::FUNC);
-        $this->assertAttributeSame('foo', 'bin', $bin);
+        $bin = new \MongoBinData(self::GUID, \MongoBinData::FUNC);
+        $this->assertAttributeSame(self::GUID, 'bin', $bin);
         $this->assertAttributeSame(\MongoBinData::FUNC, 'type', $bin);
 
         $this->assertSame('<Mongo Binary Data>', (string)$bin);
@@ -31,7 +33,7 @@ class MongoBinDataTest extends TestCase
         $bsonBinary = $bin->toBSONType();
         $this->assertInstanceOf('MongoDB\BSON\Binary', $bsonBinary);
 
-        $this->assertSame('foo', $bsonBinary->getData());
+        $this->assertSame(self::GUID, $bsonBinary->getData());
         $this->assertSame(\MongoDB\BSON\Binary::TYPE_FUNCTION, $bsonBinary->getType());
     }
 
@@ -39,10 +41,10 @@ class MongoBinDataTest extends TestCase
     {
         $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoBinData')));
 
-        $bsonBinary = new \MongoDB\BSON\Binary('foo', \MongoDB\BSON\Binary::TYPE_UUID);
+        $bsonBinary = new \MongoDB\BSON\Binary(self::GUID, \MongoDB\BSON\Binary::TYPE_UUID);
         $bin = new \MongoBinData($bsonBinary);
 
-        $this->assertAttributeSame('foo', 'bin', $bin);
+        $this->assertAttributeSame(self::GUID, 'bin', $bin);
         $this->assertAttributeSame(\MongoBinData::UUID_RFC4122, 'type', $bin);
     }
 }

+ 13 - 11
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -34,7 +34,7 @@ class MongoCollectionTest extends TestCase
             'errmsg' => null,
         ];
         $document = ['foo' => 'bar'];
-        $this->assertSame($expected, $collection->insert($document));
+        $this->assertEquals($expected, $collection->insert($document));
 
         $this->assertInstanceOf('MongoId', $document['_id']);
         $id = (string) $document['_id'];
@@ -152,7 +152,7 @@ class MongoCollectionTest extends TestCase
     public function testAcknowledgedWriteConcernWithBool()
     {
         $document = ['foo' => 'bar'];
-        $this->assertSame(
+        $this->assertEquals(
             [
                 'ok' => 1.0,
                 'n' => 0,
@@ -316,7 +316,7 @@ class MongoCollectionTest extends TestCase
         ];
 
         $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
-        $this->assertSame($expected, $result);
+        $this->assertEquals($expected, $result);
 
         $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
     }
@@ -340,7 +340,7 @@ class MongoCollectionTest extends TestCase
         ];
 
         $result = $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo']);
-        $this->assertSame($expected, $result);
+        $this->assertEquals($expected, $result);
 
         $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
         $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['bar' => 'foo']));
@@ -387,7 +387,7 @@ class MongoCollectionTest extends TestCase
         ];
 
         $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
-        $this->assertSame($expected, $result);
+        $this->assertEquals($expected, $result);
 
         $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
     }
@@ -420,7 +420,7 @@ class MongoCollectionTest extends TestCase
         ];
 
         $result = $this->getCollection()->remove(['foo' => 'bar']);
-        $this->assertSame($expected, $result);
+        $this->assertEquals($expected, $result);
 
         $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
     }
@@ -441,7 +441,7 @@ class MongoCollectionTest extends TestCase
         ];
 
         $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
-        $this->assertSame($expected, $result);
+        $this->assertEquals($expected, $result);
 
         $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
     }
@@ -665,7 +665,7 @@ class MongoCollectionTest extends TestCase
     {
         $this->expectException(\MongoConnectionException::class);
 
-        $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
+        $client = $this->getClient([], 'mongodb://localhost:28888/?connectTimeoutMS=1');
         $collection = $client->selectCollection('mongo-php-adapter', 'test');
 
         $collection->findOne();
@@ -976,7 +976,7 @@ class MongoCollectionTest extends TestCase
         $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
         $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
         $collection->insert($insertDocument);
-        $this->assertSame($expected, $collection->save($saveDocument));
+        $this->assertEquals($expected, $collection->save($saveDocument));
 
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
         $this->assertSame(1, $newCollection->count());
@@ -1267,7 +1267,8 @@ class MongoCollectionTest extends TestCase
             $expected['code'] = 27;
         }
 
-        $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
+        // Using assertArraySubset because newer versions (3.4.7?) also return `codeName`
+        $this->assertArraySubset($expected, $this->getCollection()->deleteIndex('bar'));
 
         $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
     }
@@ -1326,7 +1327,8 @@ class MongoCollectionTest extends TestCase
             $expected['code'] = 26;
         }
 
-        $this->assertSame($expected, $this->getCollection('nonExisting')->deleteIndexes());
+        // Using assertArraySubset because newer versions (3.4.7?) also return `codeName`
+        $this->assertArraySubset($expected, $this->getCollection('nonExisting')->deleteIndexes());
     }
 
     public function dataGetIndexInfo()

+ 20 - 2
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php

@@ -126,7 +126,8 @@ class MongoDBTest extends TestCase
             'code' => 13,
         ];
 
-        $this->assertEquals($expected, $db->command(['listDatabases' => 1]));
+        // Using assertArraySubset because newer versions (3.4.7?) also return `codeName`
+        $this->assertArraySubset($expected, $db->command(['listDatabases' => 1]));
     }
 
     public function testCommandCursorTimeout()
@@ -273,7 +274,24 @@ class MongoDBTest extends TestCase
 
         foreach ($this->getDatabase()->getCollectionInfo() as $collectionInfo) {
             if ($collectionInfo['name'] === 'test') {
-                $this->assertSame(['name' => 'test', 'options' => []], $collectionInfo);
+                $expected = [
+                    'name' => 'test',
+                    'options' => []
+                ];
+
+                if (version_compare($this->getServerVersion(), '3.4.0', '>=')) {
+                    $expected += [
+                        'type' => 'collection',
+                        'info' => ['readOnly' => false],
+                        'idIndex' => [
+                            'v' => $this->getDefaultIndexVersion(),
+                            'key' => ['_id' => 1],
+                            'name' => '_id_',
+                            'ns' => (string) $this->getCollection(),
+                        ],
+                    ];
+                }
+                $this->assertEquals($expected, $collectionInfo);
                 return;
             }
         }

+ 19 - 1
tests/Alcaeus/MongoDbAdapter/TestCase.php

@@ -7,6 +7,9 @@ use PHPUnit\Framework\TestCase as BaseTestCase;
 
 abstract class TestCase extends BaseTestCase
 {
+    const INDEX_VERSION_1 = 1;
+    const INDEX_VERSION_2 = 2;
+
     protected function tearDown()
     {
         $this->getCheckDatabase()->drop();
@@ -175,12 +178,27 @@ abstract class TestCase extends BaseTestCase
     }
 
     /**
+     * @return string
+     */
+    protected function getFeatureCompatibilityVersion()
+    {
+        $featureCompatibilityVersion = $this->getClient()->selectDB('admin')->command(['getParameter' => true, 'featureCompatibilityVersion' => true]);
+        return isset($featureCompatibilityVersion['featureCompatibilityVersion']) ? $featureCompatibilityVersion['featureCompatibilityVersion'] : '3.2';
+    }
+
+    /**
      * Indexes created in MongoDB 3.4 default to v: 2.
      * @return int
      * @see https://docs.mongodb.com/manual/release-notes/3.4-compatibility/#backwards-incompatible-features
      */
     protected function getDefaultIndexVersion()
     {
-        return version_compare($this->getServerVersion(), '3.4.0', '>=') ? 2 : 1;
+        if (version_compare($this->getServerVersion(), '3.4.0', '<')) {
+            self::INDEX_VERSION_1;
+        }
+
+        // Check featureCompatibilityFlag
+        $compatibilityVersion = $this->getFeatureCompatibilityVersion();
+        return $compatibilityVersion === '3.4' ? self::INDEX_VERSION_2 : self::INDEX_VERSION_1;
     }
 }