From bca69a129abff5a821b5d1f288f6b3db656bc83e Mon Sep 17 00:00:00 2001 From: pbromboszcz Date: Mon, 22 Dec 2025 09:09:04 +0100 Subject: [PATCH 1/6] remove PHPUnit TestCase::getGroups() usage --- tests/unit/Codeception/Module/MongoDbTest.php | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/unit/Codeception/Module/MongoDbTest.php b/tests/unit/Codeception/Module/MongoDbTest.php index 443e7ff..360ea44 100644 --- a/tests/unit/Codeception/Module/MongoDbTest.php +++ b/tests/unit/Codeception/Module/MongoDbTest.php @@ -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' => 'miles@davis.com']); + } + + 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' => 'miles@davis.com']); - } } protected function _tearDown() @@ -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'); From 7bceca5094261c588291285d9d6d101e2f85612c Mon Sep 17 00:00:00 2001 From: pbromboszcz Date: Mon, 22 Dec 2025 09:10:55 +0100 Subject: [PATCH 2/6] add missing mongodb-database-tools dependencies --- .github/workflows/main.yml | 9 +++++++++ src/Codeception/Lib/Driver/MongoDb.php | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index be19e5e..4b4cc8b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -29,6 +29,15 @@ jobs: 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 diff --git a/src/Codeception/Lib/Driver/MongoDb.php b/src/Codeception/Lib/Driver/MongoDb.php index 101fa6b..24c5521 100644 --- a/src/Codeception/Lib/Driver/MongoDb.php +++ b/src/Codeception/Lib/Driver/MongoDb.php @@ -112,7 +112,7 @@ public function cleanup(): void public function load(string $dumpFile): void { $cmd = sprintf( - 'mongo %s %s%s', + 'mongosh %s %s%s', $this->host . '/' . $this->dbName, $this->createUserPasswordCmdString(), escapeshellarg($dumpFile) From 782f1312e623257ac21798199a92002d7c9de793 Mon Sep 17 00:00:00 2001 From: pbromboszcz Date: Mon, 22 Dec 2025 09:11:57 +0100 Subject: [PATCH 3/6] add support for mongodb/mongodb v2 driver --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3e5848d..521ec13 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "require": { "php": "^8.0", "codeception/codeception": "^5.0", - "mongodb/mongodb": "^1.12" + "mongodb/mongodb": "^1.12 || ^2.0" }, "autoload": { "classmap": [ From af6600bb14b563503d7cd3032e11895eb7419b9c Mon Sep 17 00:00:00 2001 From: pbromboszcz Date: Mon, 22 Dec 2025 09:15:26 +0100 Subject: [PATCH 4/6] =?UTF-8?q?PHP=20add=20support=20for=208.2=E2=80=938.5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/main.yml | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4b4cc8b..7341478 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 }} @@ -29,6 +30,21 @@ 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 From ed99185c04ebf500a925819aad20d4bf180017e5 Mon Sep 17 00:00:00 2001 From: pbromboszcz Date: Mon, 22 Dec 2025 11:08:43 +0100 Subject: [PATCH 5/6] support mongosh with fallback to legacy mongo shell --- src/Codeception/Lib/Driver/MongoDb.php | 27 ++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Codeception/Lib/Driver/MongoDb.php b/src/Codeception/Lib/Driver/MongoDb.php index 24c5521..3c3034e 100644 --- a/src/Codeception/Lib/Driver/MongoDb.php +++ b/src/Codeception/Lib/Driver/MongoDb.php @@ -111,15 +111,38 @@ public function cleanup(): void */ public function load(string $dumpFile): void { + $shell = $this->findMongoShellBinary(); + $uri = $this->host . '/' . $this->dbName; + $cmd = sprintf( - 'mongosh %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('mongosh')) { + return 'mongosh'; + } + if ($this->commandExists('mongo')) { + return 'mongo'; + } + + throw new ModuleException($this, 'Neither mongosh nor mongo found in PATH.'); + } + + private function commandExists(string $cmd): bool + { + $which = sprintf('command -v %s 2>/dev/null', $cmd); + $out = shell_exec($which); + return is_string($out) && trim($out) !== ''; + } + public function loadFromMongoDump(string $dumpFile): void { [$host, $port] = $this->getHostPort(); From 9d15ed6636845c22dba6387fd2be5d2109f1c18d Mon Sep 17 00:00:00 2001 From: pbromboszcz Date: Mon, 22 Dec 2025 11:45:56 +0100 Subject: [PATCH 6/6] support mongosh with fallback to legacy mongo shell --- src/Codeception/Lib/Driver/MongoDb.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Codeception/Lib/Driver/MongoDb.php b/src/Codeception/Lib/Driver/MongoDb.php index 3c3034e..39b215f 100644 --- a/src/Codeception/Lib/Driver/MongoDb.php +++ b/src/Codeception/Lib/Driver/MongoDb.php @@ -126,21 +126,28 @@ public function load(string $dumpFile): void private function findMongoShellBinary(): string { - if ($this->commandExists('mongosh')) { - return 'mongosh'; - } 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 { - $which = sprintf('command -v %s 2>/dev/null', $cmd); - $out = shell_exec($which); - return is_string($out) && trim($out) !== ''; + $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