Преглед изворни кода

Merge branch '1.0.x'

* 1.0.x:
  Add release date for 1.0.8
  Allow using bools for writeConcern
  Expose missing option in indexInfo method
  Add changelog entries
  Use database stored in DBRef object
Andreas Braun пре 9 година
родитељ
комит
92dc6f4536

+ 19 - 0
CHANGELOG-1.0.md

@@ -3,6 +3,25 @@ CHANGELOG
 
 This changelog references the relevant changes done in minor version updates.
 
+1.0.8 (2017-01-11)
+------------------
+
+All issues and pull requests under this release may be found under the
+[1.0.8](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.0.8)
+milestone.
+
+ * [#151](https://github.com/alcaeus/mongo-php-adapter/pull/151) allows using
+ boolean values when passing a write concern option to an insert or update. While
+ never documented, this was happily accepted by the legacy driver.
+ * [#150](https://github.com/alcaeus/mongo-php-adapter/pull/150) adds missing
+ options to `MongoCollection::indexInfo`.
+ * [#149](https://github.com/alcaeus/mongo-php-adapter/pull/149) fixes calls to
+ `MongoDB::getDBRef` with references containing a `$db` field with a different
+ value than the current database.
+ * [#145](https://github.com/alcaeus/mongo-php-adapter/pull/145) fixes query
+ projections in the legacy syntax. While never documented, this was happily
+ accepted by the legacy driver.
+
 1.0.7 (2016-12-18)
 ------------------
 

+ 6 - 1
lib/Alcaeus/MongoDbAdapter/Helper/WriteConcernConverter.php

@@ -18,12 +18,17 @@ namespace Alcaeus\MongoDbAdapter\Helper;
 trait WriteConcernConverter
 {
     /**
-     * @param string|int $wstring
+     * @param string|int|bool $wstring
      * @param int $wtimeout
      * @return \MongoDB\Driver\WriteConcern
      */
     protected function createWriteConcernFromParameters($wstring, $wtimeout)
     {
+        // Convert legacy write concern
+        if (is_bool($wstring)) {
+            $wstring = (int) $wstring;
+        }
+
         if (! is_string($wstring) && ! is_int($wstring)) {
             trigger_error("w for WriteConcern must be a string or integer", E_USER_WARNING);
             return false;

+ 21 - 2
lib/Mongo/MongoCollection.php

@@ -713,8 +713,27 @@ class MongoCollection
                 'ns' => $indexInfo->getNamespace(),
             ];
 
-            if ($indexInfo->isUnique()) {
-                $infos['unique'] = true;
+            $additionalKeys = [
+                'unique',
+                'sparse',
+                'partialFilterExpression',
+                'expireAfterSeconds',
+                'storageEngine',
+                'weights',
+                'default_language',
+                'language_override',
+                'textIndexVersion',
+                'collation',
+                '2dsphereIndexVersion',
+                'bucketSize'
+            ];
+
+            foreach ($additionalKeys as $key) {
+                if (! isset($indexInfo[$key])) {
+                    continue;
+                }
+
+                $infos[$key] = $indexInfo[$key];
             }
 
             return $infos;

+ 2 - 1
lib/Mongo/MongoDB.php

@@ -376,7 +376,8 @@ class MongoDB
      */
     public function getDBRef(array $ref)
     {
-        return MongoDBRef::get($this, $ref);
+        $db = (isset($ref['$db']) && $ref['$db'] !== $this->name) ? $this->connection->selectDB($ref['$db']) : $this;
+        return MongoDBRef::get($db, $ref);
     }
 
     /**

+ 137 - 23
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -132,6 +132,26 @@ class MongoCollectionTest extends TestCase
         $this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
     }
 
+    public function testUnacknowledgedWriteWithBooleanValue()
+    {
+        $document = ['foo' => 'bar'];
+        $this->assertTrue($this->getCollection()->insert($document, ['w' => false]));
+    }
+
+    public function testAcknowledgedWriteConcernWithBool()
+    {
+        $document = ['foo' => 'bar'];
+        $this->assertSame(
+            [
+                'ok' => 1.0,
+                'n' => 0,
+                'err' => null,
+                'errmsg' => null,
+            ],
+            $this->getCollection()->insert($document, ['w' => true])
+        );
+    }
+
     public function testInsertWriteConcernException()
     {
         $this->setExpectedException(
@@ -1222,36 +1242,130 @@ class MongoCollectionTest extends TestCase
         $this->assertSame($expected, $this->getcollection('nonExisting')->deleteIndexes());
     }
 
-    public function testGetIndexInfo()
+    public static function dataGetIndexInfo()
     {
-        $collection = $this->getCollection();
-        $collection->createIndex(['foo' => 1]);
-        $collection->createIndex(['bar' => 1], ['unique' => true]);
-
-        $expected = [
-            [
-                'v' => 1,
-                'key' => ['_id' => 1],
-                'name' => '_id_',
-                'ns' => 'mongo-php-adapter.test',
+        return [
+            'plainIndex' => [
+                'expectedIndex' => [
+                    'v' => 1,
+                    'key' => ['foo' => 1],
+                    'name' => 'foo_1',
+                    'ns' => 'mongo-php-adapter.test',
+                ],
+                'fields' => ['foo' => 1],
+                'options' => [],
             ],
-            [
-                'v' => 1,
-                'key' => ['foo' => 1],
-                'name' => 'foo_1',
-                'ns' => 'mongo-php-adapter.test',
+            'uniqueIndex' => [
+                'expectedIndex' => [
+                    'v' => 1,
+                    'key' => ['foo' => 1],
+                    'name' => 'foo_1',
+                    'ns' => 'mongo-php-adapter.test',
+                    'unique' => true,
+                ],
+                'fields' => ['foo' => 1],
+                'options' => ['unique' => true],
             ],
-            [
-                'v' => 1,
-                'key' => ['bar' => 1],
-                'name' => 'bar_1',
-                'ns' => 'mongo-php-adapter.test',
-                'unique' => true,
+            'sparseIndex' => [
+                'expectedIndex' => [
+                    'v' => 1,
+                    'key' => ['foo' => 1],
+                    'name' => 'foo_1',
+                    'ns' => 'mongo-php-adapter.test',
+                    'sparse' => true,
+                ],
+                'fields' => ['foo' => 1],
+                'options' => ['sparse' => true],
+            ],
+            'ttlIndex' => [
+                'expectedIndex' => [
+                    'v' => 1,
+                    'key' => ['foo' => 1],
+                    'name' => 'foo_1',
+                    'ns' => 'mongo-php-adapter.test',
+                    'expireAfterSeconds' => 86400,
+                ],
+                'fields' => ['foo' => 1],
+                'options' => ['expireAfterSeconds' => 86400],
+            ],
+            'textIndex' => [
+                'expectedIndex' => [
+                    'v' => 1,
+                    'key' => [
+                        '_fts' => 'text',
+                        '_ftsx' => 1,
+                    ],
+                    'name' => 'foo_text',
+                    'ns' => 'mongo-php-adapter.test',
+                    'weights' => [
+                        'foo' => 1,
+                    ],
+                    'default_language' => 'english',
+                    'language_override' => 'language',
+                    'textIndexVersion' => 3,
+                ],
+                'fields' => ['foo' => 'text'],
+                'options' => [],
+            ],
+            'partialFilterExpression' => [
+                'expectedIndex' => [
+                    'v' => 1,
+                    'key' => ['foo' => 1],
+                    'name' => 'foo_1',
+                    'ns' => 'mongo-php-adapter.test',
+                    'partialFilterExpression' => [
+                        'bar' => ['$gt' => 1],
+                    ],
+                ],
+                'fields' => ['foo' => 1],
+                'options' => [
+                    'partialFilterExpression' => ['bar' => ['$gt' => 1]],
+                ],
+            ],
+            'geoSpatial' => [
+                'expectedIndex' => [
+                    'v' => 1,
+                    'key' => ['foo' => '2dsphere'],
+                    'name' => 'foo_2dsphere',
+                    'ns' => 'mongo-php-adapter.test',
+                    '2dsphereIndexVersion' => 3,
+                ],
+                'fields' => ['foo' => '2dsphere'],
+                'options' => [],
+            ],
+            'geoHaystack' => [
+                'expectedIndex' => [
+                    'v' => 1,
+                    'key' => ['foo' => 'geoHaystack', 'bar' => 1],
+                    'name' => 'foo_geoHaystack_bar_1',
+                    'ns' => 'mongo-php-adapter.test',
+                    'bucketSize' => 10,
+                ],
+                'fields' => ['foo' => 'geoHaystack', 'bar' => 1],
+                'options' => ['bucketSize' => 10],
             ],
         ];
+    }
+
+    /**
+     * @dataProvider dataGetIndexInfo
+     */
+    public function testGetIndexInfo($expectedIndex, $fields, $options)
+    {
+        $idIndex = [
+            'v' => 1,
+            'key' => ['_id' => 1],
+            'name' => '_id_',
+            'ns' => 'mongo-php-adapter.test',
+        ];
+
+        $expectedIndexInfo = [$idIndex, $expectedIndex];
+
+        $collection = $this->getCollection();
+        $collection->createIndex($fields, $options);
 
         $this->assertEquals(
-            $expected,
+            $expectedIndexInfo,
             $collection->getIndexInfo()
         );
     }

+ 20 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBRefTest.php

@@ -116,4 +116,24 @@ class MongoDBRefTest extends TestCase
 
         $this->assertNull(\MongoDBRef::get($db, []));
     }
+
+    public function testGetWithDifferentDatabase()
+    {
+        $database = $this->getDatabase();
+        $collection = $this->getCollection();
+
+        $document = ['foo' => 'bar'];
+
+        $collection->insert($document);
+
+        $ref = [
+            '$ref' => $collection->getName(),
+            '$id' => $document['_id'],
+            '$db' => (string) $database,
+        ];
+
+        $referencedDocument = $this->getClient()->selectDB('foo')->getDBRef($ref);
+
+        $this->assertEquals($document, $referencedDocument);
+    }
 }