Forráskód Böngészése

Fix inserts with numeric keys

Andreas Braun 10 éve
szülő
commit
95ca30b44e

+ 2 - 2
lib/Mongo/MongoCollection.php

@@ -923,8 +923,8 @@ class MongoCollection
     {
         $checkKeys = function($array) {
             foreach (array_keys($array) as $key) {
-                if (empty($key) || strpos($key, '*') === 1) {
-                    throw new \MongoException('document contain invalid key');
+                if (empty($key) && $key !== 0) {
+                    throw new \MongoException('zero-length keys are not allowed, did you use $ with double quotes?');
                 }
             }
         };

+ 56 - 0
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -81,6 +81,30 @@ class MongoCollectionTest extends TestCase
         $this->getCollection()->insert($document);
     }
 
+    public function testInsertWithInvalidKey()
+    {
+        $document = ['*' => 'foo'];
+        $this->getCollection()->insert($document);
+
+        $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
+    }
+
+    public function testInsertWithEmptyKey()
+    {
+        $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
+
+        $document = ['' => 'foo'];
+        $this->getCollection()->insert($document);
+    }
+
+    public function testInsertWithNumericKey()
+    {
+        $document = ['foo'];
+        $this->getCollection()->insert($document);
+
+        $this->assertSame(1, $this->getCollection()->count(['foo']));
+    }
+
     public function testInsertDuplicate()
     {
         $collection = $this->getCollection();
@@ -183,6 +207,38 @@ class MongoCollectionTest extends TestCase
         $this->getCollection()->batchInsert($documents);
     }
 
+    public function testBatchInsertObjectWithPrivateProperties()
+    {
+        $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
+
+        $documents = [new PrivatePropertiesStub()];
+        $this->getCollection()->batchInsert($documents);
+    }
+
+    public function testBatchInsertWithInvalidKey()
+    {
+        $documents = [['*' => 'foo']];
+        $this->getCollection()->batchInsert($documents);
+
+        $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
+    }
+
+    public function testBatchInsertWithEmptyKey()
+    {
+        $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
+
+        $documents = [['' => 'foo']];
+        $this->getCollection()->batchInsert($documents);
+    }
+
+    public function testBatchInsertWithNumericKey()
+    {
+        $documents = [['foo']];
+        $this->getCollection()->batchInsert($documents);
+
+        $this->assertSame(1, $this->getCollection()->count(['foo']));
+    }
+
     public function testBatchInsertEmptyBatchException()
     {
         $this->setExpectedException('MongoException', 'No write ops were included in the batch');