feat: Add postgres container

This commit is contained in:
Steffen Beisenherz
2022-10-23 14:56:44 +02:00
parent b90e75aa3d
commit 45f0e11f69
7 changed files with 61 additions and 5 deletions
+1 -1
View File
@@ -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();
}
+1 -1
View File
@@ -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);
+1 -1
View File
@@ -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);
+1 -1
View File
@@ -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');
+33
View File
@@ -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;
}
}
+1 -1
View File
@@ -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'));
+23
View File
@@ -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);
}
}