Add mongodb module

This commit is contained in:
stan220
2025-05-08 10:47:41 +03:00
parent dc94649504
commit 4134cf951d
4 changed files with 69 additions and 1 deletions
+1 -1
View File
@@ -57,7 +57,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: redis, pgsql
extensions: redis, pgsql, mongodb-mongodb/mongo-php-driver@1.15.0
- name: Install dependencies
run: composer install --prefer-dist --no-progress
+1
View File
@@ -19,6 +19,7 @@
"beluga-php/docker-php": "^1.45"
},
"require-dev": {
"ext-mongodb": "*",
"ext-pdo": "*",
"ext-pdo_mysql": "*",
"ext-pdo_pgsql": "*",
+36
View File
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Testcontainers\Modules;
use Testcontainers\Container\GenericContainer;
use Testcontainers\Wait\WaitForExec;
class MongoDBContainer extends GenericContainer
{
private const STARTUP_TIMEOUT_MS = 30_000;
public function __construct(
string $version = 'latest',
public readonly string $username = 'test',
public readonly string $password = 'test',
public readonly string $database = 'test',
) {
parent::__construct('mongo:' . $version);
$this
->withExposedPorts("27017/tcp")
->withEnvironment([
"MONGO_INITDB_ROOT_USERNAME" => $this->username,
"MONGO_INITDB_ROOT_PASSWORD" => $this->password,
"MONGO_INITDB_DATABASE" => $this->database,
])
->withWait(new WaitForExec([
'mongosh', 'admin',
'-u', $this->username,
'-p', $this->password,
'--eval', "'show dbs'",
], null, self::STARTUP_TIMEOUT_MS));
}
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Integration;
use Testcontainers\Container\StartedGenericContainer;
use Testcontainers\Modules\MongoDBContainer;
use Testcontainers\Tests\Integration\ContainerTestCase;
class MongoDBContainerTest extends ContainerTestCase
{
public function setUp(): void
{
$this->container = (new MongoDBContainer())->start();
}
public function testMongoDBContainer(): void
{
self::assertInstanceOf(StartedGenericContainer::class, $this->container);
$pingResult = $this->container->exec([
'mongosh', 'admin',
'-u', 'test',
'-p', 'test',
'--eval', "'db.runCommand(\"ping\").ok'",
]);
self::assertEquals(1, $pingResult);
}
}