mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-12 10:29:37 +00:00
some reordering and test optimizations to align new API.
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Testcontainers\ContainerRuntime;
|
||||||
|
|
||||||
|
use Docker\Docker as DockerClient;
|
||||||
|
|
||||||
|
class ContainerRuntimeClient
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var DockerClient|null Singleton instance of DockerClient
|
||||||
|
*/
|
||||||
|
private static ?DockerClient $dockerClient = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor to prevent creating instance outside the class.
|
||||||
|
*/
|
||||||
|
private function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the singleton DockerClient instance.
|
||||||
|
*
|
||||||
|
* @return DockerClient The singleton DockerClient instance.
|
||||||
|
* @throws \RuntimeException If the DockerClient instance could not be created.
|
||||||
|
*/
|
||||||
|
public static function getDockerClient(): DockerClient
|
||||||
|
{
|
||||||
|
if (self::$dockerClient === null) {
|
||||||
|
self::$dockerClient = DockerClient::create();
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$dockerClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Injects a DockerClient instance for testing or special use cases.
|
||||||
|
*
|
||||||
|
* @param DockerClient $client The DockerClient instance to set.
|
||||||
|
*/
|
||||||
|
public static function setDockerClient(DockerClient $client): void
|
||||||
|
{
|
||||||
|
self::$dockerClient = $client;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Testcontainers\Tests\Integration;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Testcontainers\Container\GenericContainer;
|
||||||
|
|
||||||
|
abstract class ContainerTestCase extends TestCase
|
||||||
|
{
|
||||||
|
protected static GenericContainer $container;
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
self::$container->remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Testcontainers\Tests\Integration;
|
||||||
|
|
||||||
|
use Testcontainers\Container\MariaDBContainer;
|
||||||
|
|
||||||
|
class MariaDBContainerTest extends ContainerTestCase
|
||||||
|
{
|
||||||
|
public static function setUpBeforeClass(): void
|
||||||
|
{
|
||||||
|
self::$container = (new MariaDBContainer())
|
||||||
|
->withMariaDBDatabase('foo')
|
||||||
|
->withMariaDBUser('bar', 'baz')
|
||||||
|
->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMySQLContainer(): void
|
||||||
|
{
|
||||||
|
$pdo = new \PDO(
|
||||||
|
sprintf('mysql:host=%s;port=3306', self::$container->getAddress()),
|
||||||
|
'bar',
|
||||||
|
'baz',
|
||||||
|
);
|
||||||
|
|
||||||
|
$query = $pdo->query('SHOW databases');
|
||||||
|
|
||||||
|
$this->assertInstanceOf(\PDOStatement::class, $query);
|
||||||
|
|
||||||
|
$databases = $query->fetchAll(\PDO::FETCH_COLUMN);
|
||||||
|
|
||||||
|
$this->assertContains('foo', $databases);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Testcontainers\Tests\Integration;
|
||||||
|
|
||||||
|
use Testcontainers\Container\MySQLContainer;
|
||||||
|
|
||||||
|
class MySQLContainerTest extends ContainerTestCase
|
||||||
|
{
|
||||||
|
public static function setUpBeforeClass(): void
|
||||||
|
{
|
||||||
|
self::$container = (new MySQLContainer())
|
||||||
|
->withMySQLDatabase('foo')
|
||||||
|
->withMySQLUser('bar', 'baz')
|
||||||
|
->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMySQLContainer(): void
|
||||||
|
{
|
||||||
|
$pdo = new \PDO(
|
||||||
|
sprintf('mysql:host=%s;port=3306', self::$container->getAddress()),
|
||||||
|
'bar',
|
||||||
|
'baz',
|
||||||
|
);
|
||||||
|
|
||||||
|
$query = $pdo->query('SHOW databases');
|
||||||
|
|
||||||
|
$this->assertInstanceOf(\PDOStatement::class, $query);
|
||||||
|
|
||||||
|
$databases = $query->fetchAll(\PDO::FETCH_COLUMN);
|
||||||
|
|
||||||
|
$this->assertContains('foo', $databases);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Testcontainers\Tests\Integration;
|
||||||
|
|
||||||
|
use Testcontainers\Container\OpenSearchContainer;
|
||||||
|
|
||||||
|
class OpenSearchContainerTest extends ContainerTestCase
|
||||||
|
{
|
||||||
|
public static function setUpBeforeClass(): void
|
||||||
|
{
|
||||||
|
self::$container = (new OpenSearchContainer())
|
||||||
|
->withDisabledSecurityPlugin()
|
||||||
|
->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \JsonException
|
||||||
|
*/
|
||||||
|
public function testOpenSearch(): void
|
||||||
|
{
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, sprintf('http://%s:%d', self::$container->getAddress(), 9200));
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
|
$response = (string) curl_exec($ch);
|
||||||
|
|
||||||
|
$this->assertNotEmpty($response);
|
||||||
|
|
||||||
|
/** @var array{cluster_name: string} $data */
|
||||||
|
$data = json_decode($response, true, JSON_THROW_ON_ERROR, JSON_THROW_ON_ERROR);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('cluster_name', $data);
|
||||||
|
|
||||||
|
$this->assertEquals('docker-cluster', $data['cluster_name']);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Testcontainers\Tests\Integration;
|
||||||
|
|
||||||
|
use Testcontainers\Container\PostgresContainer;
|
||||||
|
|
||||||
|
class PostgreSQLContainerTest extends ContainerTestCase
|
||||||
|
{
|
||||||
|
public static function setUpBeforeClass(): void
|
||||||
|
{
|
||||||
|
self::$container = (new PostgresContainer('latest', 'test'))
|
||||||
|
->withPostgresUser('test')
|
||||||
|
->withPostgresDatabase('foo')
|
||||||
|
->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPostgreSQLContainer(): void
|
||||||
|
{
|
||||||
|
$pdo = new \PDO(
|
||||||
|
sprintf('pgsql:host=%s;port=5432;dbname=foo', self::$container->getAddress()),
|
||||||
|
'test',
|
||||||
|
'test',
|
||||||
|
);
|
||||||
|
|
||||||
|
$query = $pdo->query('SELECT datname FROM pg_database');
|
||||||
|
|
||||||
|
$this->assertInstanceOf(\PDOStatement::class, $query);
|
||||||
|
|
||||||
|
$databases = $query->fetchAll(\PDO::FETCH_COLUMN);
|
||||||
|
|
||||||
|
$this->assertContains('foo', $databases);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user