Parcourir la source

Merge branch 'fix-aggregate-parameters' into 1.0.x

* fix-aggregate-parameters:
  Check result of aggregation call
  Add changelog entry for fix
  fix and test for MongoCollection::aggregate

Fixes #114.
Andreas Braun il y a 9 ans
Parent
commit
8806ab032f

+ 11 - 0
CHANGELOG-1.0.md

@@ -3,6 +3,17 @@ CHANGELOG
 
 This changelog references the relevant changes done in minor version updates.
 
+1.0.4 (xxxx-xx-xx)
+------------------
+
+All issues and pull requests under this release may be found under the
+[1.0.4](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.0.4)
+milestone.
+
+ * [#115](https://github.com/alcaeus/mongo-php-adapter/pull/115) fixes an error
+ where using the alternate syntax for `MongoCollection::aggregate` would lead to
+ empty aggregation pipelines
+
 1.0.3 (2016-04-13)
 ------------------
 

+ 2 - 1
lib/Mongo/MongoCollection.php

@@ -131,11 +131,12 @@ class MongoCollection
     public function aggregate(array $pipeline, array $op = [])
     {
         if (! TypeConverter::isNumericArray($pipeline)) {
+            $operators = func_get_args();
             $pipeline = [];
             $options = [];
 
             $i = 0;
-            foreach (func_get_args() as $operator) {
+            foreach ($operators as $operator) {
                 $i++;
                 if (! is_array($operator)) {
                     trigger_error("Argument $i is not an array", E_WARNING);

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

@@ -600,6 +600,40 @@ class MongoCollectionTest extends TestCase
         ], $result['result']);
     }
 
+    public function testAggregateWithMultiplePilelineOperatorsAsArguments()
+    {
+        $collection = $this->getCollection();
+
+        $this->prepareData();
+
+        try {
+            $result = $collection->aggregate(
+                [
+                    '$group' => [
+                        '_id' => '$foo',
+                        'count' => [ '$sum' => 1 ],
+                    ],
+                ],
+                [
+                    '$sort' => ['_id' => 1]
+                ]
+            );
+        } catch (\MongoResultException $ex) {
+            $msg = 'MongoCollection::aggregate ( array $op [, array $op [, array $... ]] ) should accept variable amount of pipeline operators as argument'
+                . "\n"
+                . $ex;
+            $this->fail($msg);
+        }
+
+        $this->assertInternalType('array', $result);
+        $this->assertArrayHasKey('result', $result);
+
+        $this->assertEquals([
+            ['_id' => 'bar', 'count' => 2],
+            ['_id' => 'foo', 'count' => 1],
+        ], $result['result']);
+    }
+
     public function testAggregateInvalidPipeline()
     {
         $collection = $this->getCollection();