PHP code example of shyim / testcontainer

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.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shyim / testcontainer example snippets




use Testcontainers\Container\Container;

$container = Container::make('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');


// 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());



use Testcontainers\Container\MySQLContainer;

$container = MySQLContainer::make('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



use Testcontainers\Container\MariaDBContainer;

$container = MariaDBContainer::make('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



use Testcontainers\Container\PostgresContainer;

$container = PostgresContainer::make('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


use Testcontainers\Container\RedisContainer;

$container = RedisContainer::make('6.0');

$container->run();

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

// Do something with redis


use Testcontainers\Container\OpenSearchContainer;

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

$container->run();

// Do something with opensearch


namespace App\Tests;

use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Tools\DsnParser;
use Testcontainers\Container\PostgresContainer;

class TestConnectionFactory extends ConnectionFactory
{
    static $testDsn;

    public function __construct(array $typesConfig, ?DsnParser $dsnParser = null)
    {
        if (!$this::$testDsn) {
            $psql = PostgresContainer::make('14.0', 'password');
            $psql->withPostgresDatabase('database');
            $psql->withPostgresUser('user');
            $psql->run();
            $this::$testDsn = sprintf('postgresql://user:password@%s:5432/database?serverVersion=14&charset=utf8', $psql->getAddress());
        }
        parent::__construct($typesConfig, $dsnParser);
    }


    public function createConnection(array $params, ?Configuration $config = null, ?EventManager $eventManager = null, array $mappingTypes = [])
    {
        $params['url'] = $this::$testDsn;
        return parent::createConnection($params, $config, $eventManager, $mappingTypes);
    }

}