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.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
Expand Down
47 changes: 24 additions & 23 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
136 changes: 47 additions & 89 deletions src/Codeception/Lib/Driver/MongoDb.php
Original file line number Diff line number Diff line change
@@ -1,84 +1,62 @@
<?php

declare(strict_types=1);

namespace Codeception\Lib\Driver;

use Codeception\Exception\ModuleConfigException;
use Codeception\Exception\ModuleException;
use Exception;
use MongoDB\Database;

class MongoDb
{
/**
* @var int
*/
const DEFAULT_PORT = 27017;

private $legacy;
/**
* @var \Codeception\Lib\Driver\MongoDB|null
*/
private $dbh;
private $dsn;
/**
* @var string|null
*/
private $dbName;
private $host;
private $user;
private $password;
/**
* @var \MongoDB\Client|null
*/
private $client;
/**
* @var string
*/
private $quiet = '';

public static function connect($dsn, $user, $password)
{
throw new \Exception(__CLASS__ . '::connect() - hm, it looked like this method had become obsolete...');
}

/**
* Connect to the Mongo server using the MongoDB extension.
*/
protected function setupMongoDB($dsn, $options)
protected function setupMongoDB(string $dsn, array $options): void
{
try {
$this->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);
}
}

Expand All @@ -87,29 +65,21 @@ 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');
}

/* 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;
}
Expand All @@ -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',
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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()
Expand All @@ -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) {
Expand All @@ -268,7 +226,7 @@ private function getHostPort()
throw new ModuleException($this, '$dsn MUST be like (mongodb://)<host>:<port>/<db name>');
}

public function setQuiet($quiet)
public function setQuiet(bool $quiet): void
{
$this->quiet = $quiet ? '--quiet' : '';
}
Expand Down
Loading