mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-08 17:29:29 +00:00
refactor Container to GenericContainer for better align to other Testcontainers language packages
This commit is contained in:
@@ -17,9 +17,9 @@ composer req --dev testcontainers/testcontainers
|
|||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Testcontainers\Container\Container;
|
use Testcontainers\Container\GenericContainer;
|
||||||
|
|
||||||
$container = Container::make('nginx:alpine');
|
$container = GenericContainer::make('nginx:alpine');
|
||||||
|
|
||||||
// set an environment variable
|
// set an environment variable
|
||||||
$container->withEnvironment('name', 'var');
|
$container->withEnvironment('name', 'var');
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ use Testcontainers\Wait\WaitInterface;
|
|||||||
* @phpstan-type ContainerInspect ContainerInspectSingleNetwork|ContainerInspectMultipleNetworks
|
* @phpstan-type ContainerInspect ContainerInspectSingleNetwork|ContainerInspectMultipleNetworks
|
||||||
* @phpstan-type DockerNetwork array{CreatedAt: string, Driver: string, ID: string, IPv6: string, Internal: string, Labels: string, Name: string, Scope: string}
|
* @phpstan-type DockerNetwork array{CreatedAt: string, Driver: string, ID: string, IPv6: string, Internal: string, Labels: string, Name: string, Scope: string}
|
||||||
*/
|
*/
|
||||||
class Container
|
class GenericContainer
|
||||||
{
|
{
|
||||||
protected Docker $dockerClient;
|
protected Docker $dockerClient;
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ class Container
|
|||||||
|
|
||||||
public static function make(string $image): self
|
public static function make(string $image): self
|
||||||
{
|
{
|
||||||
return new Container($image);
|
return new GenericContainer($image);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getId(): string
|
public function getId(): string
|
||||||
@@ -6,7 +6,7 @@ namespace Testcontainers\Container;
|
|||||||
|
|
||||||
use Testcontainers\Wait\WaitForExec;
|
use Testcontainers\Wait\WaitForExec;
|
||||||
|
|
||||||
class MariaDBContainer extends Container
|
class MariaDBContainer extends GenericContainer
|
||||||
{
|
{
|
||||||
private function __construct(string $version, string $mysqlRootPassword)
|
private function __construct(string $version, string $mysqlRootPassword)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Testcontainers\Container;
|
|||||||
|
|
||||||
use Testcontainers\Wait\WaitForExec;
|
use Testcontainers\Wait\WaitForExec;
|
||||||
|
|
||||||
class MySQLContainer extends Container
|
class MySQLContainer extends GenericContainer
|
||||||
{
|
{
|
||||||
private function __construct(string $version, string $mysqlRootPassword)
|
private function __construct(string $version, string $mysqlRootPassword)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Testcontainers\Container;
|
|||||||
|
|
||||||
use Testcontainers\Wait\WaitForHttp;
|
use Testcontainers\Wait\WaitForHttp;
|
||||||
|
|
||||||
class OpenSearchContainer extends Container
|
class OpenSearchContainer extends GenericContainer
|
||||||
{
|
{
|
||||||
private function __construct(string $version)
|
private function __construct(string $version)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Testcontainers\Container;
|
|||||||
|
|
||||||
use Testcontainers\Wait\WaitForExec;
|
use Testcontainers\Wait\WaitForExec;
|
||||||
|
|
||||||
class PostgresContainer extends Container
|
class PostgresContainer extends GenericContainer
|
||||||
{
|
{
|
||||||
private function __construct(string $version, string $rootPassword)
|
private function __construct(string $version, string $rootPassword)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Testcontainers\Container;
|
|||||||
|
|
||||||
use Testcontainers\Wait\WaitForLog;
|
use Testcontainers\Wait\WaitForLog;
|
||||||
|
|
||||||
class RedisContainer extends Container
|
class RedisContainer extends GenericContainer
|
||||||
{
|
{
|
||||||
private function __construct(string $version)
|
private function __construct(string $version)
|
||||||
{
|
{
|
||||||
|
|||||||
+4
-4
@@ -4,18 +4,18 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Testcontainers;
|
namespace Testcontainers;
|
||||||
|
|
||||||
use Testcontainers\Container\Container;
|
use Testcontainers\Container\GenericContainer;
|
||||||
|
|
||||||
class Registry
|
class Registry
|
||||||
{
|
{
|
||||||
private static bool $registeredCleanup = false;
|
private static bool $registeredCleanup = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array<int|string, Container>
|
* @var array<int|string, GenericContainer>
|
||||||
*/
|
*/
|
||||||
private static array $registry = [];
|
private static array $registry = [];
|
||||||
|
|
||||||
public static function add(Container $container): void
|
public static function add(GenericContainer $container): void
|
||||||
{
|
{
|
||||||
self::$registry[spl_object_id($container)] = $container;
|
self::$registry[spl_object_id($container)] = $container;
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ class Registry
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function remove(Container $container): void
|
public static function remove(GenericContainer $container): void
|
||||||
{
|
{
|
||||||
unset(self::$registry[spl_object_id($container)]);
|
unset(self::$registry[spl_object_id($container)]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ namespace Testcontainers\Trait;
|
|||||||
|
|
||||||
use JsonException;
|
use JsonException;
|
||||||
use Symfony\Component\Process\Process;
|
use Symfony\Component\Process\Process;
|
||||||
use Testcontainers\Container\Container;
|
use Testcontainers\Container\GenericContainer;
|
||||||
use UnexpectedValueException;
|
use UnexpectedValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @phpstan-import-type ContainerInspect from Container
|
* @phpstan-import-type ContainerInspect from GenericContainer
|
||||||
* @phpstan-import-type DockerNetwork from Container
|
* @phpstan-import-type DockerNetwork from GenericContainer
|
||||||
*/
|
*/
|
||||||
trait DockerContainerAwareTrait
|
trait DockerContainerAwareTrait
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use PHPUnit\Framework\TestCase;
|
|||||||
use Predis\Client;
|
use Predis\Client;
|
||||||
use Predis\Connection\ConnectionException;
|
use Predis\Connection\ConnectionException;
|
||||||
use Symfony\Component\Process\Process;
|
use Symfony\Component\Process\Process;
|
||||||
use Testcontainers\Container\Container;
|
use Testcontainers\Container\GenericContainer;
|
||||||
use Testcontainers\Exception\ContainerNotReadyException;
|
use Testcontainers\Exception\ContainerNotReadyException;
|
||||||
use Testcontainers\Registry;
|
use Testcontainers\Registry;
|
||||||
use Testcontainers\Trait\DockerContainerAwareTrait;
|
use Testcontainers\Trait\DockerContainerAwareTrait;
|
||||||
@@ -32,7 +32,7 @@ class WaitStrategyTest extends TestCase
|
|||||||
{
|
{
|
||||||
$called = false;
|
$called = false;
|
||||||
|
|
||||||
$container = Container::make('mysql')
|
$container = GenericContainer::make('mysql')
|
||||||
->withEnvironment('MYSQL_ROOT_PASSWORD', 'root')
|
->withEnvironment('MYSQL_ROOT_PASSWORD', 'root')
|
||||||
->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1'], function (Process $process) use (&$called) {
|
->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1'], function (Process $process) use (&$called) {
|
||||||
$called = true;
|
$called = true;
|
||||||
@@ -60,7 +60,7 @@ class WaitStrategyTest extends TestCase
|
|||||||
|
|
||||||
public function testWaitForLog(): void
|
public function testWaitForLog(): void
|
||||||
{
|
{
|
||||||
$container = Container::make('redis:6.2.5')
|
$container = GenericContainer::make('redis:6.2.5')
|
||||||
->withWait(new WaitForLog('Ready to accept connections'));
|
->withWait(new WaitForLog('Ready to accept connections'));
|
||||||
|
|
||||||
$container->run();
|
$container->run();
|
||||||
@@ -86,7 +86,7 @@ class WaitStrategyTest extends TestCase
|
|||||||
|
|
||||||
public function testWaitForHTTP(): void
|
public function testWaitForHTTP(): void
|
||||||
{
|
{
|
||||||
$container = Container::make('nginx:alpine')
|
$container = GenericContainer::make('nginx:alpine')
|
||||||
->withWait(WaitForHttp::make(80));
|
->withWait(WaitForHttp::make(80));
|
||||||
|
|
||||||
$container->run();
|
$container->run();
|
||||||
@@ -107,7 +107,7 @@ class WaitStrategyTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testWaitForTcpPortOpen(bool $wait): void
|
public function testWaitForTcpPortOpen(bool $wait): void
|
||||||
{
|
{
|
||||||
$container = Container::make('nginx:alpine');
|
$container = GenericContainer::make('nginx:alpine');
|
||||||
|
|
||||||
if ($wait) {
|
if ($wait) {
|
||||||
$container->withWait(WaitForTcpPortOpen::make(80));
|
$container->withWait(WaitForTcpPortOpen::make(80));
|
||||||
@@ -140,7 +140,7 @@ class WaitStrategyTest extends TestCase
|
|||||||
|
|
||||||
public function testWaitForHealthCheck(): void
|
public function testWaitForHealthCheck(): void
|
||||||
{
|
{
|
||||||
$container = Container::make('nginx')
|
$container = GenericContainer::make('nginx')
|
||||||
->withHealthCheckCommand('curl --fail http://localhost')
|
->withHealthCheckCommand('curl --fail http://localhost')
|
||||||
->withWait(new WaitForHealthCheck());
|
->withWait(new WaitForHealthCheck());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user