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
31 changes: 28 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ jobs:

strategy:
matrix:
php: [8.0, 8.1]
php: [8.0, 8.1, 8.2, 8.3, 8.4, 8.5]

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v6

- name: Setup PHP
- name: Setup PHP != 8.5
if: ${{ matrix.php != '8.5' }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
Expand All @@ -29,6 +30,30 @@ jobs:
env:
fail-fast: true

- name: Setup PHP 8.5
if: ${{ matrix.php == '8.5' }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, xml, dom, mongodb
tools: pecl
coverage: none
# this ini directive seems to be off by default in PHP 8.5
# see https://github.com/php/php-src/issues/20279
# enable it because codeception relies on it.
ini-values: register_argc_argv=1
env:
fail-fast: true

- name: Install mongodb-database-tools
run: |
sudo apt-get update
sudo apt-get install -y wget gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-8.0.gpg
echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
sudo apt-get update
sudo apt-get install -y mongodb-database-tools mongodb-mongosh

- name: Validate composer.json and composer.lock
run: composer validate

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": "^8.0",
"codeception/codeception": "^5.0",
"mongodb/mongodb": "^1.12"
"mongodb/mongodb": "^1.12 || ^2.0"
},
"autoload": {
"classmap": [
Expand Down
34 changes: 32 additions & 2 deletions src/Codeception/Lib/Driver/MongoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,45 @@ public function cleanup(): void
*/
public function load(string $dumpFile): void
{
$shell = $this->findMongoShellBinary();
$uri = $this->host . '/' . $this->dbName;

$cmd = sprintf(
'mongo %s %s%s',
$this->host . '/' . $this->dbName,
'%s %s %s%s',
$shell,
$uri,
$this->createUserPasswordCmdString(),
escapeshellarg($dumpFile)
);
shell_exec($cmd);
}

private function findMongoShellBinary(): string
{
if ($this->commandExists('mongo')) {
return 'mongo';
}

if ($this->commandExists('mongosh')) {
return 'mongosh';
}

throw new ModuleException($this, 'Neither mongosh nor mongo found in PATH.');
}

private function commandExists(string $cmd): bool
{
$null = PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null';

exec(
sprintf('%s --version > %s 2>&1', escapeshellcmd($cmd), $null),
$_,
$code
);

return $code === 0;
}

public function loadFromMongoDump(string $dumpFile): void
{
[$host, $port] = $this->getHostPort();
Expand Down
27 changes: 13 additions & 14 deletions tests/unit/Codeception/Module/MongoDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@ protected function _setUp()
$this->markTestSkipped('MongoDB is not installed');
}

$cleanupDirty = in_array('cleanup-dirty', $this->getGroups());
$config = $this->mongoConfig + ['cleanup' => $cleanupDirty ? 'dirty' : true];

$client = new \MongoDB\Client();

$this->initMongoModule(true);

$this->db = $client->selectDatabase('test');
$this->userCollection = $this->db->users;

$this->userCollection->insertOne(['id' => 1, 'email' => '[email protected]']);
}

private function initMongoModule($cleanup): void
{
$container = Stub::make(ModuleContainer::class);
$this->module = new MongoDb($container);
$this->module->_setConfig($config);
$this->module->_setConfig($this->mongoConfig + ['cleanup' => $cleanup]);
try {
$this->module->_initialize();
} catch (ModuleException $moduleException) {
$this->markTestSkipped($moduleException->getMessage());
}

$this->db = $client->selectDatabase('test');
$this->userCollection = $this->db->users;

if (!$cleanupDirty) {
$this->userCollection->insertOne(['id' => 1, 'email' => '[email protected]']);
}
}

protected function _tearDown()
Expand Down Expand Up @@ -181,11 +181,10 @@ public function testLoadDump()
}
}

/**
* @group cleanup-dirty
*/
public function testCleanupDirty()
{
$this->initMongoModule('dirty');

$test = $this->createMock(\Codeception\TestInterface::class);
$collection = $this->db->selectCollection('96_bulls');

Expand Down