Browse Source

Merge pull request #85 from alcaeus/count-legacy-arguments

Allow calling count with limit and skip parameters
Andreas 9 năm trước cách đây
mục cha
commit
d61d6811f4

+ 15 - 1
lib/Mongo/MongoCollection.php

@@ -724,9 +724,23 @@ class MongoCollection
      * @param array $options
      * @return int Returns the number of documents matching the query.
      */
-    public function count($query = [], array $options = [])
+    public function count($query = [], $options = [])
     {
         try {
+            // Handle legacy mode - limit and skip as second and third parameters, respectively
+            if (! is_array($options)) {
+                $limit = $options;
+                $options = [];
+
+                if ($limit !== null) {
+                    $options['limit'] = (int) $limit;
+                }
+
+                if (func_num_args() > 2) {
+                    $options['skip'] = (int) func_get_args()[2];
+                }
+            }
+
             return $this->collection->count(TypeConverter::fromLegacy($query), $options);
         } catch (\MongoDB\Driver\Exception\Exception $e) {
             throw ExceptionConverter::toLegacy($e);

+ 54 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -439,6 +439,60 @@ class MongoCollectionTest extends TestCase
         $this->assertSame(2, $collection->count(['foo' => 'bar']));
     }
 
+    public function testCountWithLimit()
+    {
+        $this->prepareData();
+        $collection = $this->getCollection();
+
+        $this->assertSame(2, $collection->count([], ['limit' => 2]));
+        $this->assertSame(1, $collection->count(['foo' => 'bar'], ['limit' => 1]));
+    }
+
+    public function testCountWithLimitLegacy()
+    {
+        $this->prepareData();
+        $collection = $this->getCollection();
+
+        $this->assertSame(2, $collection->count([], 2));
+        $this->assertSame(1, $collection->count(['foo' => 'bar'], 1));
+    }
+
+    public function testCountWithSkip()
+    {
+        $this->prepareData();
+        $collection = $this->getCollection();
+
+        $this->assertSame(2, $collection->count([], ['skip' => 1]));
+        $this->assertSame(1, $collection->count(['foo' => 'bar'], ['skip' => 1]));
+    }
+
+    public function testCountWithSkipLegacy()
+    {
+        $this->prepareData();
+        $collection = $this->getCollection();
+
+        $this->assertSame(2, $collection->count([], null, 1));
+        $this->assertSame(1, $collection->count(['foo' => 'bar'], null, 1));
+    }
+
+    public function testCountWithLimitAndSkip()
+    {
+        $this->prepareData();
+        $collection = $this->getCollection();
+
+        $this->assertSame(2, $collection->count([], ['skip' => 1, 'limit' => 2]));
+        $this->assertSame(1, $collection->count([], ['skip' => 1, 'limit' => 1]));
+    }
+
+    public function testCountWithLimitAndSkipLegacy()
+    {
+        $this->prepareData();
+        $collection = $this->getCollection();
+
+        $this->assertSame(2, $collection->count([], 2, 1));
+        $this->assertSame(1, $collection->count([], 1, 1));
+    }
+
     public function testCountTimeout()
     {
         $this->failMaxTimeMS();