Bläddra i källkod

Merge pull request #150 from alcaeus/missing-index-info

Expose missing option in indexInfo method
Andreas 9 år sedan
förälder
incheckning
33158d5e3f

+ 2 - 0
CHANGELOG-1.0.md

@@ -10,6 +10,8 @@ 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.
 
+ * [#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.

+ 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;

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

@@ -1222,36 +1222,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()
         );
     }