Bladeren bron

add unit tests to capture explain() problems, and refactor handling of empty projection array based on CR comment

Rob Williams 8 jaren geleden
bovenliggende
commit
eb00c842e0

+ 1 - 1
lib/Alcaeus/MongoDbAdapter/AbstractCursor.php

@@ -276,7 +276,7 @@ abstract class AbstractCursor
             $converter = 'convert' . ucfirst($option);
             $value = method_exists($this, $converter) ? $this->$converter() : $this->$option;
 
-            if ($value === null || ($option === 'projection' && $value === [])) {
+            if ($value === null) {
                 continue;
             }
 

+ 2 - 2
lib/Alcaeus/MongoDbAdapter/TypeConverter.php

@@ -97,14 +97,14 @@ class TypeConverter
      * this was never documented, the legacy driver applied the same conversion.
      *
      * @param array $fields
-     * @return array
+     * @return array|null
      *
      * @throws \MongoException
      */
     public static function convertProjection($fields)
     {
         if (! is_array($fields) || $fields === []) {
-            return [];
+            return null;
         }
 
         if (! TypeConverter::isNumericArray($fields)) {

+ 68 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php

@@ -432,6 +432,74 @@ class MongoCursorTest extends TestCase
         $this->assertArraySubset($expected, $cursor->explain());
     }
 
+    public function testExplainWithEmptyProjection()
+    {
+        $this->prepareData();
+
+        $collection = $this->getCollection();
+        $cursor = $collection->find(['foo' => 'bar']);
+
+        $expected = [
+            'queryPlanner' => [
+                'plannerVersion' => 1,
+                'namespace' => 'mongo-php-adapter.test',
+                'indexFilterSet' => false,
+                'parsedQuery' => [
+                    'foo' => ['$eq' => 'bar']
+                ],
+                'winningPlan' => [],
+                'rejectedPlans' => [],
+            ],
+            'executionStats' => [
+                'executionSuccess' => true,
+                'nReturned' => 2,
+                'totalKeysExamined' => 0,
+                'totalDocsExamined' => 3,
+                'executionStages' => [],
+                'allPlansExecution' => [],
+            ],
+            'serverInfo' => [
+                'port' => 27017,
+            ],
+        ];
+
+        $this->assertArraySubset($expected, $cursor->explain());
+    }
+
+    public function testExplainConvertsQuery()
+    {
+        $this->prepareData();
+
+        $collection = $this->getCollection();
+        $cursor = $collection->find(['foo' => new \MongoRegex('/^b/')]);
+
+        $expected = [
+            'queryPlanner' => [
+                'plannerVersion' => 1,
+                'namespace' => 'mongo-php-adapter.test',
+                'indexFilterSet' => false,
+                'parsedQuery' => [
+                    'foo' => new \MongoRegex('/^b/')
+                ],
+                'winningPlan' => [],
+                'rejectedPlans' => [],
+            ],
+            'executionStats' => [
+                'executionSuccess' => true,
+                'nReturned' => 2,
+                'totalKeysExamined' => 0,
+                'totalDocsExamined' => 3,
+                'executionStages' => [],
+                'allPlansExecution' => [],
+            ],
+            'serverInfo' => [
+                'port' => 27017,
+            ],
+        ];
+
+        $this->assertArraySubset($expected, $cursor->explain());
+    }
+
 
     /**
      * @return \PHPUnit_Framework_MockObject_MockObject