Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Frontify/testcontainers-php

 
 

Repository files navigation

Testcontainers for PHP

Testcontainers is a PHP package that makes it simple to create and clean up container-based dependencies for automated integration/smoke tests. The package is inspired by the Testcontainers project for Java.

@sironheart has annoyed me to test testcontainers, but it didn't existed in PHP yet.

Installation

Add this to your project with composer

composer req --dev shyim/testcontainer

Usage/Examples

Starting a general Container

<?php

use Testcontainer\Container\MySQLContainer;

$container = new Container('nginx:alpine');

// set an environment variable
$container->withEnvironment('name', 'var');

// enable health check for an container
$container->withHealthCheckCommand('curl --fail localhost');

// mount current dir to /var/www/html
$container->withMount(__DIR__, '/var/www/html');

Normally you have to wait until the Container is ready. so for this you can define an wait rule:

// Run mysqladmin ping until the command returns exit code 0
$container->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']));

$container->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']), function(Process $process) {
    // throw exception if process result is bad
});

// Wait until that message is in the logs
$container->withWait(new WaitForLog('Ready to accept connections'));


// Wait for an http request to succeed
$container->withWait(WaitForHttp::make($port, $method = 'GET', $path = '/'));

// Wait until the docker heartcheck is green
$container->withWait(new WaitForHealthCheck());

MySQL

<?php

use Testcontainer\Container\MySQLContainer;

$container = new MySQLContainer('8.0');
$container->withMySQLDatabase('foo');
$container->withMySQLUser('bar', 'baz');

$container->run();

$pdo = new \PDO(
    sprintf('mysql:host=%s;port=3306', $container->getAddress()),
    'bar',
    'baz',
);

// Do something with pdo

MariaDB

<?php

use Testcontainer\Container\MariaDBContainer;

$container = new MariaDBContainer('8.0');
$container->withMariaDBDatabase('foo');
$container->withMariaDBUser('bar', 'baz');

$container->run();

$pdo = new \PDO(
    sprintf('mysql:host=%s;port=3306', $container->getAddress()),
    'bar',
    'baz',
);

// Do something with pdo

PostgreSQL

<?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

use Testcontainer\Container\RedisContainer;

$container = new RedisContainer('6.0');

$container->run();

$redis = new \Redis();
$redis->connect($container->getAddress());

// Do something with redis

OpenSearch

use Testcontainer\Container\OpenSearchContainer;

$container = new OpenSearchContainer('2');
$container->disableSecurityPlugin();

$container->run();

// Do something with opensearch

License

MIT

Releases

No releases published

Packages

No packages published

Languages

  • PHP 96.5%
  • Nix 3.5%