|
|
@@ -5,6 +5,7 @@ namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
|
|
|
use MongoDB\BSON\Regex;
|
|
|
use MongoDB\Driver\ReadPreference;
|
|
|
use Alcaeus\MongoDbAdapter\Tests\TestCase;
|
|
|
+use PHPUnit\Framework\Error\Warning;
|
|
|
|
|
|
/**
|
|
|
* @author alcaeus <alcaeus@alcaeus.org>
|
|
|
@@ -51,7 +52,11 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testInsertInvalidData()
|
|
|
{
|
|
|
- $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
|
|
|
+ // Dirty hack to support both PHPUnit 5.x and 6.x
|
|
|
+ $className = class_exists(Warning::class) ? Warning::class : \PHPUnit_Framework_Error_Warning::class;
|
|
|
+ $this->expectException($className);
|
|
|
+
|
|
|
+ $this->expectExceptionMessage('MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
|
|
|
|
|
|
$document = 8;
|
|
|
$this->getCollection()->insert($document);
|
|
|
@@ -83,7 +88,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testInsertObjectWithPrivateProperties()
|
|
|
{
|
|
|
- $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
|
|
|
+ $this->expectException(\MongoException::class);
|
|
|
+ $this->expectExceptionMessage('zero-length keys are not allowed, did you use $ with double quotes?');
|
|
|
|
|
|
$document = new PrivatePropertiesStub();
|
|
|
$this->getCollection()->insert($document);
|
|
|
@@ -99,7 +105,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testInsertWithEmptyKey()
|
|
|
{
|
|
|
- $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
|
|
|
+ $this->expectException(\MongoException::class);
|
|
|
+ $this->expectExceptionMessage('zero-length keys are not allowed, did you use $ with double quotes?');
|
|
|
|
|
|
$document = ['' => 'foo'];
|
|
|
$this->getCollection()->insert($document);
|
|
|
@@ -123,7 +130,9 @@ class MongoCollectionTest extends TestCase
|
|
|
$collection->insert($document);
|
|
|
|
|
|
unset($document['_id']);
|
|
|
- $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter\.test/');
|
|
|
+
|
|
|
+ $this->expectException(\MongoDuplicateKeyException::class);
|
|
|
+ $this->expectExceptionMessageRegExp('/E11000 duplicate key error .* mongo-php-adapter\.test/');
|
|
|
$collection->insert($document);
|
|
|
}
|
|
|
|
|
|
@@ -155,10 +164,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testInsertWriteConcernException()
|
|
|
{
|
|
|
- $this->setExpectedException(
|
|
|
- 'MongoWriteConcernException',
|
|
|
- "cannot use 'w' > 1 when a host is not replicated"
|
|
|
- );
|
|
|
+ $this->expectException(\MongoWriteConcernException::class);
|
|
|
+ $this->expectExceptionMessage("cannot use 'w' > 1 when a host is not replicated");
|
|
|
|
|
|
$document = ['foo' => 'bar'];
|
|
|
$this->getCollection()->insert($document, ['w' => 2]);
|
|
|
@@ -228,7 +235,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testBatchInsertException()
|
|
|
{
|
|
|
- $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
|
|
|
+ $this->expectException(\MongoDuplicateKeyException::class);
|
|
|
+ $this->expectExceptionMessageRegExp('/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
|
|
|
|
|
|
$id = new \MongoId();
|
|
|
$documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
|
|
|
@@ -237,7 +245,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testBatchInsertObjectWithPrivateProperties()
|
|
|
{
|
|
|
- $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
|
|
|
+ $this->expectException(\MongoException::class);
|
|
|
+ $this->expectExceptionMessage('zero-length keys are not allowed, did you use $ with double quotes?');
|
|
|
|
|
|
$documents = [new PrivatePropertiesStub()];
|
|
|
$this->getCollection()->batchInsert($documents);
|
|
|
@@ -253,7 +262,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testBatchInsertWithEmptyKey()
|
|
|
{
|
|
|
- $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
|
|
|
+ $this->expectException(\MongoException::class);
|
|
|
+ $this->expectExceptionMessage('zero-length keys are not allowed, did you use $ with double quotes?');
|
|
|
|
|
|
$documents = [['' => 'foo']];
|
|
|
$this->getCollection()->batchInsert($documents);
|
|
|
@@ -269,7 +279,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testBatchInsertEmptyBatchException()
|
|
|
{
|
|
|
- $this->setExpectedException('MongoException', 'No write ops were included in the batch');
|
|
|
+ $this->expectException(\MongoException::class);
|
|
|
+ $this->expectExceptionMessage('No write ops were included in the batch');
|
|
|
|
|
|
$documents = [];
|
|
|
$this->getCollection()->batchInsert($documents, ['w' => 2]);
|
|
|
@@ -277,7 +288,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testUpdateWriteConcern()
|
|
|
{
|
|
|
- $this->setExpectedException('MongoWriteConcernException', "cannot use 'w' > 1 when a host is not replicated");
|
|
|
+ $this->expectException(\MongoWriteConcernException::class);
|
|
|
+ $this->expectExceptionMessage("cannot use 'w' > 1 when a host is not replicated");
|
|
|
|
|
|
$this->getCollection()->update([], ['$set' => ['foo' => 'bar']], ['w' => 2]);
|
|
|
}
|
|
|
@@ -333,7 +345,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testUpdateReplaceMultiple()
|
|
|
{
|
|
|
- $this->setExpectedExceptionRegExp('MongoWriteConcernException', '/multi update only works with \$ operators/', 9);
|
|
|
+ $this->expectException(\MongoWriteConcernException::class);
|
|
|
+ $this->expectExceptionMessageRegExp('/multi update only works with \$ operators/', 9);
|
|
|
$this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
|
|
|
}
|
|
|
|
|
|
@@ -347,7 +360,7 @@ class MongoCollectionTest extends TestCase
|
|
|
$document = ['foo' => 'foo'];
|
|
|
$collection->insert($document);
|
|
|
|
|
|
- $this->setExpectedException('MongoDuplicateKeyException');
|
|
|
+ $this->expectException(\MongoDuplicateKeyException::class);
|
|
|
$collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
|
|
|
}
|
|
|
|
|
|
@@ -507,7 +520,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testFindWithProjectionAndSequentialNumericKeys()
|
|
|
{
|
|
|
- $this->setExpectedException(\MongoException::class, 'field names must be strings', 8);
|
|
|
+ $this->expectException(\MongoException::class);
|
|
|
+ $this->expectExceptionMessage('field names must be strings', 8);
|
|
|
$this->getCollection()->findOne([], [true, false]);
|
|
|
}
|
|
|
|
|
|
@@ -605,7 +619,7 @@ class MongoCollectionTest extends TestCase
|
|
|
{
|
|
|
$this->failMaxTimeMS();
|
|
|
|
|
|
- $this->setExpectedException('MongoExecutionTimeoutException');
|
|
|
+ $this->expectException(\MongoExecutionTimeoutException::class);
|
|
|
|
|
|
$this->getCollection()->count([], ['maxTimeMS' => 1]);
|
|
|
}
|
|
|
@@ -646,7 +660,7 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testFindOneConnectionIssue()
|
|
|
{
|
|
|
- $this->setExpectedException('MongoConnectionException');
|
|
|
+ $this->expectException(\MongoConnectionException::class);
|
|
|
|
|
|
$client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
|
|
|
$collection = $client->selectCollection('mongo-php-adapter', 'test');
|
|
|
@@ -772,7 +786,8 @@ class MongoCollectionTest extends TestCase
|
|
|
],
|
|
|
];
|
|
|
|
|
|
- $this->setExpectedException('MongoResultException', 'Unrecognized pipeline stage name');
|
|
|
+ $this->expectException(\MongoResultException::class);
|
|
|
+ $this->expectExceptionMessage('Unrecognized pipeline stage name');
|
|
|
$collection->aggregate($pipeline);
|
|
|
}
|
|
|
|
|
|
@@ -782,7 +797,7 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
$this->failMaxTimeMS();
|
|
|
|
|
|
- $this->setExpectedException('MongoExecutionTimeoutException');
|
|
|
+ $this->expectException(\MongoExecutionTimeoutException::class);
|
|
|
|
|
|
$pipeline = [
|
|
|
[
|
|
|
@@ -998,7 +1013,7 @@ class MongoCollectionTest extends TestCase
|
|
|
$document = ['foo' => 'bar'];
|
|
|
$collection->save($document);
|
|
|
|
|
|
- $this->setExpectedException('MongoDuplicateKeyException');
|
|
|
+ $this->expectException(\MongoDuplicateKeyException::class);
|
|
|
|
|
|
unset($document['_id']);
|
|
|
$collection->save($document);
|
|
|
@@ -1070,7 +1085,8 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testCreateIndexInvalid()
|
|
|
{
|
|
|
- $this->setExpectedException('MongoException', 'index specification has no elements');
|
|
|
+ $this->expectException(\MongoException::class);
|
|
|
+ $this->expectExceptionMessage('index specification has no elements');
|
|
|
|
|
|
$this->getCollection()->createIndex([]);
|
|
|
}
|
|
|
@@ -1146,7 +1162,7 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testCreateIndexesWithDifferentOptions()
|
|
|
{
|
|
|
- $this->setExpectedException('MongoResultException');
|
|
|
+ $this->expectException(\MongoResultException::class);
|
|
|
|
|
|
$this->getCollection()->createIndex(['foo' => 1]);
|
|
|
|
|
|
@@ -1180,7 +1196,7 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testCreateIndexWithSameNameAndDifferentOptions()
|
|
|
{
|
|
|
- $this->setExpectedException('MongoResultException');
|
|
|
+ $this->expectException(\MongoResultException::class);
|
|
|
|
|
|
$this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
|
|
|
|
|
|
@@ -1228,7 +1244,8 @@ class MongoCollectionTest extends TestCase
|
|
|
$collection = $this->getCollection();
|
|
|
$collection->ensureIndex(['bar' => 1], ['unique' => true]);
|
|
|
|
|
|
- $this->setExpectedException('MongoResultException', 'Index with name: bar_1 already exists with different options');
|
|
|
+ $this->expectException(\MongoResultException::class);
|
|
|
+ $this->expectExceptionMessage('Index with name: bar_1 already exists with different options');
|
|
|
$collection->ensureIndex(['bar' => 1]);
|
|
|
}
|
|
|
|
|
|
@@ -1680,7 +1697,7 @@ class MongoCollectionTest extends TestCase
|
|
|
$this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
|
|
|
$collection = $this->getCollection();
|
|
|
|
|
|
- $this->setExpectedException('MongoResultException');
|
|
|
+ $this->expectException(\MongoResultException::class);
|
|
|
|
|
|
$collection->findAndModify(
|
|
|
array("inprogress" => false, "name" => "Next promo"),
|
|
|
@@ -1697,7 +1714,7 @@ class MongoCollectionTest extends TestCase
|
|
|
$id = '54203e08d51d4a1f868b456e';
|
|
|
$collection = $this->getCollection();
|
|
|
|
|
|
- $this->setExpectedException('MongoExecutionTimeoutException');
|
|
|
+ $this->expectException(\MongoExecutionTimeoutException::class);
|
|
|
|
|
|
$document = $collection->findAndModify(
|
|
|
['_id' => new \MongoId($id)],
|
|
|
@@ -1761,14 +1778,16 @@ class MongoCollectionTest extends TestCase
|
|
|
|
|
|
public function testEmptyCollectionName()
|
|
|
{
|
|
|
- $this->setExpectedException('Exception', 'Collection name cannot be empty');
|
|
|
+ $this->expectException(\Exception::class);
|
|
|
+ $this->expectExceptionMessage('Collection name cannot be empty');
|
|
|
|
|
|
new \MongoCollection($this->getDatabase(), '');
|
|
|
}
|
|
|
|
|
|
public function testSelectCollectionWithNullBytes()
|
|
|
{
|
|
|
- $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
|
|
|
+ $this->expectException(\Exception::class);
|
|
|
+ $this->expectExceptionMessage('Collection name cannot contain null bytes');
|
|
|
|
|
|
new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
|
|
|
}
|