Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests when running against newer servers/drivers #260

Merged
merged 8 commits into from
Nov 5, 2019
6 changes: 6 additions & 0 deletions lib/Alcaeus/MongoDbAdapter/ExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 4 additions & 1 deletion lib/Mongo/MongoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Alcaeus\MongoDbAdapter\Helper;
use Alcaeus\MongoDbAdapter\TypeConverter;
use Alcaeus\MongoDbAdapter\ExceptionConverter;
use MongoDB\Driver\Exception\CommandException;

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

$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 = [
Expand Down
18 changes: 13 additions & 5 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Alcaeus\MongoDbAdapter\Tests\TestCase;
use MongoId;
use PHPUnit\Framework\Error\Warning;
use function extension_loaded;

/**
* @author alcaeus <alcaeus@alcaeus.org>
Expand Down Expand Up @@ -844,6 +845,8 @@ public function testDistinctWithIdQuery()

public function testAggregate()
{
$this->skipTestIf(extension_loaded('mongo'));

$collection = $this->getCollection();

$this->prepareData();
Expand All @@ -860,7 +863,7 @@ public function testAggregate()
]
];

$result = $collection->aggregate($pipeline);
$result = $collection->aggregate($pipeline, ['cursor' => true]);
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('result', $result);

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

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();
Expand Down Expand Up @@ -906,6 +911,8 @@ public function testAggregateWithMultiplePilelineOperatorsAsArguments()

public function testAggregateInvalidPipeline()
{
$this->skipTestIf(extension_loaded('mongo'));

$collection = $this->getCollection();

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

$this->expectException(\MongoResultException::class);
$this->expectExceptionMessage('Unrecognized pipeline stage name');
$collection->aggregate($pipeline);
$collection->aggregate($pipeline, ['cursor' => true]);
}

public function testAggregateTimeoutException()
Expand All @@ -939,7 +946,7 @@ public function testAggregateTimeoutException()
]
];

$collection->aggregate($pipeline, ['maxTimeMS' => 1]);
$collection->aggregate($pipeline, ['maxTimeMS' => 1, 'cursor' => true]);
}

public function testAggregateCursor()
Expand Down Expand Up @@ -1723,6 +1730,8 @@ public function testFindAndModifyWithFields()

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];
Expand Down Expand Up @@ -1887,7 +1896,6 @@ public function testValidate()
'ns' => 'mongo-php-adapter.test',
'nrecords' => 1,
'nIndexes' => 1,
'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
'valid' => true,
'errors' => [],
],
Expand All @@ -1904,7 +1912,7 @@ public function testDrop()
'nIndexesWas' => 1,
'ok' => 1.0
];
$this->assertSame($expected, $this->getCollection()->drop());
$this->assertEquals($expected, $this->getCollection()->drop());
}

public function testEmptyCollectionName()
Expand Down
2 changes: 1 addition & 1 deletion tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public function testGetCollectionInfo()
],
];
}
$this->assertEquals($expected, $collectionInfo);
$this->assertArraySubset($expected, $collectionInfo);
return;
}
}
Expand Down
17 changes: 11 additions & 6 deletions tests/Alcaeus/MongoDbAdapter/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,19 @@ protected function failMaxTimeMS()
/**
* @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');
}
}

Expand All @@ -183,7 +184,11 @@ protected function getServerVersion()
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'];
}

/**
Expand All @@ -199,6 +204,6 @@ protected function getDefaultIndexVersion()

// 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;
}
}