Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
php: [7.1, 7.2, 7.3, 7.4, 8.0]
php: [7.4, 8.0]

steps:
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"minimum-stability": "RC",
"require": {
"php": "^7.1 || ^8.0",
"php": "^7.4 || ^8.0",
"codeception/codeception": "^4.0"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ composer require "codeception/module-mongodb" --dev

## Requirements

* `PHP 7.1` or higher.
* `PHP 7.4` or higher.

## Documentation

Expand Down
34 changes: 12 additions & 22 deletions src/Codeception/Lib/Driver/MongoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,18 @@ class MongoDb
/**
* @var int
*/
const DEFAULT_PORT = 27017;
/**
* @var \Codeception\Lib\Driver\MongoDB|null
*/
private $dbh;
/**
* @var string|null
*/
private $dbName;
private $host;
private $user;
private $password;
/**
* @var \MongoDB\Client|null
*/
private $client;
/**
* @var string
*/
private $quiet = '';
public const DEFAULT_PORT = 27017;

private ?Database $dbh;

private ?string $dbName = null;
private string $host;
private string $user;
private string $password;

private ?\MongoDB\Client $client = null;

private string $quiet = '';

/**
* Connect to the Mongo server using the MongoDB extension.
Expand Down Expand Up @@ -117,8 +109,6 @@ public function cleanup(): void
/**
* 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 string $dumpFile
*/
public function load(string $dumpFile): void
{
Expand Down
68 changes: 29 additions & 39 deletions src/Codeception/Module/MongoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,31 @@ class MongoDb extends Module implements RequiresPackage
/**
* @var string
*/
const DUMP_TYPE_JS = 'js';
public const DUMP_TYPE_JS = 'js';

/**
* @var string
*/
const DUMP_TYPE_MONGODUMP = 'mongodump';
public const DUMP_TYPE_MONGODUMP = 'mongodump';

/**
* @var string
*/
const DUMP_TYPE_MONGODUMP_TAR_GZ = 'mongodump-tar-gz';
public const DUMP_TYPE_MONGODUMP_TAR_GZ = 'mongodump-tar-gz';

/**
* @api
* @var
*/
public $dbh;

/**
* @var
*/
protected $dumpFile;
protected ?string $dumpFile = null;

protected bool $isDumpFileEmpty = true;

/**
* @var bool
* @var mixed|null
*/
protected $isDumpFileEmpty = true;

protected $dbHash;

/**
Expand All @@ -104,15 +103,9 @@ class MongoDb extends Module implements RequiresPackage
'quiet' => false,
];

/**
* @var bool
*/
protected $populated = false;
protected bool $populated = false;

/**
* @var \Codeception\Lib\Driver\MongoDb
*/
public $driver;
public ?MongoDbDriver $driver = null;

/**
* @var string[]
Expand All @@ -127,11 +120,12 @@ public function _initialize()
$this->config['user'],
$this->config['password']
);
} catch (MongoConnectionException $e) {
throw new ModuleException(__CLASS__, $e->getMessage() . ' while creating Mongo connection');
} catch (MongoConnectionException $exception) {
throw new ModuleException(__CLASS__,
$exception->getMessage() . ' while creating Mongo connection'
);
}

// starting with loading dump
if ($this->config['populate']) {
$this->cleanup();
$this->loadDump();
Expand All @@ -149,6 +143,7 @@ private function validateDump(): void
Please, check path for dump file: " . $this->config['dump']
);
}

$this->dumpFile = Configuration::projectDir() . $this->config['dump'];
$this->isDumpFileEmpty = false;

Expand All @@ -158,6 +153,7 @@ private function validateDump(): void
if (count(explode("\n", $content)) === 0) {
$this->isDumpFileEmpty = true;
}

return;
}

Expand All @@ -169,6 +165,7 @@ private function validateDump(): void
Please, check dump: " . $this->config['dump']
);
}

$this->isDumpFileEmpty = true;
$dumpDir = dir($this->dumpFile);
while (false !== ($entry = $dumpDir->read())) {
Expand All @@ -177,6 +174,7 @@ private function validateDump(): void
break;
}
}

$dumpDir->close();
return;
}
Expand All @@ -188,13 +186,15 @@ private function validateDump(): void
"Tar gunzip archives are not supported for Windows systems"
);
}

if (!preg_match('#(\.tar\.gz|\.tgz)$#', $this->dumpFile)) {
throw new ModuleConfigException(
__CLASS__,
"Dump file must be a valid tar gunzip archive.\n
Please, check dump file: " . $this->config['dump']
);
}

return;
}

Expand Down Expand Up @@ -235,16 +235,17 @@ protected function shouldCleanup(): bool
protected function cleanup(): void
{
$dbh = $this->driver->getDbh();
if (!$dbh) {
if ($dbh === null) {
throw new ModuleConfigException(
__CLASS__,
"No connection to database. Remove this module from config if you don't need database repopulation"
);
}

try {
$this->driver->cleanup();
} catch (Exception $e) {
throw new ModuleException(__CLASS__, $e->getMessage());
} catch (Exception $exception) {
throw new ModuleException(__CLASS__, $exception->getMessage());
}
}

Expand All @@ -260,10 +261,12 @@ protected function loadDump(): void
if ($this->config['dump_type'] === self::DUMP_TYPE_JS) {
$this->driver->load($this->dumpFile);
}

if ($this->config['dump_type'] === self::DUMP_TYPE_MONGODUMP) {
$this->driver->setQuiet($this->config['quiet']);
$this->driver->loadFromMongoDump($this->dumpFile);
}

if ($this->config['dump_type'] === self::DUMP_TYPE_MONGODUMP_TAR_GZ) {
$this->driver->setQuiet($this->config['quiet']);
$this->driver->loadFromTarGzMongoDump($this->dumpFile);
Expand All @@ -284,8 +287,6 @@ protected function loadDump(): void
* <?php
* $I->useDatabase('db_1');
* ```
*
* @param string $dbName
*/
public function useDatabase(string $dbName): void
{
Expand All @@ -300,10 +301,6 @@ public function useDatabase(string $dbName): void
* $I->haveInCollection('users', ['name' => 'John', 'email' => 'john@coltrane.com']);
* $user_id = $I->haveInCollection('users', ['email' => 'john@coltrane.com']);
* ```
*
* @param string $collection
* @param array $data
* @return string
*/
public function haveInCollection(string $collection, array $data): string
{
Expand All @@ -320,8 +317,6 @@ public function haveInCollection(string $collection, array $data): string
* <?php
* $I->seeInCollection('users', ['name' => 'miles']);
* ```
*
* @param string $collection
*/
public function seeInCollection(string $collection, array $criteria = []): void
{
Expand All @@ -337,8 +332,6 @@ public function seeInCollection(string $collection, array $criteria = []): void
* <?php
* $I->dontSeeInCollection('users', ['name' => 'miles']);
* ```
*
* @param string $collection
*/
public function dontSeeInCollection(string $collection, array $criteria = []): void
{
Expand All @@ -355,7 +348,6 @@ public function dontSeeInCollection(string $collection, array $criteria = []): v
* $user = $I->grabFromCollection('users', ['name' => 'miles']);
* ```
*
* @param string $collection
* @return \MongoDB\Model\BSONDocument|mixed
*/
public function grabFromCollection(string $collection, array $criteria = [])
Expand All @@ -373,8 +365,6 @@ public function grabFromCollection(string $collection, array $criteria = [])
* // or
* $count = $I->grabCollectionCount('users', ['isAdmin' => true]);
* ```
*
* @param string $collection
*/
public function grabCollectionCount(string $collection, array $criteria = []): int
{
Expand Down Expand Up @@ -408,6 +398,7 @@ public function seeElementIsArray(string $collection, array $criteria = [], stri
'Error: you should test against a single element criteria when asserting that elementIsArray'
);
}

\PHPUnit\Framework\Assert::assertEquals(1, $res, 'Specified element is not a Mongo Object');
}

Expand Down Expand Up @@ -437,6 +428,7 @@ public function seeElementIsObject(string $collection, array $criteria = [], str
'Error: you should test against a single element criteria when asserting that elementIsObject'
);
}

\PHPUnit\Framework\Assert::assertEquals(1, $res, 'Specified element is not a Mongo Object');
}

Expand All @@ -448,8 +440,6 @@ public function seeElementIsObject(string $collection, array $criteria = [], str
* $I->seeNumElementsInCollection('users', 2);
* $I->seeNumElementsInCollection('users', 1, ['name' => 'miles']);
* ```
*
* @param string $collection
*/
public function seeNumElementsInCollection(string $collection, int $expected, array $criteria = []): void
{
Expand Down