Skip to content

Commit

Permalink
Merge pull request #1 from Sironheart/main
Browse files Browse the repository at this point in the history
Add Postgres Container
  • Loading branch information
shyim committed Oct 23, 2022
2 parents b90e75a + ef831a0 commit b770cdf
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Container/MariaDBContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Container/MySQLContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Container/OpenSearchContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
36 changes: 36 additions & 0 deletions src/Container/PostgresContainer.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Container/RedisContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit b770cdf

Please sign in to comment.