From 45f0e11f6933c95363a5a8e39e7d78fa6859a447 Mon Sep 17 00:00:00 2001 From: Steffen Beisenherz <13799656+Sironheart@users.noreply.github.com> Date: Sun, 23 Oct 2022 14:56:44 +0200 Subject: [PATCH 1/4] feat: Add postgres container --- src/Container/Container.php | 2 +- src/Container/MariaDBContainer.php | 2 +- src/Container/MySQLContainer.php | 2 +- src/Container/OpenSearchContainer.php | 2 +- src/Container/PostgresContainer.php | 33 +++++++++++++++++++++++++++ src/Container/RedisContainer.php | 2 +- tests/Integration/ContainerTest.php | 23 +++++++++++++++++++ 7 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/Container/PostgresContainer.php diff --git a/src/Container/Container.php b/src/Container/Container.php index b02b8b6..319454c 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -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(); } diff --git a/src/Container/MariaDBContainer.php b/src/Container/MariaDBContainer.php index 7096e77..f131692 100644 --- a/src/Container/MariaDBContainer.php +++ b/src/Container/MariaDBContainer.php @@ -8,7 +8,7 @@ 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); diff --git a/src/Container/MySQLContainer.php b/src/Container/MySQLContainer.php index c233072..d9bfb42 100644 --- a/src/Container/MySQLContainer.php +++ b/src/Container/MySQLContainer.php @@ -8,7 +8,7 @@ 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); diff --git a/src/Container/OpenSearchContainer.php b/src/Container/OpenSearchContainer.php index 569864c..87c7f3a 100644 --- a/src/Container/OpenSearchContainer.php +++ b/src/Container/OpenSearchContainer.php @@ -8,7 +8,7 @@ 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'); diff --git a/src/Container/PostgresContainer.php b/src/Container/PostgresContainer.php new file mode 100644 index 0000000..cccb20c --- /dev/null +++ b/src/Container/PostgresContainer.php @@ -0,0 +1,33 @@ +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; + } +} diff --git a/src/Container/RedisContainer.php b/src/Container/RedisContainer.php index ab8e9c2..f6aef9f 100644 --- a/src/Container/RedisContainer.php +++ b/src/Container/RedisContainer.php @@ -8,7 +8,7 @@ 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')); diff --git a/tests/Integration/ContainerTest.php b/tests/Integration/ContainerTest.php index 9cf947c..3550670 100644 --- a/tests/Integration/ContainerTest.php +++ b/tests/Integration/ContainerTest.php @@ -9,6 +9,7 @@ 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 @@ public function testOpenSearch(): void $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); + } } From 54c5d34886efeebe7dc558b33736d535d510382f Mon Sep 17 00:00:00 2001 From: Steffen Beisenherz <13799656+Sironheart@users.noreply.github.com> Date: Sun, 23 Oct 2022 15:04:30 +0200 Subject: [PATCH 2/4] fix: Tests and postgres image --- .github/workflows/php.yml | 2 +- src/Container/PostgresContainer.php | 5 ++++- tests/Integration/ContainerTest.php | 5 +++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 5cf1afb..623a45f 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -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 diff --git a/src/Container/PostgresContainer.php b/src/Container/PostgresContainer.php index cccb20c..9d14a33 100644 --- a/src/Container/PostgresContainer.php +++ b/src/Container/PostgresContainer.php @@ -4,12 +4,15 @@ namespace Testcontainer\Container; +use Testcontainer\Wait\WaitForExec; + class PostgresContainer extends Container { private function __construct(string $version, string $rootPassword) { - parent::__construct('mariadb:' . $version); + 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 $mysqlRootPassword = 'root'): self diff --git a/tests/Integration/ContainerTest.php b/tests/Integration/ContainerTest.php index 3550670..a647024 100644 --- a/tests/Integration/ContainerTest.php +++ b/tests/Integration/ContainerTest.php @@ -101,11 +101,12 @@ public function testPostgreSQLContainer(): void { $container = PostgresContainer::make('latest', 'test') ->withPostgresUser('test') - ->withPostgresDatabase('foo'); + ->withPostgresDatabase('foo') + ->run(); $pdo = new \PDO( - sprintf('postgres:host=%s;port=3306', $container->getAddress()), + sprintf('postgres:host=%s;port=5432', $container->getAddress()), 'test', 'test', ); From a6c3c4daca3c85909ac87c9bab6798003168d548 Mon Sep 17 00:00:00 2001 From: Steffen Beisenherz Date: Sun, 23 Oct 2022 14:04:59 +0000 Subject: [PATCH 3/4] fix: pdo_pgsql usage --- src/Container/PostgresContainer.php | 4 ++-- tests/Integration/ContainerTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Container/PostgresContainer.php b/src/Container/PostgresContainer.php index 9d14a33..15f3553 100644 --- a/src/Container/PostgresContainer.php +++ b/src/Container/PostgresContainer.php @@ -15,9 +15,9 @@ private function __construct(string $version, string $rootPassword) $this->withWait(new WaitForExec(["pg_isready", "-h", "127.0.0.1"])); } - public static function make(string $version = 'latest', string $mysqlRootPassword = 'root'): self + public static function make(string $version = 'latest', string $dbPassword = 'root'): self { - return new self($version, $mysqlRootPassword); + return new self($version, $dbPassword); } public function withPostgresUser(string $username): self diff --git a/tests/Integration/ContainerTest.php b/tests/Integration/ContainerTest.php index a647024..c7cd88a 100644 --- a/tests/Integration/ContainerTest.php +++ b/tests/Integration/ContainerTest.php @@ -106,12 +106,12 @@ public function testPostgreSQLContainer(): void $pdo = new \PDO( - sprintf('postgres:host=%s;port=5432', $container->getAddress()), + sprintf('pgsql:host=%s;port=5432;dbname=foo', $container->getAddress()), 'test', 'test', ); - $query = $pdo->query('SHOW databases'); + $query = $pdo->query('SELECT datname FROM pg_database'); $this->assertInstanceOf(\PDOStatement::class, $query); From ef831a0d8e4022ebde041352752f0e82d017db52 Mon Sep 17 00:00:00 2001 From: Steffen Beisenherz Date: Sun, 23 Oct 2022 16:28:22 +0000 Subject: [PATCH 4/4] chore: Add PostgresContainer to README --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index f4ade8f..015f01b 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,28 @@ $pdo = new \PDO( // Do something with pdo ``` +### PostgreSQL + +```php +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