Kaynağa Gözat

Ensure correct return types for index methods

Andreas Braun 10 yıl önce
ebeveyn
işleme
db5d912803

+ 15 - 10
lib/Mongo/MongoCollection.php

@@ -437,9 +437,19 @@ class MongoCollection
      * @param array $keys Field or fields to use as index.
      * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
      * @return array Returns the database response.
+     *
+     * @todo This method does not yet return the correct result
      */
-    public function createIndex(array $keys, array $options = array())
+    public function createIndex(array $keys, array $options = [])
     {
+        // Note: this is what the result array should look like
+//        $expected = [
+//            'createdCollectionAutomatically' => true,
+//            'numIndexesBefore' => 1,
+//            'numIndexesAfter' => 2,
+//            'ok' => 1.0
+//        ];
+
         return $this->collection->createIndex($keys, $options);
     }
 
@@ -472,7 +482,8 @@ class MongoCollection
         } else {
             throw new \InvalidArgumentException();
         }
-        return $this->collection->dropIndex($indexName);
+
+        return TypeConverter::convertObjectToLegacyArray($this->collection->dropIndex($indexName));
     }
 
     /**
@@ -482,7 +493,7 @@ class MongoCollection
      */
     public function deleteIndexes()
     {
-        return $this->collection->dropIndexes();
+        return TypeConverter::convertObjectToLegacyArray($this->collection->dropIndexes());
     }
 
     /**
@@ -626,13 +637,7 @@ class MongoCollection
             $command['group']['finalize'] = $condition['finalize'];
         }
 
-        $result = $this->db->command($command, [], $hash);
-        unset($result['waitedMS']);
-        $result['ok'] = (int)$result['ok'];
-        if (count($result['retval'])) {
-            $result['retval'] = current($result['retval']);
-        }
-        return $result;
+        return $this->db->command($command, [], $hash);
     }
 
     /**

+ 43 - 21
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -293,51 +293,70 @@ class MongoCollectionTest extends TestCase
 
     public function testDeleteIndexUsingIndexName()
     {
-        $collection = $this->getCollection();
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
-
         $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
-        $collection->deleteIndex('bar');
+
+        $expected = [
+            'nIndexesWas' => 2,
+            'ok' => 1.0,
+        ];
+        $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
 
         $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
     }
 
-    public function testdeleteindexusingkeys()
+    public function testDeleteIndexUsingKeys()
     {
-        $collection = $this->getcollection();
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
-
         $newCollection->createIndex(['bar' => 1]);
-        $collection->deleteIndex(['bar' => 1]);
+
+        $expected = [
+            'nIndexesWas' => 2,
+            'ok' => 1.0,
+        ];
+        $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
 
         $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
     }
 
     public function testDeleteIndexes()
     {
-        $collection = $this->getcollection();
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
-
         $newCollection->createIndex(['bar' => 1]);
-        $collection->deleteIndexes();
+
+        $expected = [
+            'nIndexesWas' => 2,
+            'msg' => 'non-_id indexes dropped for collection',
+            'ok' => 1.0,
+        ];
+        $this->assertSame($expected, $this->getcollection()->deleteIndexes());
 
         $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
     }
 
     public function testGetIndexInfo()
     {
-        $this->prepareData();
+        $collection = $this->getCollection();
+        $collection->createIndex(['foo' => 1]);
 
-        $this->assertSame(
+        $expected = [
             [
-                [
-                    'v'    => 1,
-                    'key'  => ['_id' => 1],
-                    'name' => '_id_',
-                    'ns'   => 'mongo-php-adapter.test',
-                ],
+                'v' => 1,
+                'key' => ['_id' => 1],
+                'name' => '_id_',
+                'ns' => 'mongo-php-adapter.test',
             ],
-            $this->getcollection()->getIndexInfo()
+            [
+                'v' => 1,
+                'key' => ['foo' => 1],
+                'name' => 'foo_1',
+                'ns' => 'mongo-php-adapter.test',
+            ],
+        ];
+
+        $this->assertSame(
+            $expected,
+            $collection->getIndexInfo()
         );
     }
 
@@ -411,10 +430,11 @@ class MongoCollectionTest extends TestCase
 
         $this->assertEquals(
             [
-                'retval' => ['count' => 1],
+                'waitedMS' => 0,
+                'retval' => [['count' => 1.0]],
                 'count'  => 1,
                 'keys'   => 1,
-                'ok'     => 1,
+                'ok'     => 1.0,
             ],
             $result
         );
@@ -433,6 +453,8 @@ class MongoCollectionTest extends TestCase
             ['remove' => true]
         );
 
+        $this->assertEquals('bar', $document['foo']);
+
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
         $this->assertSame(0, $newCollection->count());
     }