mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-08 19:29:39 +00:00
feat: Add postgres container
This commit is contained in:
@@ -38,7 +38,7 @@ class Container
|
||||
*/
|
||||
private array $mounts = [];
|
||||
|
||||
public function __construct(private string $image)
|
||||
protected function __construct(private string $image)
|
||||
{
|
||||
$this->wait = new WaitForNothing();
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use Testcontainer\Wait\WaitForExec;
|
||||
|
||||
class MariaDBContainer extends Container
|
||||
{
|
||||
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
|
||||
private function __construct(string $version, string $mysqlRootPassword)
|
||||
{
|
||||
parent::__construct('mariadb:' . $version);
|
||||
$this->withEnvironment('MARIADB_ROOT_PASSWORD', $mysqlRootPassword);
|
||||
|
||||
@@ -8,7 +8,7 @@ use Testcontainer\Wait\WaitForExec;
|
||||
|
||||
class MySQLContainer extends Container
|
||||
{
|
||||
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
|
||||
private function __construct(string $version, string $mysqlRootPassword)
|
||||
{
|
||||
parent::__construct('mysql:' . $version);
|
||||
$this->withEnvironment('MYSQL_ROOT_PASSWORD', $mysqlRootPassword);
|
||||
|
||||
@@ -8,7 +8,7 @@ use Testcontainer\Wait\WaitForHttp;
|
||||
|
||||
class OpenSearchContainer extends Container
|
||||
{
|
||||
public function __construct(string $version = 'latest')
|
||||
private function __construct(string $version)
|
||||
{
|
||||
parent::__construct('opensearchproject/opensearch:' . $version);
|
||||
$this->withEnvironment('discovery.type', 'single-node');
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Testcontainer\Container;
|
||||
|
||||
class PostgresContainer extends Container
|
||||
{
|
||||
private function __construct(string $version, string $rootPassword)
|
||||
{
|
||||
parent::__construct('mariadb:' . $version);
|
||||
$this->withEnvironment('POSTGRES_PASSWORD', $rootPassword);
|
||||
}
|
||||
|
||||
public static function make(string $version = 'latest', string $mysqlRootPassword = 'root'): self
|
||||
{
|
||||
return new self($version, $mysqlRootPassword);
|
||||
}
|
||||
|
||||
public function withPostgresUser(string $username): self
|
||||
{
|
||||
$this->withEnvironment('POSTGRES_USER', $username);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withPostgresDatabase(string $database): self
|
||||
{
|
||||
$this->withEnvironment('POSTGRES_DB', $database);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ use Testcontainer\Wait\WaitForLog;
|
||||
|
||||
class RedisContainer extends Container
|
||||
{
|
||||
public function __construct(string $version = 'latest')
|
||||
private function __construct(string $version)
|
||||
{
|
||||
parent::__construct('redis:' . $version);
|
||||
$this->withWait(new WaitForLog('Ready to accept connections'));
|
||||
|
||||
@@ -9,6 +9,7 @@ use Redis;
|
||||
use Testcontainer\Container\MariaDBContainer;
|
||||
use Testcontainer\Container\MySQLContainer;
|
||||
use Testcontainer\Container\OpenSearchContainer;
|
||||
use Testcontainer\Container\PostgresContainer;
|
||||
use Testcontainer\Container\RedisContainer;
|
||||
|
||||
class ContainerTest extends TestCase
|
||||
@@ -95,4 +96,26 @@ class ContainerTest extends TestCase
|
||||
|
||||
$this->assertEquals('docker-cluster', $data['cluster_name']);
|
||||
}
|
||||
|
||||
public function testPostgreSQLContainer(): void
|
||||
{
|
||||
$container = PostgresContainer::make('latest', 'test')
|
||||
->withPostgresUser('test')
|
||||
->withPostgresDatabase('foo');
|
||||
|
||||
|
||||
$pdo = new \PDO(
|
||||
sprintf('postgres:host=%s;port=3306', $container->getAddress()),
|
||||
'test',
|
||||
'test',
|
||||
);
|
||||
|
||||
$query = $pdo->query('SHOW databases');
|
||||
|
||||
$this->assertInstanceOf(\PDOStatement::class, $query);
|
||||
|
||||
$databases = $query->fetchAll(\PDO::FETCH_COLUMN);
|
||||
|
||||
$this->assertContains('foo', $databases);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user