@@ -17,12 +17,15 @@ composer req --dev testcontainers/testcontainers
1717``` php
1818<?php
1919
20- use Testcontainers\Container\Container ;
20+ use Testcontainers\Container\GenericContainer ;
2121
22- $container = Container::make ('nginx:alpine');
22+ $container = new GenericContainer ('nginx:alpine');
2323
2424// set an environment variable
25- $container->withEnvironment('name', 'var');
25+ $container->withEnvironment([
26+ 'key1' => 'val1',
27+ 'key2' => 'val2'
28+ ]);
2629
2730// enable health check for an container
2831$container->withHealthCheckCommand('curl --fail localhost');
@@ -35,10 +38,19 @@ Normally you have to wait until the Container is ready. so for this you can defi
3538
3639``` php
3740
41+ use Testcontainers\Container\GenericContainer;
42+ use Testcontainers\Wait\WaitForExec;
43+ use Testcontainers\Wait\WaitForLog;
44+ use Testcontainers\Wait\WaitForHttp;
45+ use Testcontainers\Wait\WaitForHealthCheck;
46+ use Testcontainers\Wait\WaitForHostPort;
47+
48+ $container = new GenericContainer('nginx:alpine');
49+
3850// Run mysqladmin ping until the command returns exit code 0
3951$container->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']));
4052
41- $container->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']), function(Process $process ) {
53+ $container->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']), function($exitCode, $contents ) {
4254 // throw exception if process result is bad
4355});
4456
@@ -47,7 +59,10 @@ $container->withWait(new WaitForLog('Ready to accept connections'));
4759
4860
4961// Wait for an http request to succeed
50- $container->withWait(WaitForHttp::make($port, $method = 'GET', $path = '/'));
62+ $container->withWait(new WaitForHttp($port, $method = 'GET', $path = '/'));
63+
64+ // Wait for all bound ports to be open
65+ $container->withWait(new WaitForHostPort());
5166
5267// Wait until the docker heartcheck is green
5368$container->withWait(new WaitForHealthCheck());
@@ -58,16 +73,19 @@ $container->withWait(new WaitForHealthCheck());
5873``` php
5974<?php
6075
61- use Testcontainers\Container \MySQLContainer;
76+ use Testcontainers\Modules \MySQLContainer;
6277
63- $container = MySQLContainer::make('8.0');
64- $container->withMySQLDatabase('foo');
65- $container->withMySQLUser('bar', 'baz');
66-
67- $container->run();
78+ $container = (new MySQLContainer('8.0'))
79+ ->withMySQLDatabase('foo')
80+ ->withMySQLUser('bar', 'baz')
81+ ->start();
6882
6983$pdo = new \PDO(
70- sprintf('mysql:host=%s;port=3306', $container->getAddress()),
84+ sprintf(
85+ 'mysql:host=%s;port=%d',
86+ $container->getHost(),
87+ $container->getFirstMappedPort()
88+ ),
7189 'bar',
7290 'baz',
7391);
@@ -80,16 +98,19 @@ $pdo = new \PDO(
8098``` php
8199<?php
82100
83- use Testcontainers\Container\MariaDBContainer;
84-
85- $container = MariaDBContainer::make('8.0');
86- $container->withMariaDBDatabase('foo');
87- $container->withMariaDBUser('bar', 'baz');
101+ use Testcontainers\Modules\MariaDBContainer;
88102
89- $container->run();
103+ $container = $container = (new MariaDBContainer())
104+ ->withMariaDBDatabase('foo')
105+ ->withMariaDBUser('bar', 'baz')
106+ ->start();
90107
91108$pdo = new \PDO(
92- sprintf('mysql:host=%s;port=3306', $container->getAddress()),
109+ sprintf(
110+ 'mysql:host=%s;port=%d',
111+ $container->getHost(),
112+ $container->getFirstMappedPort()
113+ ),
93114 'bar',
94115 'baz',
95116);
@@ -102,18 +123,21 @@ $pdo = new \PDO(
102123``` php
103124<?php
104125
105- use Testcontainers\Container \PostgresContainer;
126+ use Testcontainers\Modules \PostgresContainer;
106127
107- $container = PostgresContainer::make('15.0', 'password');
108- $container->withPostgresDatabase('database');
109- $container->withPostgresUser('username');
110-
111- $container->run();
128+ $container = (new PostgresContainer())
129+ ->withPostgresUser('bar')
130+ ->withPostgresDatabase('foo')
131+ ->start();
112132
113133$pdo = new \PDO(
114- sprintf('pgsql:host=%s;port=5432;dbname=database', $container->getAddress()),
115- 'username',
116- 'password',
134+ sprintf(
135+ 'pgsql:host=%s;port=%d;dbname=foo',
136+ self::$container->getHost(),
137+ self::$container->getFirstMappedPort()
138+ ),
139+ 'bar',
140+ 'test',
117141);
118142
119143// Do something with pdo
@@ -123,14 +147,13 @@ $pdo = new \PDO(
123147
124148``` php
125149
126- use Testcontainers\Container \RedisContainer;
150+ use Testcontainers\Modules \RedisContainer;
127151
128- $container = RedisContainer::make('6.0');
129-
130- $container->run();
152+ $container = (new RedisContainer())
153+ ->start();
131154
132155$redis = new \Redis();
133- $redis->connect($container->getAddress ());
156+ $redis->connect($container->getHost(), $container->getFirstMappedPort ());
134157
135158// Do something with redis
136159```
@@ -139,12 +162,11 @@ $redis->connect($container->getAddress());
139162
140163``` php
141164
142- use Testcontainers\Container\OpenSearchContainer;
143-
144- $container = OpenSearchContainer::make('2');
145- $container->disableSecurityPlugin();
165+ use Testcontainers\Modules\OpenSearchContainer;
146166
147- $container->run();
167+ $container = (new OpenSearchContainer())
168+ ->withDisabledSecurityPlugin()
169+ ->start();
148170
149171// Do something with opensearch
150172```
@@ -166,7 +188,7 @@ use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
166188use Doctrine\Common\EventManager;
167189use Doctrine\DBAL\Configuration;
168190use Doctrine\DBAL\Tools\DsnParser;
169- use Testcontainers\Container \PostgresContainer;
191+ use Testcontainers\Modules \PostgresContainer;
170192
171193class TestConnectionFactory extends ConnectionFactory
172194{
@@ -175,11 +197,12 @@ class TestConnectionFactory extends ConnectionFactory
175197 public function __construct(array $typesConfig, ?DsnParser $dsnParser = null)
176198 {
177199 if (!$this::$testDsn) {
178- $psql = PostgresContainer::make('14.0', 'password');
179- $psql->withPostgresDatabase('database');
180- $psql->withPostgresUser('user');
181- $psql->run();
182- $this::$testDsn = sprintf('postgresql://user:password@%s:5432/database?serverVersion=14&charset=utf8', $psql->getAddress());
200+ $psql = (new PostgresContainer())
201+ ->withPostgresUser('user')
202+ ->withPostgresPassword('password')
203+ ->withPostgresDatabase('database')
204+ ->start();
205+ $this::$testDsn = sprintf('postgresql://user:password@%s:%d/database?serverVersion=14&charset=utf8', $psql->getAddress(), $psql->getFirstMappedPort());
183206 }
184207 parent::__construct($typesConfig, $dsnParser);
185208 }
0 commit comments