Merge pull request #1 from Sironheart/main

Add Postgres Container
This commit is contained in:
Shyim
2022-10-23 19:14:43 +02:00
committed by GitHub
9 changed files with 88 additions and 6 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: redis
extensions: redis, pgsql
- name: Install dependencies
run: composer install --prefer-dist --no-progress
+22
View File
@@ -99,6 +99,28 @@ $pdo = new \PDO(
// Do something with pdo
```
### PostgreSQL
```php
<?php
use Testcontainer\Container\PostgresContainer;
$container = new PostgresContainer('15.0', 'password');
$container->withPostgresDatabase('database');
$container->withPostgresUser('username');
$container->run();
$pdo = new \PDO(
sprintf('pgsql:host=%s;port=5432;dbname=database', $container->getAddress()),
'username',
'password',
);
// Do something with pdo
```
### Redis
```php
+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');
+36
View File
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Testcontainer\Container;
use Testcontainer\Wait\WaitForExec;
class PostgresContainer extends Container
{
private function __construct(string $version, string $rootPassword)
{
parent::__construct('postgres:' . $version);
$this->withEnvironment('POSTGRES_PASSWORD', $rootPassword);
$this->withWait(new WaitForExec(["pg_isready", "-h", "127.0.0.1"]));
}
public static function make(string $version = 'latest', string $dbPassword = 'root'): self
{
return new self($version, $dbPassword);
}
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'));
+24
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,27 @@ class ContainerTest extends TestCase
$this->assertEquals('docker-cluster', $data['cluster_name']);
}
public function testPostgreSQLContainer(): void
{
$container = PostgresContainer::make('latest', 'test')
->withPostgresUser('test')
->withPostgresDatabase('foo')
->run();
$pdo = new \PDO(
sprintf('pgsql:host=%s;port=5432;dbname=foo', $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);
}
}