Procházet zdrojové kódy

Allow PHPUnit 6.x on PHP 7

Andreas Braun před 8 roky
rodič
revize
93ebbb3246

+ 1 - 1
.travis.yml

@@ -8,7 +8,7 @@ matrix:
   fast_finish: true
   include:
     - php: 5.6
-      env: DRIVER_VERSION=stable LEGACY_DRIVER_VERSION=stable COMPOSER_FLAGS="--prefer-dist --prefer-lowest" SERVER_VERSION=3.0 COLUMNS=80
+      env: DRIVER_VERSION=stable LEGACY_DRIVER_VERSION=stable COMPOSER_FLAGS="--prefer-dist --prefer-lowest" SERVER_VERSION=3.0
       addons:
         apt:
           sources:

+ 1 - 1
composer.json

@@ -15,7 +15,7 @@
         "mongodb/mongodb": "^1.0.1"
     },
     "require-dev": {
-        "phpunit/phpunit": "^5.4"
+        "phpunit/phpunit": "^5.7 || ^6.0"
     },
     "provide": {
         "ext-mongo": "1.6.14"

+ 2 - 1
tests/Alcaeus/MongoDbAdapter/ExceptionConverterTest.php

@@ -4,8 +4,9 @@ namespace Alcaeus\MongoDbAdapter\Tests;
 
 use MongoDB\Driver\Exception;
 use Alcaeus\MongoDbAdapter\ExceptionConverter;
+use PHPUnit\Framework\TestCase;
 
-class ExceptionConverterTest extends \PHPUnit_Framework_TestCase
+class ExceptionConverterTest extends TestCase
 {
     /**
      * @dataProvider exceptionProvider

+ 4 - 2
tests/Alcaeus/MongoDbAdapter/Mongo/MongoClientTest.php

@@ -24,14 +24,16 @@ class MongoClientTest extends TestCase
 
     public function testSelectDBWithEmptyName()
     {
-        $this->setExpectedException('Exception', 'Database name cannot be empty');
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('Database name cannot be empty');
 
         $this->getClient()->selectDB('');
     }
 
     public function testSelectDBWithInvalidName()
     {
-        $this->setExpectedException('Exception', 'Database name contains invalid characters');
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('Database name contains invalid characters');
 
         $this->getClient()->selectDB('/');
     }

+ 48 - 29
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -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));
     }

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

@@ -56,7 +56,7 @@ class MongoCursorTest extends TestCase
         $client = $this->getClient(['connect' => false], 'mongodb://localhost:28888');
         $cursor = $client->selectCollection('mongo-php-adapter', 'test')->find();
 
-        $this->setExpectedException('MongoConnectionException');
+        $this->expectException(\MongoConnectionException::class);
 
         $cursor->count();
     }

+ 11 - 7
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php

@@ -17,14 +17,16 @@ class MongoDBTest extends TestCase
 
     public function testEmptyDatabaseName()
     {
-        $this->setExpectedException('Exception', 'Database name cannot be empty');
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('Database name cannot be empty');
 
         new \MongoDB($this->getClient(), '');
     }
 
     public function testInvalidDatabaseName()
     {
-        $this->setExpectedException('Exception', 'Database name contains invalid characters');
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('Database name contains invalid characters');
 
         new \MongoDB($this->getClient(), '/');
     }
@@ -41,7 +43,8 @@ class MongoDBTest extends TestCase
     {
         $database = $this->getDatabase();
 
-        $this->setExpectedException('Exception', 'Collection name cannot be empty');
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('Collection name cannot be empty');
 
         $database->selectCollection('');
     }
@@ -50,7 +53,8 @@ class MongoDBTest extends TestCase
     {
         $database = $this->getDatabase();
 
-        $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('Collection name cannot contain null bytes');
 
         $database->selectCollection('foo' . chr(0));
     }
@@ -257,7 +261,7 @@ class MongoDBTest extends TestCase
 
         $this->failMaxTimeMS();
 
-        $this->setExpectedException('MongoExecutionTimeoutException');
+        $this->expectException(\MongoExecutionTimeoutException::class);
 
         $database->getCollectionNames(['maxTimeMS' => 1]);
     }
@@ -286,7 +290,7 @@ class MongoDBTest extends TestCase
 
         $this->failMaxTimeMS();
 
-        $this->setExpectedException('MongoExecutionTimeoutException');
+        $this->expectException(\MongoExecutionTimeoutException::class);
 
         $database->getCollectionInfo(['maxTimeMS' => 1]);
     }
@@ -342,7 +346,7 @@ class MongoDBTest extends TestCase
     {
         $this->failMaxTimeMS();
 
-        $this->setExpectedException('MongoExecutionTimeoutException');
+        $this->expectException(\MongoExecutionTimeoutException::class);
 
         $this->getDatabase()->listCollections(['maxTimeMS' => 1]);
     }

+ 2 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDeleteBatchTest.php

@@ -63,7 +63,8 @@ class MongoDeleteBatchTest extends TestCase
         $collection = $this->getCollection();
         $batch = new \MongoDeleteBatch($collection);
 
-        $this->setExpectedException('Exception', "Expected \$item to contain 'q' key");
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage("Expected \$item to contain 'q' key");
 
         $batch->add([]);
     }

+ 10 - 5
tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSTest.php

@@ -345,7 +345,8 @@ class MongoGridFSTest extends TestCase
         $document = ['_id' => $id];
         $collection->insert($document);
 
-        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
+        $this->expectException(\MongoGridFSException::class);
+        $this->expectExceptionMessageRegExp('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
 
         $collection->storeBytes('foo', ['_id' => $id]);
     }
@@ -358,7 +359,8 @@ class MongoGridFSTest extends TestCase
         $document = ['n' => 0];
         $collection->chunks->insert($document);
 
-        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
+        $this->expectException(\MongoGridFSException::class);
+        $this->expectExceptionMessageRegExp('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
 
         $collection->storeBytes('foo');
     }
@@ -371,7 +373,8 @@ class MongoGridFSTest extends TestCase
         $document = ['_id' => $id];
         $collection->insert($document);
 
-        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
+        $this->expectException(\MongoGridFSException::class);
+        $this->expectExceptionMessageRegExp('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
 
         $collection->storeFile(__FILE__, ['_id' => $id]);
     }
@@ -384,7 +387,8 @@ class MongoGridFSTest extends TestCase
         $document = ['n' => 0];
         $collection->chunks->insert($document);
 
-        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
+        $this->expectException(\MongoGridFSException::class);
+        $this->expectExceptionMessageRegExp('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
 
         $collection->storeFile(__FILE__);
     }
@@ -397,7 +401,8 @@ class MongoGridFSTest extends TestCase
         $document = ['length' => filesize(__FILE__)];
         $collection->insert($document);
 
-        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
+        $this->expectException(\MongoGridFSException::class);
+        $this->expectExceptionMessageRegExp('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
 
         $collection->storeFile(fopen(__FILE__, 'r'));
     }

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

@@ -4,7 +4,7 @@ namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
 
 use Alcaeus\MongoDbAdapter\Tests\TestCase;
 
-class MongoLogTest extends \PHPUnit_Framework_Testcase
+class MongoLogTest extends Testcase
 {
     public function testSetCallback()
     {

+ 2 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoUpdateBatchTest.php

@@ -180,7 +180,8 @@ class MongoUpdateBatchTest extends TestCase
         $collection = $this->getCollection();
         $batch = new \MongoUpdateBatch($collection);
 
-        $this->setExpectedException('Exception', "Expected \$item to contain 'q' key");
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage("Expected \$item to contain 'q' key");
 
         $batch->add([]);
     }

+ 2 - 1
tests/Alcaeus/MongoDbAdapter/TestCase.php

@@ -3,8 +3,9 @@
 namespace Alcaeus\MongoDbAdapter\Tests;
 
 use MongoDB\Client;
+use PHPUnit\Framework\TestCase as BaseTestCase;
 
-abstract class TestCase extends \PHPUnit_Framework_TestCase
+abstract class TestCase extends BaseTestCase
 {
     protected function tearDown()
     {