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

Merge pull request #260 from alcaeus/fix-tests

Fix tests when running against newer servers/drivers
Andreas Braun 6 лет назад
Родитель
Сommit
80abc69000

+ 6 - 0
lib/Alcaeus/MongoDbAdapter/ExceptionConverter.php

@@ -32,6 +32,12 @@ class ExceptionConverter
      */
     public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
     {
+        // Starting with ext-mongodb 1.6.0, errors during bulk write are always wrapped in a BulkWriteException.
+        // If a BulkWriteException wraps another driver exception, use that instead.
+        if ($e instanceof Exception\BulkWriteException && $e->getPrevious() instanceof Exception\Exception) {
+            $e = $e->getPrevious();
+        }
+
         $message = $e->getMessage();
         $code = $e->getCode();
 

+ 4 - 1
lib/Mongo/MongoCollection.php

@@ -20,6 +20,7 @@ if (class_exists('MongoCollection', false)) {
 use Alcaeus\MongoDbAdapter\Helper;
 use Alcaeus\MongoDbAdapter\TypeConverter;
 use Alcaeus\MongoDbAdapter\ExceptionConverter;
+use MongoDB\Driver\Exception\CommandException;
 
 /**
  * Represents a database collection.
@@ -626,7 +627,9 @@ class MongoCollection
 
             $this->collection->createIndex($keys, $options);
         } catch (\MongoDB\Driver\Exception\Exception $e) {
-            throw ExceptionConverter::toLegacy($e, 'MongoResultException');
+            if (! $e instanceof CommandException || strpos($e->getMessage(), 'with a different name') === false) {
+                throw ExceptionConverter::toLegacy($e, 'MongoResultException');
+            }
         }
 
         $result = [

+ 13 - 5
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -8,6 +8,7 @@ use MongoDB\Driver\ReadPreference;
 use Alcaeus\MongoDbAdapter\Tests\TestCase;
 use MongoId;
 use PHPUnit\Framework\Error\Warning;
+use function extension_loaded;
 
 /**
  * @author alcaeus <alcaeus@alcaeus.org>
@@ -844,6 +845,8 @@ class MongoCollectionTest extends TestCase
 
     public function testAggregate()
     {
+        $this->skipTestIf(extension_loaded('mongo'));
+
         $collection = $this->getCollection();
 
         $this->prepareData();
@@ -860,7 +863,7 @@ class MongoCollectionTest extends TestCase
             ]
         ];
 
-        $result = $collection->aggregate($pipeline);
+        $result = $collection->aggregate($pipeline, ['cursor' => true]);
         $this->assertInternalType('array', $result);
         $this->assertArrayHasKey('result', $result);
 
@@ -872,6 +875,8 @@ class MongoCollectionTest extends TestCase
 
     public function testAggregateWithMultiplePilelineOperatorsAsArguments()
     {
+        $this->skipTestIf(version_compare($this->getServerVersion(), '3.6.0', '>='), 'Test does not apply to MongoDB >= 3.6.');
+
         $collection = $this->getCollection();
 
         $this->prepareData();
@@ -906,6 +911,8 @@ class MongoCollectionTest extends TestCase
 
     public function testAggregateInvalidPipeline()
     {
+        $this->skipTestIf(extension_loaded('mongo'));
+
         $collection = $this->getCollection();
 
         $pipeline = [
@@ -916,7 +923,7 @@ class MongoCollectionTest extends TestCase
 
         $this->expectException(\MongoResultException::class);
         $this->expectExceptionMessage('Unrecognized pipeline stage name');
-        $collection->aggregate($pipeline);
+        $collection->aggregate($pipeline, ['cursor' => true]);
     }
 
     public function testAggregateTimeoutException()
@@ -939,7 +946,7 @@ class MongoCollectionTest extends TestCase
             ]
         ];
 
-        $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
+        $collection->aggregate($pipeline, ['maxTimeMS' => 1, 'cursor' => true]);
     }
 
     public function testAggregateCursor()
@@ -1723,6 +1730,8 @@ class MongoCollectionTest extends TestCase
 
     public function testGroup()
     {
+        $this->skipTestIf(version_compare($this->getServerVersion(), '4.2.0', '>='), 'Test does not apply to MongoDB >= 4.2.');
+
         $collection = $this->getCollection();
 
         $document1 = ['a' => 2];
@@ -1887,7 +1896,6 @@ class MongoCollectionTest extends TestCase
                 'ns' => 'mongo-php-adapter.test',
                 'nrecords' => 1,
                 'nIndexes' => 1,
-                'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
                 'valid' => true,
                 'errors' => [],
             ],
@@ -1904,7 +1912,7 @@ class MongoCollectionTest extends TestCase
             'nIndexesWas' => 1,
             'ok' => 1.0
         ];
-        $this->assertSame($expected, $this->getCollection()->drop());
+        $this->assertEquals($expected, $this->getCollection()->drop());
     }
 
     public function testEmptyCollectionName()

+ 1 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php

@@ -290,7 +290,7 @@ class MongoDBTest extends TestCase
                         ],
                     ];
                 }
-                $this->assertEquals($expected, $collectionInfo);
+                $this->assertArraySubset($expected, $collectionInfo);
                 return;
             }
         }

+ 11 - 6
tests/Alcaeus/MongoDbAdapter/TestCase.php

@@ -153,18 +153,19 @@ abstract class TestCase extends BaseTestCase
     /**
      * @param bool $condition
      */
-    protected function skipTestUnless($condition)
+    protected function skipTestUnless($condition, $message = null)
     {
-        $this->skipTestIf(! $condition);
+        $this->skipTestIf(! $condition, $message);
     }
 
     /**
      * @param bool $condition
+     * @param string|null $message
      */
-    protected function skipTestIf($condition)
+    protected function skipTestIf($condition, $message = null)
     {
         if ($condition) {
-            $this->markTestSkipped('Test only applies when running against mongo-php-adapter');
+            $this->markTestSkipped($message !== null ? $message : 'Test only applies when running against mongo-php-adapter');
         }
     }
 
@@ -183,7 +184,11 @@ abstract class TestCase extends BaseTestCase
     protected function getFeatureCompatibilityVersion()
     {
         $featureCompatibilityVersion = $this->getClient()->selectDB('admin')->command(['getParameter' => true, 'featureCompatibilityVersion' => true]);
-        return isset($featureCompatibilityVersion['featureCompatibilityVersion']) ? $featureCompatibilityVersion['featureCompatibilityVersion'] : '3.2';
+        if (! isset($featureCompatibilityVersion['featureCompatibilityVersion'])) {
+            return '3.2';
+        }
+
+        return isset($featureCompatibilityVersion['featureCompatibilityVersion']['version']) ? $featureCompatibilityVersion['featureCompatibilityVersion']['version'] : $featureCompatibilityVersion['featureCompatibilityVersion'];
     }
 
     /**
@@ -199,6 +204,6 @@ abstract class TestCase extends BaseTestCase
 
         // Check featureCompatibilityFlag
         $compatibilityVersion = $this->getFeatureCompatibilityVersion();
-        return $compatibilityVersion === '3.4' ? self::INDEX_VERSION_2 : self::INDEX_VERSION_1;
+        return version_compare($compatibilityVersion, '3.4', '>=') ? self::INDEX_VERSION_2 : self::INDEX_VERSION_1;
     }
 }