ソースを参照

Merge pull request #155 from alcaeus/fix-type-conversion-bson-types

Fix handling of BSON types in TypeConverter
Andreas 9 年 前
コミット
6992c27cf5

+ 2 - 0
CHANGELOG-1.0.md

@@ -10,6 +10,8 @@ All issues and pull requests under this release may be found under the
 [1.0.9](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.0.9)
 milestone.
 
+ * [#155](https://github.com/alcaeus/mongo-php-adapter/pull/155) fixes the handling
+ of BSON types when converting legacy types to BSON types.
  * [#154](https://github.com/alcaeus/mongo-php-adapter/pull/154) makes the `options`
  parameter in `MongoDB::createCollection` optional.
 

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

@@ -41,6 +41,8 @@ class TypeConverter
         switch (true) {
             case $value instanceof TypeInterface:
                 return $value->toBSONType();
+            case $value instanceof BSON\Type:
+                return $value;
             case is_array($value):
             case is_object($value);
                 $result = [];

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

@@ -2,6 +2,7 @@
 
 namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
 
+use MongoDB\BSON\Regex;
 use MongoDB\Driver\ReadPreference;
 use Alcaeus\MongoDbAdapter\Tests\TestCase;
 
@@ -1784,6 +1785,27 @@ class MongoCollectionTest extends TestCase
         $this->assertCount(1, $items);
         $this->assertCount(1, $items[0]['loveItems']);
     }
+
+    public static function dataFindWithRegex()
+    {
+        return [
+            'MongoRegex' => [new \MongoRegex('/^foo.*/i')],
+            'BSONRegex' => [new Regex('^foo.*', 'i')],
+        ];
+    }
+
+    /**
+     * @dataProvider dataFindWithRegex
+     */
+    public function testFindWithRegex($regex)
+    {
+        $this->skipTestIf(extension_loaded('mongo'));
+        $document = ['name' => 'FOO 123'];
+        $this->getCollection()->insert($document);
+
+        $cursor = $this->getCollection()->find(['name' => $regex]);
+        $this->assertSame(1, $cursor->count());
+    }
 }
 
 class PrivatePropertiesStub