diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index be19e5e..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,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 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": [ diff --git a/src/Codeception/Lib/Driver/MongoDb.php b/src/Codeception/Lib/Driver/MongoDb.php index 101fa6b..39b215f 100644 --- a/src/Codeception/Lib/Driver/MongoDb.php +++ b/src/Codeception/Lib/Driver/MongoDb.php @@ -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(); 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');