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

Fix aggregation when not using cursor

Andreas Braun 9 лет назад
Родитель
Сommit
8537e34206

+ 1 - 1
composer.json

@@ -11,7 +11,7 @@
     "require": {
         "php": "^5.5 || ^7.0",
         "ext-hash": "*",
-        "mongodb/mongodb": "^1.0.0"
+        "mongodb/mongodb": "^1.0.1"
     },
     "require-dev": {
         "phpunit/phpunit": "^4.8 || ^5.0"

+ 8 - 2
lib/Mongo/MongoCollection.php

@@ -161,9 +161,15 @@ class MongoCollection
         }
 
         try {
-            return $this->collection->aggregate(TypeConverter::fromLegacy($pipeline), $options);
+            $cursor = $this->collection->aggregate(TypeConverter::fromLegacy($pipeline), $options);
+
+            return [
+                'ok' => 1.0,
+                'result' => TypeConverter::toLegacy($cursor),
+                'waitedMS' => 0,
+            ];
         } catch (\MongoDB\Driver\Exception\Exception $e) {
-            throw ExceptionConverter::toLegacy($e);
+            throw ExceptionConverter::toLegacy($e,'MongoResultException');
         }
     }
 

+ 14 - 2
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -494,7 +494,6 @@ class MongoCollectionTest extends TestCase
 
     public function testAggregate()
     {
-        $this->skipTestUnless(extension_loaded('mongo'));
         $collection = $this->getCollection();
 
         $this->prepareData();
@@ -521,9 +520,22 @@ class MongoCollectionTest extends TestCase
         ], $result['result']);
     }
 
+    public function testAggregateInvalidPipeline()
+    {
+        $collection = $this->getCollection();
+
+        $pipeline = [
+            [
+                '$invalid' => []
+            ],
+        ];
+
+        $this->setExpectedException('MongoResultException', 'Unrecognized pipeline stage name');
+        $collection->aggregate($pipeline);
+    }
+
     public function testAggregateTimeoutException()
     {
-        $this->skipTestUnless(extension_loaded('mongo'));
         $collection = $this->getCollection();
 
         $this->failMaxTimeMS();