Skip to content

Commit 36e04e5

Browse files
pbrombpbromboszcz
andauthored
Fix CI, update PHP support and MongoDB driver (#18)
* remove PHPUnit TestCase::getGroups() usage * add missing mongodb-database-tools dependencies * add support for mongodb/mongodb v2 driver * PHP add support for 8.2–8.5 * support mongosh with fallback to legacy mongo shell * support mongosh with fallback to legacy mongo shell --------- Co-authored-by: pbromboszcz <pbromboszcz@gbox.pl>
1 parent f9d3ed6 commit 36e04e5

File tree

4 files changed

+74
-20
lines changed

4 files changed

+74
-20
lines changed

.github/workflows/main.yml

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ jobs:
1313

1414
strategy:
1515
matrix:
16-
php: [8.0, 8.1]
16+
php: [8.0, 8.1, 8.2, 8.3, 8.4, 8.5]
1717

1818
steps:
1919
- name: Checkout code
20-
uses: actions/checkout@v2
20+
uses: actions/checkout@v6
2121

22-
- name: Setup PHP
22+
- name: Setup PHP != 8.5
23+
if: ${{ matrix.php != '8.5' }}
2324
uses: shivammathur/setup-php@v2
2425
with:
2526
php-version: ${{ matrix.php }}
@@ -29,6 +30,30 @@ jobs:
2930
env:
3031
fail-fast: true
3132

33+
- name: Setup PHP 8.5
34+
if: ${{ matrix.php == '8.5' }}
35+
uses: shivammathur/setup-php@v2
36+
with:
37+
php-version: ${{ matrix.php }}
38+
extensions: mbstring, xml, dom, mongodb
39+
tools: pecl
40+
coverage: none
41+
# this ini directive seems to be off by default in PHP 8.5
42+
# see https://github.com/php/php-src/issues/20279
43+
# enable it because codeception relies on it.
44+
ini-values: register_argc_argv=1
45+
env:
46+
fail-fast: true
47+
48+
- name: Install mongodb-database-tools
49+
run: |
50+
sudo apt-get update
51+
sudo apt-get install -y wget gnupg
52+
wget -qO - https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-8.0.gpg
53+
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
54+
sudo apt-get update
55+
sudo apt-get install -y mongodb-database-tools mongodb-mongosh
56+
3257
- name: Validate composer.json and composer.lock
3358
run: composer validate
3459

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"require": {
1515
"php": "^8.0",
1616
"codeception/codeception": "^5.0",
17-
"mongodb/mongodb": "^1.12"
17+
"mongodb/mongodb": "^1.12 || ^2.0"
1818
},
1919
"autoload": {
2020
"classmap": [

src/Codeception/Lib/Driver/MongoDb.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,45 @@ public function cleanup(): void
111111
*/
112112
public function load(string $dumpFile): void
113113
{
114+
$shell = $this->findMongoShellBinary();
115+
$uri = $this->host . '/' . $this->dbName;
116+
114117
$cmd = sprintf(
115-
'mongo %s %s%s',
116-
$this->host . '/' . $this->dbName,
118+
'%s %s %s%s',
119+
$shell,
120+
$uri,
117121
$this->createUserPasswordCmdString(),
118122
escapeshellarg($dumpFile)
119123
);
120124
shell_exec($cmd);
121125
}
122126

127+
private function findMongoShellBinary(): string
128+
{
129+
if ($this->commandExists('mongo')) {
130+
return 'mongo';
131+
}
132+
133+
if ($this->commandExists('mongosh')) {
134+
return 'mongosh';
135+
}
136+
137+
throw new ModuleException($this, 'Neither mongosh nor mongo found in PATH.');
138+
}
139+
140+
private function commandExists(string $cmd): bool
141+
{
142+
$null = PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null';
143+
144+
exec(
145+
sprintf('%s --version > %s 2>&1', escapeshellcmd($cmd), $null),
146+
$_,
147+
$code
148+
);
149+
150+
return $code === 0;
151+
}
152+
123153
public function loadFromMongoDump(string $dumpFile): void
124154
{
125155
[$host, $port] = $this->getHostPort();

tests/unit/Codeception/Module/MongoDbTest.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,26 @@ protected function _setUp()
4141
$this->markTestSkipped('MongoDB is not installed');
4242
}
4343

44-
$cleanupDirty = in_array('cleanup-dirty', $this->getGroups());
45-
$config = $this->mongoConfig + ['cleanup' => $cleanupDirty ? 'dirty' : true];
46-
4744
$client = new \MongoDB\Client();
4845

46+
$this->initMongoModule(true);
47+
48+
$this->db = $client->selectDatabase('test');
49+
$this->userCollection = $this->db->users;
50+
51+
$this->userCollection->insertOne(['id' => 1, 'email' => 'miles@davis.com']);
52+
}
53+
54+
private function initMongoModule($cleanup): void
55+
{
4956
$container = Stub::make(ModuleContainer::class);
5057
$this->module = new MongoDb($container);
51-
$this->module->_setConfig($config);
58+
$this->module->_setConfig($this->mongoConfig + ['cleanup' => $cleanup]);
5259
try {
5360
$this->module->_initialize();
5461
} catch (ModuleException $moduleException) {
5562
$this->markTestSkipped($moduleException->getMessage());
5663
}
57-
58-
$this->db = $client->selectDatabase('test');
59-
$this->userCollection = $this->db->users;
60-
61-
if (!$cleanupDirty) {
62-
$this->userCollection->insertOne(['id' => 1, 'email' => 'miles@davis.com']);
63-
}
6464
}
6565

6666
protected function _tearDown()
@@ -181,11 +181,10 @@ public function testLoadDump()
181181
}
182182
}
183183

184-
/**
185-
* @group cleanup-dirty
186-
*/
187184
public function testCleanupDirty()
188185
{
186+
$this->initMongoModule('dirty');
187+
189188
$test = $this->createMock(\Codeception\TestInterface::class);
190189
$collection = $this->db->selectCollection('96_bulls');
191190

0 commit comments

Comments
 (0)