Просмотр исходного кода

Merge branch '1.0.x'

* 1.0.x:
  Add latest PR to changelog
  Allow calling count with limit and skip parameters
Andreas Braun 9 лет назад
Родитель
Сommit
4fed2d46df

+ 12 - 0
CHANGELOG-1.0.md

@@ -3,6 +3,18 @@ CHANGELOG
 
 This changelog references the relevant changes done in minor version updates.
 
+1.0.1 (????-??-??)
+------------------
+
+All issues and pull requests under this release may be found under the
+[1.0.1](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.0.1)
+milestone.
+
+ * [#85](https://github.com/alcaeus/mongo-php-adapter/pull/85) fixes calls to
+ `MongoCollection::count` using the legacy syntax of providing `skip` and `limit`
+ arguments instead of an `options` array.
+
+
 1.0.0 (2016-03-18)
 ------------------
 

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