Skip to content

Commit 80abc69

Browse files
authored
Merge pull request #260 from alcaeus/fix-tests
Fix tests when running against newer servers/drivers
2 parents 93b81eb + a14a8aa commit 80abc69

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

lib/Alcaeus/MongoDbAdapter/ExceptionConverter.php

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ class ExceptionConverter
3232
*/
3333
public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
3434
{
35+
// Starting with ext-mongodb 1.6.0, errors during bulk write are always wrapped in a BulkWriteException.
36+
// If a BulkWriteException wraps another driver exception, use that instead.
37+
if ($e instanceof Exception\BulkWriteException && $e->getPrevious() instanceof Exception\Exception) {
38+
$e = $e->getPrevious();
39+
}
40+
3541
$message = $e->getMessage();
3642
$code = $e->getCode();
3743

lib/Mongo/MongoCollection.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Alcaeus\MongoDbAdapter\Helper;
2121
use Alcaeus\MongoDbAdapter\TypeConverter;
2222
use Alcaeus\MongoDbAdapter\ExceptionConverter;
23+
use MongoDB\Driver\Exception\CommandException;
2324

2425
/**
2526
* Represents a database collection.
@@ -626,7 +627,9 @@ public function createIndex($keys, array $options = [])
626627

627628
$this->collection->createIndex($keys, $options);
628629
} catch (\MongoDB\Driver\Exception\Exception $e) {
629-
throw ExceptionConverter::toLegacy($e, 'MongoResultException');
630+
if (! $e instanceof CommandException || strpos($e->getMessage(), 'with a different name') === false) {
631+
throw ExceptionConverter::toLegacy($e, 'MongoResultException');
632+
}
630633
}
631634

632635
$result = [

tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Alcaeus\MongoDbAdapter\Tests\TestCase;
99
use MongoId;
1010
use PHPUnit\Framework\Error\Warning;
11+
use function extension_loaded;
1112

1213
/**
1314
* @author alcaeus <alcaeus@alcaeus.org>
@@ -844,6 +845,8 @@ public function testDistinctWithIdQuery()
844845

845846
public function testAggregate()
846847
{
848+
$this->skipTestIf(extension_loaded('mongo'));
849+
847850
$collection = $this->getCollection();
848851

849852
$this->prepareData();
@@ -860,7 +863,7 @@ public function testAggregate()
860863
]
861864
];
862865

863-
$result = $collection->aggregate($pipeline);
866+
$result = $collection->aggregate($pipeline, ['cursor' => true]);
864867
$this->assertInternalType('array', $result);
865868
$this->assertArrayHasKey('result', $result);
866869

@@ -872,6 +875,8 @@ public function testAggregate()
872875

873876
public function testAggregateWithMultiplePilelineOperatorsAsArguments()
874877
{
878+
$this->skipTestIf(version_compare($this->getServerVersion(), '3.6.0', '>='), 'Test does not apply to MongoDB >= 3.6.');
879+
875880
$collection = $this->getCollection();
876881

877882
$this->prepareData();
@@ -906,6 +911,8 @@ public function testAggregateWithMultiplePilelineOperatorsAsArguments()
906911

907912
public function testAggregateInvalidPipeline()
908913
{
914+
$this->skipTestIf(extension_loaded('mongo'));
915+
909916
$collection = $this->getCollection();
910917

911918
$pipeline = [
@@ -916,7 +923,7 @@ public function testAggregateInvalidPipeline()
916923

917924
$this->expectException(\MongoResultException::class);
918925
$this->expectExceptionMessage('Unrecognized pipeline stage name');
919-
$collection->aggregate($pipeline);
926+
$collection->aggregate($pipeline, ['cursor' => true]);
920927
}
921928

922929
public function testAggregateTimeoutException()
@@ -939,7 +946,7 @@ public function testAggregateTimeoutException()
939946
]
940947
];
941948

942-
$collection->aggregate($pipeline, ['maxTimeMS' => 1]);
949+
$collection->aggregate($pipeline, ['maxTimeMS' => 1, 'cursor' => true]);
943950
}
944951

945952
public function testAggregateCursor()
@@ -1723,6 +1730,8 @@ public function testFindAndModifyWithFields()
17231730

17241731
public function testGroup()
17251732
{
1733+
$this->skipTestIf(version_compare($this->getServerVersion(), '4.2.0', '>='), 'Test does not apply to MongoDB >= 4.2.');
1734+
17261735
$collection = $this->getCollection();
17271736

17281737
$document1 = ['a' => 2];
@@ -1887,7 +1896,6 @@ public function testValidate()
18871896
'ns' => 'mongo-php-adapter.test',
18881897
'nrecords' => 1,
18891898
'nIndexes' => 1,
1890-
'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
18911899
'valid' => true,
18921900
'errors' => [],
18931901
],
@@ -1904,7 +1912,7 @@ public function testDrop()
19041912
'nIndexesWas' => 1,
19051913
'ok' => 1.0
19061914
];
1907-
$this->assertSame($expected, $this->getCollection()->drop());
1915+
$this->assertEquals($expected, $this->getCollection()->drop());
19081916
}
19091917

19101918
public function testEmptyCollectionName()

tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public function testGetCollectionInfo()
290290
],
291291
];
292292
}
293-
$this->assertEquals($expected, $collectionInfo);
293+
$this->assertArraySubset($expected, $collectionInfo);
294294
return;
295295
}
296296
}

tests/Alcaeus/MongoDbAdapter/TestCase.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,19 @@ protected function failMaxTimeMS()
153153
/**
154154
* @param bool $condition
155155
*/
156-
protected function skipTestUnless($condition)
156+
protected function skipTestUnless($condition, $message = null)
157157
{
158-
$this->skipTestIf(! $condition);
158+
$this->skipTestIf(! $condition, $message);
159159
}
160160

161161
/**
162162
* @param bool $condition
163+
* @param string|null $message
163164
*/
164-
protected function skipTestIf($condition)
165+
protected function skipTestIf($condition, $message = null)
165166
{
166167
if ($condition) {
167-
$this->markTestSkipped('Test only applies when running against mongo-php-adapter');
168+
$this->markTestSkipped($message !== null ? $message : 'Test only applies when running against mongo-php-adapter');
168169
}
169170
}
170171

@@ -183,7 +184,11 @@ protected function getServerVersion()
183184
protected function getFeatureCompatibilityVersion()
184185
{
185186
$featureCompatibilityVersion = $this->getClient()->selectDB('admin')->command(['getParameter' => true, 'featureCompatibilityVersion' => true]);
186-
return isset($featureCompatibilityVersion['featureCompatibilityVersion']) ? $featureCompatibilityVersion['featureCompatibilityVersion'] : '3.2';
187+
if (! isset($featureCompatibilityVersion['featureCompatibilityVersion'])) {
188+
return '3.2';
189+
}
190+
191+
return isset($featureCompatibilityVersion['featureCompatibilityVersion']['version']) ? $featureCompatibilityVersion['featureCompatibilityVersion']['version'] : $featureCompatibilityVersion['featureCompatibilityVersion'];
187192
}
188193

189194
/**
@@ -199,6 +204,6 @@ protected function getDefaultIndexVersion()
199204

200205
// Check featureCompatibilityFlag
201206
$compatibilityVersion = $this->getFeatureCompatibilityVersion();
202-
return $compatibilityVersion === '3.4' ? self::INDEX_VERSION_2 : self::INDEX_VERSION_1;
207+
return version_compare($compatibilityVersion, '3.4', '>=') ? self::INDEX_VERSION_2 : self::INDEX_VERSION_1;
203208
}
204209
}

0 commit comments

Comments
 (0)