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/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 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..15f3553 --- /dev/null +++ b/src/Container/PostgresContainer.php @@ -0,0 +1,36 @@ +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; + } +} 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..c7cd88a 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,27 @@ public function testOpenSearch(): void $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); + } }