mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-12 09:29:35 +00:00
fix issue with passing envs into the container. Remove container registry usage.
This commit is contained in:
@@ -14,7 +14,6 @@ use Docker\API\Model\PortBinding;
|
|||||||
use Docker\Docker;
|
use Docker\Docker;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Testcontainers\ContainerRuntime\ContainerRuntimeClient;
|
use Testcontainers\ContainerRuntime\ContainerRuntimeClient;
|
||||||
use Testcontainers\Registry;
|
|
||||||
use Testcontainers\Wait\WaitForContainerRunning;
|
use Testcontainers\Wait\WaitForContainerRunning;
|
||||||
use Testcontainers\Wait\WaitInterface;
|
use Testcontainers\Wait\WaitInterface;
|
||||||
|
|
||||||
@@ -225,7 +224,11 @@ class GenericContainer
|
|||||||
$hostConfig->setPortBindings($portMap);
|
$hostConfig->setPortBindings($portMap);
|
||||||
$containerCreatePostBody->setHostConfig($hostConfig);
|
$containerCreatePostBody->setHostConfig($hostConfig);
|
||||||
$containerCreatePostBody->setImage($this->image);
|
$containerCreatePostBody->setImage($this->image);
|
||||||
//$containerCreatePostBody->setEnv($this->env);
|
$envs = [];
|
||||||
|
foreach ($this->env as $key => $value) {
|
||||||
|
$envs[] = $key . '=' . $value;
|
||||||
|
}
|
||||||
|
$containerCreatePostBody->setEnv($envs);
|
||||||
|
|
||||||
$containerCreateResponse = $this->dockerClient->containerCreate($containerCreatePostBody);
|
$containerCreateResponse = $this->dockerClient->containerCreate($containerCreatePostBody);
|
||||||
$this->id = $containerCreateResponse?->getId() ?? '';
|
$this->id = $containerCreateResponse?->getId() ?? '';
|
||||||
@@ -237,8 +240,6 @@ class GenericContainer
|
|||||||
return $this->start();
|
return $this->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
Registry::add($this);
|
|
||||||
|
|
||||||
$this->dockerClient->containerStart($this->id);
|
$this->dockerClient->containerStart($this->id);
|
||||||
|
|
||||||
if(!isset($this->wait)) {
|
if(!isset($this->wait)) {
|
||||||
@@ -260,8 +261,6 @@ class GenericContainer
|
|||||||
$this->dockerClient->containerStop($this->id);
|
$this->dockerClient->containerStop($this->id);
|
||||||
$this->dockerClient->containerDelete($this->id);
|
$this->dockerClient->containerDelete($this->id);
|
||||||
|
|
||||||
Registry::remove($this);
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,7 +316,7 @@ class GenericContainer
|
|||||||
{
|
{
|
||||||
$response = $this->dockerClient->containerInspect($this->id);
|
$response = $this->dockerClient->containerInspect($this->id);
|
||||||
$settings = $response->getNetworkSettings();
|
$settings = $response->getNetworkSettings();
|
||||||
var_dump($settings);
|
//var_dump($settings);
|
||||||
|
|
||||||
$ports = [];
|
$ports = [];
|
||||||
foreach ($settings->getPorts() as $port => $value) {
|
foreach ($settings->getPorts() as $port => $value) {
|
||||||
|
|||||||
@@ -4,24 +4,19 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Testcontainers\Container;
|
namespace Testcontainers\Container;
|
||||||
|
|
||||||
use Testcontainers\Wait\WaitForExec;
|
|
||||||
|
|
||||||
class MariaDBContainer extends GenericContainer
|
class MariaDBContainer extends GenericContainer
|
||||||
{
|
{
|
||||||
private function __construct(string $version, string $mysqlRootPassword)
|
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
|
||||||
{
|
{
|
||||||
parent::__construct('mariadb:' . $version);
|
parent::__construct('mariadb:' . $version);
|
||||||
|
$this->withExposedPorts(3306);
|
||||||
$this->withEnvironment('MARIADB_ROOT_PASSWORD', $mysqlRootPassword);
|
$this->withEnvironment('MARIADB_ROOT_PASSWORD', $mysqlRootPassword);
|
||||||
|
|
||||||
$binary = 'mysqladmin';
|
|
||||||
|
|
||||||
if ($version === 'latest' || version_compare($version, '11.0.0', '>')) {
|
|
||||||
$binary = 'mariadb-admin';
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->withWait(new WaitForExec([$binary, 'ping', '-h', '127.0.0.1']));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use constructor instead
|
||||||
|
* Left for backward compatibility
|
||||||
|
*/
|
||||||
public static function make(string $version = 'latest', string $mysqlRootPassword = 'root'): self
|
public static function make(string $version = 'latest', string $mysqlRootPassword = 'root'): self
|
||||||
{
|
{
|
||||||
return new self($version, $mysqlRootPassword);
|
return new self($version, $mysqlRootPassword);
|
||||||
|
|||||||
@@ -8,13 +8,17 @@ use Testcontainers\Wait\WaitForExec;
|
|||||||
|
|
||||||
class MySQLContainer extends GenericContainer
|
class MySQLContainer extends GenericContainer
|
||||||
{
|
{
|
||||||
private function __construct(string $version, string $mysqlRootPassword)
|
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
|
||||||
{
|
{
|
||||||
parent::__construct('mysql:' . $version);
|
parent::__construct('mysql:' . $version);
|
||||||
|
$this->withExposedPorts(3306);
|
||||||
$this->withEnvironment('MYSQL_ROOT_PASSWORD', $mysqlRootPassword);
|
$this->withEnvironment('MYSQL_ROOT_PASSWORD', $mysqlRootPassword);
|
||||||
$this->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use constructor instead
|
||||||
|
* Left for backward compatibility
|
||||||
|
*/
|
||||||
public static function make(string $version = 'latest', string $mysqlRootPassword = 'root'): self
|
public static function make(string $version = 'latest', string $mysqlRootPassword = 'root'): self
|
||||||
{
|
{
|
||||||
return new self($version, $mysqlRootPassword);
|
return new self($version, $mysqlRootPassword);
|
||||||
|
|||||||
@@ -8,23 +8,36 @@ use Testcontainers\Wait\WaitForHttp;
|
|||||||
|
|
||||||
class OpenSearchContainer extends GenericContainer
|
class OpenSearchContainer extends GenericContainer
|
||||||
{
|
{
|
||||||
private function __construct(string $version)
|
public function __construct(string $version = 'latest')
|
||||||
{
|
{
|
||||||
parent::__construct('opensearchproject/opensearch:' . $version);
|
parent::__construct('opensearchproject/opensearch:' . $version);
|
||||||
|
$this->withExposedPorts(9200);
|
||||||
$this->withEnvironment('discovery.type', 'single-node');
|
$this->withEnvironment('discovery.type', 'single-node');
|
||||||
$this->withEnvironment('OPENSEARCH_INITIAL_ADMIN_PASSWORD', 'c3o_ZPHo!');
|
$this->withEnvironment('OPENSEARCH_INITIAL_ADMIN_PASSWORD', 'c3o_ZPHo!');
|
||||||
$this->withWait(WaitForHttp::make(9200));
|
$this->withWait(WaitForHttp::make(9200));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use constructor instead
|
||||||
|
* Left for backward compatibility
|
||||||
|
*/
|
||||||
public static function make(string $version = 'latest'): self
|
public static function make(string $version = 'latest'): self
|
||||||
{
|
{
|
||||||
return new self($version);
|
return new self($version);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function disableSecurityPlugin(): self
|
public function withDisabledSecurityPlugin(): self
|
||||||
{
|
{
|
||||||
$this->withEnvironment('plugins.security.disabled', 'true');
|
$this->withEnvironment('plugins.security.disabled', 'true');
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use withDisabledSecurityPlugin instead
|
||||||
|
*/
|
||||||
|
public function disableSecurityPlugin(): self
|
||||||
|
{
|
||||||
|
return $this->withDisabledSecurityPlugin();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,17 @@ use Testcontainers\Wait\WaitForExec;
|
|||||||
|
|
||||||
class PostgresContainer extends GenericContainer
|
class PostgresContainer extends GenericContainer
|
||||||
{
|
{
|
||||||
private function __construct(string $version, string $rootPassword)
|
public function __construct(string $version = 'latest', string $rootPassword = 'root')
|
||||||
{
|
{
|
||||||
parent::__construct('postgres:' . $version);
|
parent::__construct('postgres:' . $version);
|
||||||
$this->withEnvironment('POSTGRES_PASSWORD', $rootPassword);
|
$this->withEnvironment('POSTGRES_PASSWORD', $rootPassword);
|
||||||
$this->withWait(new WaitForExec(["pg_isready", "-h", "127.0.0.1"]));
|
$this->withWait(new WaitForExec(["pg_isready", "-h", "127.0.0.1"]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use constructor instead
|
||||||
|
* Left for backward compatibility
|
||||||
|
*/
|
||||||
public static function make(string $version = 'latest', string $dbPassword = 'root'): self
|
public static function make(string $version = 'latest', string $dbPassword = 'root'): self
|
||||||
{
|
{
|
||||||
return new self($version, $dbPassword);
|
return new self($version, $dbPassword);
|
||||||
|
|||||||
@@ -8,12 +8,17 @@ use Testcontainers\Wait\WaitForLog;
|
|||||||
|
|
||||||
class RedisContainer extends GenericContainer
|
class RedisContainer extends GenericContainer
|
||||||
{
|
{
|
||||||
private function __construct(string $version)
|
public function __construct(string $version = 'latest')
|
||||||
{
|
{
|
||||||
parent::__construct('redis:' . $version);
|
parent::__construct('redis:' . $version);
|
||||||
$this->withWait(new WaitForLog('Ready to accept connections'));
|
$this->withExposedPorts(6379);
|
||||||
|
//$this->withWait(new WaitForLog('Ready to accept connections'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use constructor instead
|
||||||
|
* Left for backward compatibility
|
||||||
|
*/
|
||||||
public static function make(string $version = 'latest'): self
|
public static function make(string $version = 'latest'): self
|
||||||
{
|
{
|
||||||
return new self($version);
|
return new self($version);
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ class WaitForContainerRunning implements WaitInterface
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var_dump($containerStatus);
|
||||||
|
|
||||||
usleep($this->pollInterval * 1000);
|
usleep($this->pollInterval * 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use Closure;
|
|||||||
use Docker\API\Model\ContainersIdExecPostBody;
|
use Docker\API\Model\ContainersIdExecPostBody;
|
||||||
use Docker\API\Model\ExecIdStartPostBody;
|
use Docker\API\Model\ExecIdStartPostBody;
|
||||||
use Docker\Docker;
|
use Docker\Docker;
|
||||||
use Testcontainers\Exception\ContainerNotReadyException;
|
|
||||||
|
|
||||||
class WaitForExec implements WaitInterface
|
class WaitForExec implements WaitInterface
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,33 +4,30 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Testcontainers\Tests\Integration;
|
namespace Testcontainers\Tests\Integration;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use Predis\Client;
|
||||||
use Testcontainers\Container\GenericContainer;
|
use Testcontainers\Container\RedisContainer;
|
||||||
use Testcontainers\Registry;
|
|
||||||
|
|
||||||
class RedisContainerTest extends TestCase
|
class RedisContainerTest extends ContainerTestCase
|
||||||
{
|
{
|
||||||
// public static function tearDownAfterClass(): void
|
public static function setUpBeforeClass(): void
|
||||||
// {
|
{
|
||||||
// parent::tearDownAfterClass();
|
self::$container = (new RedisContainer())
|
||||||
//
|
->start();
|
||||||
// Registry::cleanup();
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
public function testRedisContainer(): void
|
public function testRedisContainer(): void
|
||||||
{
|
{
|
||||||
$redisContainer = (new GenericContainer('redis:alpine'))
|
$redisClient = new Client([
|
||||||
->withExposedPorts(6379)
|
|
||||||
->start();
|
|
||||||
|
|
||||||
$redisClient = new \Predis\Client([
|
|
||||||
'host' => 'localhost',
|
'host' => 'localhost',
|
||||||
'port' => 6379,
|
'port' => 6379,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$redisClient->ping();
|
||||||
|
|
||||||
|
$this->assertTrue($redisClient->isConnected());
|
||||||
|
|
||||||
$redisClient->set('greetings', 'Hello, World!');
|
$redisClient->set('greetings', 'Hello, World!');
|
||||||
|
|
||||||
$this->assertEquals('Hello, World!', $redisClient->get('greetings'));
|
$this->assertEquals('Hello, World!', $redisClient->get('greetings'));
|
||||||
$redisContainer->remove();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ use Predis\Connection\ConnectionException;
|
|||||||
use Symfony\Component\Process\Process;
|
use Symfony\Component\Process\Process;
|
||||||
use Testcontainers\Container\GenericContainer;
|
use Testcontainers\Container\GenericContainer;
|
||||||
use Testcontainers\Exception\ContainerNotReadyException;
|
use Testcontainers\Exception\ContainerNotReadyException;
|
||||||
use Testcontainers\Registry;
|
|
||||||
use Testcontainers\Trait\DockerContainerAwareTrait;
|
|
||||||
use Testcontainers\Wait\WaitForExec;
|
use Testcontainers\Wait\WaitForExec;
|
||||||
use Testcontainers\Wait\WaitForHealthCheck;
|
use Testcontainers\Wait\WaitForHealthCheck;
|
||||||
use Testcontainers\Wait\WaitForHttp;
|
use Testcontainers\Wait\WaitForHttp;
|
||||||
@@ -24,8 +22,6 @@ class WaitStrategyTest extends TestCase
|
|||||||
public static function tearDownAfterClass(): void
|
public static function tearDownAfterClass(): void
|
||||||
{
|
{
|
||||||
parent::tearDownAfterClass();
|
parent::tearDownAfterClass();
|
||||||
|
|
||||||
Registry::cleanup();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWaitForExec(): void
|
public function testWaitForExec(): void
|
||||||
|
|||||||
Reference in New Issue
Block a user