diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e24a7b2..8cfbf38 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: - php: [7.0, 7.1, 7.2, 7.3, 7.4, 8.0] + php: [7.1, 7.2, 7.3, 7.4, 8.0] steps: - name: Checkout code diff --git a/composer.json b/composer.json index 438ecf5..51f141e 100644 --- a/composer.json +++ b/composer.json @@ -1,25 +1,26 @@ { - "name":"codeception/module-mongodb", - "description":"MongoDB module for Codeception", - "keywords":["codeception", "mongodb"], - "homepage":"http://codeception.com/", - "type":"library", - "license":"MIT", - "authors":[ - { - "name":"Michael Bodnarchuk" - } - ], - "minimum-stability": "RC", - - "require": { - "php": ">=5.6.0 <9.0", - "codeception/codeception": "^4.0" - }, - "autoload":{ - "classmap": ["src/"] - }, - "config": { - "classmap-authoritative": true - } + "name": "codeception/module-mongodb", + "description": "MongoDB module for Codeception", + "keywords": [ "codeception", "mongodb" ], + "homepage": "https://codeception.com/", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Michael Bodnarchuk" + } + ], + "minimum-stability": "RC", + "require": { + "php": "^7.1 || ^8.0", + "codeception/codeception": "^4.0" + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "config": { + "classmap-authoritative": true + } } diff --git a/readme.md b/readme.md index 86d9967..64bb378 100644 --- a/readme.md +++ b/readme.md @@ -13,6 +13,10 @@ A MongoDB module for Codeception. composer require "codeception/module-mongodb" --dev ``` +## Requirements + +* `PHP 7.1` or higher. + ## Documentation See [the module documentation](https://codeception.com/docs/modules/MongoDb). diff --git a/src/Codeception/Lib/Driver/MongoDb.php b/src/Codeception/Lib/Driver/MongoDb.php index 0880ef4..09aa46e 100644 --- a/src/Codeception/Lib/Driver/MongoDb.php +++ b/src/Codeception/Lib/Driver/MongoDb.php @@ -1,84 +1,62 @@ client = new \MongoDB\Client($dsn, $options); $this->dbh = $this->client->selectDatabase($this->dbName); - } catch (\MongoDB\Driver\Exception $e) { - throw new ModuleException($this, sprintf('Failed to open Mongo connection: %s', $e->getMessage())); - } - } - - /** - * Connect to the Mongo server using the legacy mongo extension. - */ - protected function setupMongo($dsn, $options) - { - try { - $this->client = new \MongoClient($dsn, $options); - $this->dbh = $this->client->selectDB($this->dbName); - } catch (\MongoConnectionException $e) { - throw new ModuleException($this, sprintf('Failed to open Mongo connection: %s', $e->getMessage())); + } catch (\MongoDB\Driver\Exception $exception) { + throw new ModuleException($this, sprintf('Failed to open Mongo connection: %s', $exception->getMessage())); } } /** * Clean up the Mongo database using the MongoDB extension. */ - protected function cleanupMongoDB() + protected function cleanupMongoDB(): void { try { $this->dbh->drop(); } catch (\MongoDB\Driver\Exception $e) { - throw new \Exception(sprintf('Failed to drop the DB: %s', $e->getMessage())); - } - } - - /** - * Clean up the Mongo database using the legacy Mongo extension. - */ - protected function cleanupMongo() - { - try { - $list = $this->dbh->listCollections(); - } catch (\MongoException $e) { - throw new \Exception(sprintf('Failed to list collections of the DB: %s', $e->getMessage())); - } - foreach ($list as $collection) { - try { - $collection->drop(); - } catch (\MongoException $e) { - throw new \Exception(sprintf('Failed to drop collection: %s', $e->getMessage())); - } + throw new Exception(sprintf('Failed to drop the DB: %s', $e->getMessage()), $e->getCode(), $e); } } @@ -87,21 +65,13 @@ protected function cleanupMongo() * * @static * - * @param $dsn - * @param $user - * @param $password - * * @throws ModuleConfigException - * @throws \Exception + * @throws Exception */ - public function __construct($dsn, $user, $password) + public function __construct(string $dsn, string $user, string $password) { - $this->legacy = extension_loaded('mongodb') === false && - class_exists('\\MongoClient') && - strpos(\MongoClient::VERSION, 'mongofill') === false; - /* defining DB name */ - $this->dbName = preg_replace('/\?.*/', '', substr($dsn, strrpos($dsn, '/') + 1)); + $this->dbName = preg_replace('#\?.*#', '', substr($dsn, strrpos($dsn, '/') + 1)); if (strlen($this->dbName) == 0) { throw new ModuleConfigException($this, 'Please specify valid $dsn with DB name after the host:port'); @@ -109,7 +79,7 @@ class_exists('\\MongoClient') && /* defining host */ if (strpos($dsn, 'mongodb://') !== false) { - $this->host = str_replace('mongodb://', '', preg_replace('/\?.*/', '', $dsn)); + $this->host = str_replace('mongodb://', '', preg_replace('#\?.*#', '', $dsn)); } else { $this->host = $dsn; } @@ -126,39 +96,31 @@ class_exists('\\MongoClient') && ]; } - $this->{$this->legacy ? 'setupMongo' : 'setupMongoDB'}($dsn, $options); - - $this->dsn = $dsn; + $this->setupMongoDB($dsn, $options); $this->user = $user; $this->password = $password; } /** * @static - * - * @param $dsn - * @param $user - * @param $password - * - * @return MongoDb */ - public static function create($dsn, $user, $password) + public static function create(string $dsn, string $user, string $password): \Codeception\Lib\Driver\MongoDb { return new MongoDb($dsn, $user, $password); } - public function cleanup() + public function cleanup(): void { - $this->{$this->legacy ? 'cleanupMongo' : 'cleanupMongoDB'}(); + $this->cleanupMongoDB(); } /** * dump file has to be a javascript document where one can use all the mongo shell's commands * just FYI: this file can be easily created be RockMongo's export button * - * @param $dumpFile + * @param string $dumpFile */ - public function load($dumpFile) + public function load(string $dumpFile): void { $cmd = sprintf( 'mongo %s %s%s', @@ -169,9 +131,9 @@ public function load($dumpFile) shell_exec($cmd); } - public function loadFromMongoDump($dumpFile) + public function loadFromMongoDump(string $dumpFile): void { - list($host, $port) = $this->getHostPort(); + [$host, $port] = $this->getHostPort(); $cmd = sprintf( "mongorestore %s --host %s --port %s -d %s %s %s", $this->quiet, @@ -184,9 +146,9 @@ public function loadFromMongoDump($dumpFile) shell_exec($cmd); } - public function loadFromTarGzMongoDump($dumpFile) + public function loadFromTarGzMongoDump(string $dumpFile): void { - list($host, $port) = $this->getHostPort(); + [$host, $port] = $this->getHostPort(); $getDirCmd = sprintf( "tar -tf %s | awk 'BEGIN { FS = \"/\" } ; { print $1 }' | uniq", escapeshellarg($dumpFile) @@ -213,7 +175,7 @@ public function loadFromTarGzMongoDump($dumpFile) shell_exec($cmd); } - private function createUserPasswordCmdString() + private function createUserPasswordCmdString(): string { if ($this->user && $this->password) { return sprintf( @@ -225,14 +187,17 @@ private function createUserPasswordCmdString() return ''; } + /** + * @return \Codeception\Lib\Driver\MongoDb|\MongoDB\Database|null + */ public function getDbh() { return $this->dbh; } - public function setDatabase($dbName) + public function setDatabase(string $dbName): void { - $this->dbh = $this->client->{$this->legacy ? 'selectDB' : 'selectDatabase'}($dbName); + $this->dbh = $this->client->selectDatabase($dbName); } public function getDbHash() @@ -243,20 +208,13 @@ public function getDbHash() $result = iterator_to_array($result); } - return isset($result[0]->md5) ? $result[0]->md5 : null; + return $result[0]->md5 ?? null; } /** - * Determine if this driver is using the legacy extension or not. - * - * @return bool + * @return string[]|int[] */ - public function isLegacy() - { - return $this->legacy; - } - - private function getHostPort() + private function getHostPort(): array { $hostPort = explode(':', $this->host); if (count($hostPort) === 2) { @@ -268,7 +226,7 @@ private function getHostPort() throw new ModuleException($this, '$dsn MUST be like (mongodb://):/'); } - public function setQuiet($quiet) + public function setQuiet(bool $quiet): void { $this->quiet = $quiet ? '--quiet' : ''; } diff --git a/src/Codeception/Module/MongoDb.php b/src/Codeception/Module/MongoDb.php index 42a4413..5c9152c 100644 --- a/src/Codeception/Module/MongoDb.php +++ b/src/Codeception/Module/MongoDb.php @@ -1,13 +1,18 @@ true, 'cleanup' => true, + 'dsn' => '', 'dump' => null, 'dump_type' => self::DUMP_TYPE_JS, - 'user' => null, - 'password' => null, + 'user' => '', + 'password' => '', 'quiet' => false, ]; + /** + * @var bool + */ protected $populated = false; /** @@ -90,18 +114,20 @@ class MongoDb extends CodeceptionModule implements RequiresPackage */ public $driver; + /** + * @var string[] + */ protected $requiredFields = ['dsn']; public function _initialize() { - try { $this->driver = MongoDbDriver::create( $this->config['dsn'], $this->config['user'], $this->config['password'] ); - } catch (\MongoConnectionException $e) { + } catch (MongoConnectionException $e) { throw new ModuleException(__CLASS__, $e->getMessage() . ' while creating Mongo connection'); } @@ -113,9 +139,9 @@ public function _initialize() } } - private function validateDump() + private function validateDump(): void { - if ($this->config['dump'] && ($this->config['cleanup'] or ($this->config['populate']))) { + if ($this->config['dump'] && ($this->config['cleanup'] || $this->config['populate'])) { if (!file_exists(Configuration::projectDir() . $this->config['dump'])) { throw new ModuleConfigException( __CLASS__, @@ -128,8 +154,8 @@ private function validateDump() if ($this->config['dump_type'] === self::DUMP_TYPE_JS) { $content = file_get_contents($this->dumpFile); - $content = trim(preg_replace('%/\*(?:(?!\*/).)*\*/%s', "", $content)); - if (!sizeof(explode("\n", $content))) { + $content = trim(preg_replace('#/\*(?:(?!\*/).)*\*/#s', "", $content)); + if (count(explode("\n", $content)) === 0) { $this->isDumpFileEmpty = true; } return; @@ -162,7 +188,7 @@ private function validateDump() "Tar gunzip archives are not supported for Windows systems" ); } - if (!preg_match('/(\.tar\.gz|\.tgz)$/', $this->dumpFile)) { + if (!preg_match('#(\.tar\.gz|\.tgz)$#', $this->dumpFile)) { throw new ModuleConfigException( __CLASS__, "Dump file must be a valid tar gunzip archive.\n @@ -195,7 +221,7 @@ public function _after(TestInterface $test) $this->populated = false; } - protected function shouldCleanup() + protected function shouldCleanup(): bool { if ($this->populated) { return false; @@ -206,7 +232,7 @@ protected function shouldCleanup() : (bool)$this->config['cleanup']; } - protected function cleanup() + protected function cleanup(): void { $dbh = $this->driver->getDbh(); if (!$dbh) { @@ -217,12 +243,12 @@ protected function cleanup() } try { $this->driver->cleanup(); - } catch (\Exception $e) { + } catch (Exception $e) { throw new ModuleException(__CLASS__, $e->getMessage()); } } - protected function loadDump() + protected function loadDump(): void { $this->validateDump(); @@ -242,7 +268,7 @@ protected function loadDump() $this->driver->setQuiet($this->config['quiet']); $this->driver->loadFromTarGzMongoDump($this->dumpFile); } - } catch (\Exception $e) { + } catch (Exception $e) { throw new ModuleException(__CLASS__, $e->getMessage()); } @@ -254,14 +280,14 @@ protected function loadDump() /** * Specify the database to use * - * ``` php + * ```php * useDatabase('db_1'); * ``` * - * @param $dbName + * @param string $dbName */ - public function useDatabase($dbName) + public function useDatabase(string $dbName): void { $this->driver->setDatabase($dbName); } @@ -269,22 +295,19 @@ public function useDatabase($dbName) /** * Inserts data into collection * - * ``` php + * ```php * haveInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com')); - * $user_id = $I->haveInCollection('users', array('email' => 'john@coltrane.com')); + * $I->haveInCollection('users', ['name' => 'John', 'email' => 'john@coltrane.com']); + * $user_id = $I->haveInCollection('users', ['email' => 'john@coltrane.com']); * ``` * - * @param $collection + * @param string $collection * @param array $data + * @return string */ - public function haveInCollection($collection, array $data) + public function haveInCollection(string $collection, array $data): string { $collection = $this->driver->getDbh()->selectCollection($collection); - if ($this->driver->isLegacy()) { - $collection->insert($data); - return $data['_id']; - } $response = $collection->insertOne($data); return (string) $response->getInsertedId(); @@ -293,15 +316,14 @@ public function haveInCollection($collection, array $data) /** * Checks if collection contains an item. * - * ``` php + * ```php * seeInCollection('users', array('name' => 'miles')); + * $I->seeInCollection('users', ['name' => 'miles']); * ``` * - * @param $collection - * @param array $criteria + * @param string $collection */ - public function seeInCollection($collection, $criteria = []) + public function seeInCollection(string $collection, array $criteria = []): void { $collection = $this->driver->getDbh()->selectCollection($collection); $res = $collection->count($criteria); @@ -311,15 +333,14 @@ public function seeInCollection($collection, $criteria = []) /** * Checks if collection doesn't contain an item. * - * ``` php + * ```php * dontSeeInCollection('users', array('name' => 'miles')); + * $I->dontSeeInCollection('users', ['name' => 'miles']); * ``` * - * @param $collection - * @param array $criteria + * @param string $collection */ - public function dontSeeInCollection($collection, $criteria = []) + public function dontSeeInCollection(string $collection, array $criteria = []): void { $collection = $this->driver->getDbh()->selectCollection($collection); $res = $collection->count($criteria); @@ -329,16 +350,15 @@ public function dontSeeInCollection($collection, $criteria = []) /** * Grabs a data from collection * - * ``` php + * ```php * grabFromCollection('users', array('name' => 'miles')); + * $user = $I->grabFromCollection('users', ['name' => 'miles']); * ``` * - * @param $collection - * @param array $criteria - * @return array + * @param string $collection + * @return \MongoDB\Model\BSONDocument|mixed */ - public function grabFromCollection($collection, $criteria = []) + public function grabFromCollection(string $collection, array $criteria = []) { $collection = $this->driver->getDbh()->selectCollection($collection); return $collection->findOne($criteria); @@ -347,18 +367,16 @@ public function grabFromCollection($collection, $criteria = []) /** * Grabs the documents count from a collection * - * ``` php + * ```php * grabCollectionCount('users'); * // or - * $count = $I->grabCollectionCount('users', array('isAdmin' => true)); + * $count = $I->grabCollectionCount('users', ['isAdmin' => true]); * ``` * - * @param $collection - * @param array $criteria - * @return integer + * @param string $collection */ - public function grabCollectionCount($collection, $criteria = []) + public function grabCollectionCount(string $collection, array $criteria = []): int { $collection = $this->driver->getDbh()->selectCollection($collection); return $collection->count($criteria); @@ -367,16 +385,12 @@ public function grabCollectionCount($collection, $criteria = []) /** * Asserts that an element in a collection exists and is an Array * - * ``` php + * ```php * seeElementIsArray('users', array('name' => 'John Doe') , 'data.skills'); + * $I->seeElementIsArray('users', ['name' => 'John Doe'], 'data.skills'); * ``` - * - * @param String $collection - * @param Array $criteria - * @param String $elementToCheck */ - public function seeElementIsArray($collection, $criteria = [], $elementToCheck = null) + public function seeElementIsArray(string $collection, array $criteria = [], string $elementToCheck = null): void { $collection = $this->driver->getDbh()->selectCollection($collection); @@ -385,7 +399,7 @@ public function seeElementIsArray($collection, $criteria = [], $elementToCheck = $criteria, [ $elementToCheck => ['$exists' => true], - '$where' => "Array.isArray(this.{$elementToCheck})" + '$where' => sprintf('Array.isArray(this.%s)', $elementToCheck) ] ) ); @@ -400,16 +414,12 @@ public function seeElementIsArray($collection, $criteria = [], $elementToCheck = /** * Asserts that an element in a collection exists and is an Object * - * ``` php + * ```php * seeElementIsObject('users', array('name' => 'John Doe') , 'data'); + * $I->seeElementIsObject('users', ['name' => 'John Doe'], 'data'); * ``` - * - * @param String $collection - * @param Array $criteria - * @param String $elementToCheck */ - public function seeElementIsObject($collection, $criteria = [], $elementToCheck = null) + public function seeElementIsObject(string $collection, array $criteria = [], string $elementToCheck = null): void { $collection = $this->driver->getDbh()->selectCollection($collection); @@ -418,7 +428,7 @@ public function seeElementIsObject($collection, $criteria = [], $elementToCheck $criteria, [ $elementToCheck => ['$exists' => true], - '$where' => "! Array.isArray(this.{$elementToCheck}) && isObject(this.{$elementToCheck})" + '$where' => sprintf('! Array.isArray(this.%s) && isObject(this.%s)', $elementToCheck, $elementToCheck) ] ) ); @@ -433,17 +443,15 @@ public function seeElementIsObject($collection, $criteria = [], $elementToCheck /** * Count number of records in a collection * - * ``` php + * ```php * seeNumElementsInCollection('users', 2); - * $I->seeNumElementsInCollection('users', 1, array('name' => 'miles')); + * $I->seeNumElementsInCollection('users', 1, ['name' => 'miles']); * ``` * - * @param $collection - * @param integer $expected - * @param array $criteria + * @param string $collection */ - public function seeNumElementsInCollection($collection, $expected, $criteria = []) + public function seeNumElementsInCollection(string $collection, int $expected, array $criteria = []): void { $collection = $this->driver->getDbh()->selectCollection($collection); $res = $collection->count($criteria); @@ -452,6 +460,8 @@ public function seeNumElementsInCollection($collection, $expected, $criteria = [ /** * Returns list of classes and corresponding packages required for this module + * + * @return array */ public function _requires() { diff --git a/tests/unit/Codeception/Module/MongoDbLegacyTest.php b/tests/unit/Codeception/Module/MongoDbLegacyTest.php deleted file mode 100644 index 4c8e9d8..0000000 --- a/tests/unit/Codeception/Module/MongoDbLegacyTest.php +++ /dev/null @@ -1,149 +0,0 @@ - 'mongodb://localhost:27017/test' - ); - - /** - * @var MongoDb - */ - protected $module; - - /** - * @var \MongoDb - */ - protected $db; - - /** - * @var \MongoCollection - */ - private $userCollection; - - protected function _setUp() - { - if (!class_exists('Mongo')) { - $this->markTestSkipped('Mongo is not installed'); - } - if (!class_exists('MongoDB\Client')) { - $this->markTestSkipped('MongoDb\Client is not installed'); - } - - $mongo = new \MongoClient(); - - $container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer'); - $this->module = new MongoDb($container); - $this->module->_setConfig($this->mongoConfig); - $this->module->_initialize(); - - $this->db = $mongo->selectDB('test'); - $this->userCollection = $this->db->createCollection('users'); - $this->userCollection->insert(array('id' => 1, 'email' => 'miles@davis.com')); - } - - protected function _tearDown() - { - if (!is_null($this->userCollection)) { - $this->userCollection->drop(); - } - } - - public function testSeeInCollection() - { - $this->module->seeInCollection('users', array('email' => 'miles@davis.com')); - } - - public function testDontSeeInCollection() - { - $this->module->dontSeeInCollection('users', array('email' => 'davert@davert.com')); - } - - public function testHaveAndSeeInCollection() - { - $this->module->haveInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com')); - $this->module->seeInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com')); - } - - public function testGrabFromCollection() - { - $user = $this->module->grabFromCollection('users', array('id' => 1)); - $this->assertArrayHasKey('email', $user); - $this->assertEquals('miles@davis.com', $user['email']); - } - - public function testSeeNumElementsInCollection() - { - $this->module->seeNumElementsInCollection('users', 1); - $this->module->seeNumElementsInCollection('users', 1, array('email' => 'miles@davis.com')); - $this->module->seeNumElementsInCollection('users', 0, array('name' => 'Doe')); - } - - public function testGrabCollectionCount() - { - $this->userCollection->insert(array('id' => 2, 'email' => 'louis@armstrong.com')); - $this->userCollection->insert(array('id' => 3, 'email' => 'dizzy@gillespie.com')); - - $this->assertEquals(1, $this->module->grabCollectionCount('users', array('id' => 3))); - $this->assertEquals(3, $this->module->grabCollectionCount('users')); - } - - public function testSeeElementIsArray() - { - $this->userCollection->insert(array('id' => 4, 'trumpets' => array('piccolo', 'bass', 'slide'))); - - $this->module->seeElementIsArray('users', array('id' => 4), 'trumpets'); - } - - - public function testSeeElementIsArrayThrowsError() - { - $this->expectException('PHPUnit\Framework\ExpectationFailedException'); - - $this->userCollection->insert(array('id' => 5, 'trumpets' => array('piccolo', 'bass', 'slide'))); - $this->userCollection->insert(array('id' => 6, 'trumpets' => array('piccolo', 'bass', 'slide'))); - $this->module->seeElementIsArray('users', array(), 'trumpets'); - } - - public function testSeeElementIsObject() - { - $trumpet = new \StdClass; - - $trumpet->name = 'Trumpet 1'; - $trumpet->pitch = 'B♭'; - $trumpet->price = array('min' => 458, 'max' => 891); - - $this->userCollection->insert(array('id' => 6, 'trumpet' => $trumpet)); - - $this->module->seeElementIsObject('users', array('id' => 6), 'trumpet'); - } - - public function testSeeElementIsObjectThrowsError() - { - $trumpet = new \StdClass; - - $trumpet->name = 'Trumpet 1'; - $trumpet->pitch = 'B♭'; - $trumpet->price = array('min' => 458, 'max' => 891); - - $this->expectException('PHPUnit\Framework\ExpectationFailedException'); - - $this->userCollection->insert(array('id' => 5, 'trumpet' => $trumpet)); - $this->userCollection->insert(array('id' => 6, 'trumpet' => $trumpet)); - - $this->module->seeElementIsObject('users', array(), 'trumpet'); - } - - public function testUseDatabase() - { - $this->module->useDatabase('example'); - $this->module->haveInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me')); - $this->module->seeInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me')); - $this->module->dontSeeInCollection('users', array('email' => 'miles@davis.com')); - } -} diff --git a/tests/unit/Codeception/Module/MongoDbTest.php b/tests/unit/Codeception/Module/MongoDbTest.php index cc053d2..9ea3990 100644 --- a/tests/unit/Codeception/Module/MongoDbTest.php +++ b/tests/unit/Codeception/Module/MongoDbTest.php @@ -1,19 +1,24 @@ 'mongodb://localhost:27017/test?connectTimeoutMS=300', 'dump' => 'tests/data/dumps/mongo.js', 'populate' => true - ); + ]; /** * @var MongoDb @@ -39,22 +44,22 @@ protected function _setUp() $cleanupDirty = in_array('cleanup-dirty', $this->getGroups()); $config = $this->mongoConfig + ['cleanup' => $cleanupDirty ? 'dirty' : true]; - $mongo = new \MongoDB\Client(); + $client = new \MongoDB\Client(); - $container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer'); + $container = Stub::make(ModuleContainer::class); $this->module = new MongoDb($container); $this->module->_setConfig($config); try { $this->module->_initialize(); - } catch (ModuleException $e) { - $this->markTestSkipped($e->getMessage()); + } catch (ModuleException $moduleException) { + $this->markTestSkipped($moduleException->getMessage()); } - $this->db = $mongo->selectDatabase('test'); + $this->db = $client->selectDatabase('test'); $this->userCollection = $this->db->users; if (!$cleanupDirty) { - $this->userCollection->insertOne(array('id' => 1, 'email' => 'miles@davis.com')); + $this->userCollection->insertOne(['id' => 1, 'email' => 'miles@davis.com']); } } @@ -67,23 +72,23 @@ protected function _tearDown() public function testSeeInCollection() { - $this->module->seeInCollection('users', array('email' => 'miles@davis.com')); + $this->module->seeInCollection('users', ['email' => 'miles@davis.com']); } public function testDontSeeInCollection() { - $this->module->dontSeeInCollection('users', array('email' => 'davert@davert.com')); + $this->module->dontSeeInCollection('users', ['email' => 'davert@davert.com']); } public function testHaveAndSeeInCollection() { - $this->module->haveInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com')); - $this->module->seeInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com')); + $this->module->haveInCollection('users', ['name' => 'John', 'email' => 'john@coltrane.com']); + $this->module->seeInCollection('users', ['name' => 'John', 'email' => 'john@coltrane.com']); } public function testGrabFromCollection() { - $user = $this->module->grabFromCollection('users', array('id' => 1)); + $user = $this->module->grabFromCollection('users', ['id' => 1]); $this->assertArrayHasKey('email', $user); $this->assertEquals('miles@davis.com', $user['email']); } @@ -91,71 +96,72 @@ public function testGrabFromCollection() public function testSeeNumElementsInCollection() { $this->module->seeNumElementsInCollection('users', 1); - $this->module->seeNumElementsInCollection('users', 1, array('email' => 'miles@davis.com')); - $this->module->seeNumElementsInCollection('users', 0, array('name' => 'Doe')); + $this->module->seeNumElementsInCollection('users', 1, ['email' => 'miles@davis.com']); + $this->module->seeNumElementsInCollection('users', 0, ['name' => 'Doe']); } public function testGrabCollectionCount() { - $this->userCollection->insertOne(array('id' => 2, 'email' => 'louis@armstrong.com')); - $this->userCollection->insertOne(array('id' => 3, 'email' => 'dizzy@gillespie.com')); + $this->userCollection->insertOne(['id' => 2, 'email' => 'louis@armstrong.com']); + $this->userCollection->insertOne(['id' => 3, 'email' => 'dizzy@gillespie.com']); - $this->assertEquals(1, $this->module->grabCollectionCount('users', array('id' => 3))); + $this->assertEquals(1, $this->module->grabCollectionCount('users', ['id' => 3])); $this->assertEquals(3, $this->module->grabCollectionCount('users')); } public function testSeeElementIsArray() { - $this->userCollection->insertOne(array('id' => 4, 'trumpets' => array('piccolo', 'bass', 'slide'))); + $this->userCollection->insertOne(['id' => 4, 'trumpets' => ['piccolo', 'bass', 'slide']]); - $this->module->seeElementIsArray('users', array('id' => 4), 'trumpets'); + $this->module->seeElementIsArray('users', ['id' => 4], 'trumpets'); } public function testSeeElementIsArrayThrowsError() { - $this->expectException('PHPUnit\Framework\ExpectationFailedException'); + $this->expectException(ExpectationFailedException::class); - $this->userCollection->insertOne(array('id' => 5, 'trumpets' => array('piccolo', 'bass', 'slide'))); - $this->userCollection->insertOne(array('id' => 6, 'trumpets' => array('piccolo', 'bass', 'slide'))); - $this->module->seeElementIsArray('users', array(), 'trumpets'); + $this->userCollection->insertOne(['id' => 5, 'trumpets' => ['piccolo', 'bass', 'slide']]); + $this->userCollection->insertOne(['id' => 6, 'trumpets' => ['piccolo', 'bass', 'slide']]); + + $this->module->seeElementIsArray('users', [], 'trumpets'); } public function testSeeElementIsObject() { - $trumpet = new \StdClass; + $trumpet = new StdClass; $trumpet->name = 'Trumpet 1'; $trumpet->pitch = 'B♭'; - $trumpet->price = array('min' => 458, 'max' => 891); + $trumpet->price = ['min' => 458, 'max' => 891]; - $this->userCollection->insertOne(array('id' => 6, 'trumpet' => $trumpet)); + $this->userCollection->insertOne(['id' => 6, 'trumpet' => $trumpet]); - $this->module->seeElementIsObject('users', array('id' => 6), 'trumpet'); + $this->module->seeElementIsObject('users', ['id' => 6], 'trumpet'); } public function testSeeElementIsObjectThrowsError() { - $trumpet = new \StdClass; + $trumpet = new StdClass; $trumpet->name = 'Trumpet 1'; $trumpet->pitch = 'B♭'; - $trumpet->price = array('min' => 458, 'max' => 891); + $trumpet->price = ['min' => 458, 'max' => 891]; - $this->expectException('PHPUnit\Framework\ExpectationFailedException'); + $this->expectException(ExpectationFailedException::class); - $this->userCollection->insertOne(array('id' => 5, 'trumpet' => $trumpet)); - $this->userCollection->insertOne(array('id' => 6, 'trumpet' => $trumpet)); + $this->userCollection->insertOne(['id' => 5, 'trumpet' => $trumpet]); + $this->userCollection->insertOne(['id' => 6, 'trumpet' => $trumpet]); - $this->module->seeElementIsObject('users', array(), 'trumpet'); + $this->module->seeElementIsObject('users', [], 'trumpet'); } public function testUseDatabase() { $this->module->useDatabase('example'); - $this->module->haveInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me')); - $this->module->seeInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me')); - $this->module->dontSeeInCollection('users', array('email' => 'miles@davis.com')); + $this->module->haveInCollection('stuff', ['name' => 'Ashley', 'email' => 'me@ashleyclarke.me']); + $this->module->seeInCollection('stuff', ['name' => 'Ashley', 'email' => 'me@ashleyclarke.me']); + $this->module->dontSeeInCollection('users', ['email' => 'miles@davis.com']); } public function testLoadDump() @@ -180,7 +186,7 @@ public function testLoadDump() */ public function testCleanupDirty() { - $test = $this->createMock('Codeception\TestInterface'); + $test = $this->createMock(\Codeception\TestInterface::class); $collection = $this->db->selectCollection('96_bulls'); $hash1 = $this->module->driver->getDbHash(); @@ -192,7 +198,7 @@ public function testCleanupDirty() $this->module->_before($test); // No cleanup expected $this->assertEquals($hash1, $this->module->driver->getDbHash()); - $collection->insertOne(array('name' => 'Coby White','position' => 'pg')); + $collection->insertOne(['name' => 'Coby White','position' => 'pg']); $hashDirty = $this->module->driver->getDbHash(); $this->assertNotEquals($hash1, $hashDirty);