Browse Source

Fix the Collection::getIndexInfo method for missing "unique" option

Damien ALEXANDRE 9 years ago
parent
commit
05a26b43c1

+ 7 - 2
lib/Mongo/MongoCollection.php

@@ -706,12 +706,18 @@ class MongoCollection
     public function getIndexInfo()
     {
         $convertIndex = function(\MongoDB\Model\IndexInfo $indexInfo) {
-            return [
+            $infos = [
                 'v' => $indexInfo->getVersion(),
                 'key' => $indexInfo->getKey(),
                 'name' => $indexInfo->getName(),
                 'ns' => $indexInfo->getNamespace(),
             ];
+
+            if ($indexInfo->isUnique()) {
+                $infos['unique'] = true;
+            }
+
+            return $infos;
         };
 
         return array_map($convertIndex, iterator_to_array($this->collection->listIndexes()));
@@ -1010,4 +1016,3 @@ class MongoCollection
         return ['db', 'name'];
     }
 }
-

+ 9 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -1177,6 +1177,7 @@ class MongoCollectionTest extends TestCase
     {
         $collection = $this->getCollection();
         $collection->createIndex(['foo' => 1]);
+        $collection->createIndex(['bar' => 1], ['unique' => true]);
 
         $expected = [
             [
@@ -1191,9 +1192,16 @@ class MongoCollectionTest extends TestCase
                 'name' => 'foo_1',
                 'ns' => 'mongo-php-adapter.test',
             ],
+            [
+                'v' => 1,
+                'key' => ['bar' => 1],
+                'name' => 'bar_1',
+                'ns' => 'mongo-php-adapter.test',
+                'unique' => true,
+            ],
         ];
 
-        $this->assertSame(
+        $this->assertEquals(
             $expected,
             $collection->getIndexInfo()
         );