- Notifications
You must be signed in to change notification settings - Fork27
https://www.testcontainers.org implementation for PHP
License
testcontainers/testcontainers-php
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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 theTestcontainers project for Java.
The officialtestcontainers/testcontainers-php library requires PHP 8.1 or newer. If you need to support older PHP versions, including those that are EOL, please check out the community-maintainedk-kinzal/testcontainers-php project. The discussion about supporting legacy versions lives inissue #38.
Add this to your project with composer
composer req --dev testcontainers/testcontainers
<?phpuseTestcontainers\Container\GenericContainer;$container =newGenericContainer('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');
Normally you have to wait until the Container is ready. so for this you can define an wait rule:
useTestcontainers\Container\GenericContainer;useTestcontainers\Wait\WaitForExec;useTestcontainers\Wait\WaitForLog;useTestcontainers\Wait\WaitForHttp;useTestcontainers\Wait\WaitForHealthCheck;useTestcontainers\Wait\WaitForHostPort;$container =newGenericContainer('nginx:alpine');// Run mysqladmin ping until the command returns exit code 0$container->withWait(newWaitForExec(['mysqladmin','ping','-h','127.0.0.1']));$container->withWait(newWaitForExec(['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(newWaitForLog('Ready to accept connections'));// Wait for an http request to succeed$container->withWait(newWaitForHttp($port,$method ='GET',$path ='/'));// Wait for all bound ports to be open$container->withWait(newWaitForHostPort());// Wait until the docker heartcheck is green$container->withWait(newWaitForHealthCheck());
<?phpuseTestcontainers\Modules\MySQLContainer;$container = (newMySQLContainer('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
<?phpuseTestcontainers\Modules\MariaDBContainer;$container =$container = (newMariaDBContainer()) ->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
<?phpuseTestcontainers\Modules\PostgresContainer;$container = (newPostgresContainer()) ->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 = (newRedisContainer()) ->start();$redis =new \Redis();$redis->connect($container->getHost(),$container->getFirstMappedPort());// Do something with redis
useTestcontainers\Modules\OpenSearchContainer;$container = (newOpenSearchContainer()) ->withDisabledSecurityPlugin() ->start();// Do something with opensearch
# config/packages/test/services.yamlparameters:'doctrine.dbal.connection_factory.class':App\Tests\TestConnectionFactory
namespaceApp\Tests;useDoctrine\Bundle\DoctrineBundle\ConnectionFactory;useDoctrine\Common\EventManager;useDoctrine\DBAL\Configuration;useDoctrine\DBAL\Tools\DsnParser;useTestcontainers\Modules\PostgresContainer;class TestConnectionFactoryextends ConnectionFactory{static$testDsn;publicfunction__construct(array$typesConfig, ?DsnParser$dsnParser =null) {if (!$this::$testDsn) {$psql = (newPostgresContainer()) ->withPostgresUser('user') ->withPostgresPassword('password') ->withPostgresDatabase('database') ->start();$this::$testDsn =sprintf('postgresql://user:password@%s:%d/database?serverVersion=14&charset=utf8',$psql->getAddress(),$psql->getFirstMappedPort()); }parent::__construct($typesConfig,$dsnParser); }publicfunctioncreateConnection(array$params, ?Configuration$config =null, ?EventManager$eventManager =null,array$mappingTypes = []) {$params['url'] =$this::$testDsn;returnparent::createConnection($params,$config,$eventManager,$mappingTypes); }}
About
https://www.testcontainers.org implementation for PHP
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.