1. Go to this page and download the library: Download shyim/testcontainer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shyim / testcontainer example snippets
useTestcontainers\Container\GenericContainer;
$container = new GenericContainer('nginx:alpine');
// set an environment variable
$container->withEnvironment([
'key1' => 'val1',
'key2' => 'val2'
]);
// enable health check for an container
$container->withHealthCheckCommand('curl --fail localhost');
// mount current dir to /var/www/html
$container->withMount(__DIR__, '/var/www/html');
useTestcontainers\Container\GenericContainer;
useTestcontainers\Wait\WaitForExec;
useTestcontainers\Wait\WaitForLog;
useTestcontainers\Wait\WaitForHttp;
useTestcontainers\Wait\WaitForHealthCheck;
useTestcontainers\Wait\WaitForHostPort;
$container = new GenericContainer('nginx:alpine');
// 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($exitCode, $contents){
// 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(new WaitForHttp($port, $method = 'GET', $path = '/'));
// Wait for all bound ports to be open
$container->withWait(new WaitForHostPort());
// Wait until the docker heartcheck is green
$container->withWait(new WaitForHealthCheck());
useTestcontainers\Modules\MySQLContainer;
$container = (new MySQLContainer('8.0'))
->withMySQLDatabase('foo')
->withMySQLUser('bar', 'baz')
->start();
$pdo = new \PDO(
sprintf(
'mysql:host=%s;port=%d',
$container->getHost(),
$container->getFirstMappedPort()
),
'bar',
'baz',
);
// Do something with pdo
useTestcontainers\Modules\MariaDBContainer;
$container = $container = (new MariaDBContainer())
->withMariaDBDatabase('foo')
->withMariaDBUser('bar', 'baz')
->start();
$pdo = new \PDO(
sprintf(
'mysql:host=%s;port=%d',
$container->getHost(),
$container->getFirstMappedPort()
),
'bar',
'baz',
);
// Do something with pdo
useTestcontainers\Modules\PostgresContainer;
$container = (new PostgresContainer())
->withPostgresUser('bar')
->withPostgresDatabase('foo')
->start();
$pdo = new \PDO(
sprintf(
'pgsql:host=%s;port=%d;dbname=foo',
self::$container->getHost(),
self::$container->getFirstMappedPort()
),
'bar',
'test',
);
// Do something with pdo
useTestcontainers\Modules\RedisContainer;
$container = (new RedisContainer())
->start();
$redis = new \Redis();
$redis->connect($container->getHost(), $container->getFirstMappedPort());
// Do something with redis
useTestcontainers\Modules\OpenSearchContainer;
$container = (new OpenSearchContainer())
->withDisabledSecurityPlugin()
->start();
// Do something with opensearch