mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-08 17:29:29 +00:00
test improvements
This commit is contained in:
@@ -9,10 +9,11 @@ use Testcontainers\Container\StartedTestContainer;
|
||||
|
||||
abstract class ContainerTestCase extends TestCase
|
||||
{
|
||||
protected static StartedTestContainer $container;
|
||||
protected StartedTestContainer $container;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
self::$container->stop();
|
||||
$this->container->stop();
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,20 +4,110 @@ declare(strict_types=1);
|
||||
|
||||
namespace Testcontainers\Tests\Integration;
|
||||
|
||||
use Docker\API\Model\ContainersIdJsonGetResponse200;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Testcontainers\Container\GenericContainer;
|
||||
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
|
||||
use Testcontainers\Wait\WaitForHostPort;
|
||||
|
||||
class GenericContainerTest extends ContainerTestCase
|
||||
class GenericContainerTest extends TestCase
|
||||
{
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
self::$container = (new GenericContainer('alpine'))
|
||||
->withCommand(['tail', '-f', '/dev/null'])
|
||||
->start();
|
||||
}
|
||||
|
||||
public function testExec(): void
|
||||
{
|
||||
$actual = self::$container->exec(['echo', 'testcontainers']);
|
||||
self::assertSame('testcontainers', $actual);
|
||||
$container = (new GenericContainer('alpine'))
|
||||
->withCommand(['tail', '-f', '/dev/null'])
|
||||
->start();
|
||||
$result = $container->exec(['echo', 'testcontainers']);
|
||||
|
||||
self::assertSame('testcontainers', $result);
|
||||
|
||||
$container->stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testShouldReturnFirstMappedPort(): void
|
||||
{
|
||||
$container = (new GenericContainer('nginx'))
|
||||
->withPortGenerator(new FixedPortGenerator([8080]))
|
||||
->withExposedPorts(80)
|
||||
->withWait(new WaitForHostPort(8080))
|
||||
->start();
|
||||
$firstMappedPort = $container->getFirstMappedPort();
|
||||
|
||||
self::assertSame($firstMappedPort, 8080, 'First mapped port does not match 8080');
|
||||
|
||||
$container->stop();
|
||||
}
|
||||
|
||||
public function testShouldCaptureStderrWhenCommandFails(): void
|
||||
{
|
||||
$container = (new GenericContainer('alpine'))
|
||||
->withCommand(['tail', '-f', '/dev/null'])
|
||||
->start();
|
||||
$result = $container->exec(['ls', '/nonexistent/path']);
|
||||
|
||||
self::assertStringContainsString('No such file or directory', $result, 'Expected stderr in the output');
|
||||
|
||||
$container->stop();
|
||||
}
|
||||
|
||||
public function testShouldSetEnvironmentVariables(): void
|
||||
{
|
||||
$container = (new GenericContainer('alpine'))
|
||||
->withCommand(['tail', '-f', '/dev/null'])
|
||||
->withEnvironment(['TEST_ENV' => 'testValue'])
|
||||
->start();
|
||||
$output = $container->exec(['env']);
|
||||
|
||||
self::assertStringContainsString('TEST_ENV=testValue', $output);
|
||||
|
||||
$container->stop();
|
||||
}
|
||||
|
||||
public function testShouldSetEntrypoint(): void
|
||||
{
|
||||
$container = (new GenericContainer('cristianrgreco/testcontainer:1.1.14'))
|
||||
->withEntrypoint('node')
|
||||
->withCommand(['index.js'])
|
||||
->withExposedPorts(8080)
|
||||
->start();
|
||||
|
||||
/** @var ContainersIdJsonGetResponse200|null $inspectResult */
|
||||
$inspectResult = $container->getClient()->containerInspect($container->getId());
|
||||
$entrypoint = $inspectResult?->getConfig()?->getEntrypoint() ?? [];
|
||||
|
||||
self::assertContains('node', $entrypoint);
|
||||
|
||||
$container->stop();
|
||||
}
|
||||
|
||||
public function testShouldSetMount(): void
|
||||
{
|
||||
$localPath = __DIR__ . '/../Fixtures/Docker';
|
||||
$containerPath = '/mnt/test-data';
|
||||
|
||||
$container = (new GenericContainer('alpine'))
|
||||
->withMount($localPath, $containerPath)
|
||||
->withCommand(['tail', '-f', '/dev/null'])
|
||||
->start();
|
||||
|
||||
$result = $container->exec(["cat", $containerPath.'/test.txt']);
|
||||
self::assertSame('hello world', $result);
|
||||
}
|
||||
|
||||
public function testShouldSetPrivilegedMode(): void
|
||||
{
|
||||
$container = (new GenericContainer('alpine'))
|
||||
->withPrivilegedMode()
|
||||
->withCommand(['tail', '-f', '/dev/null'])
|
||||
->start();
|
||||
|
||||
/** @var ContainersIdJsonGetResponse200|null $inspectResult */
|
||||
$inspectResult = $container->getClient()->containerInspect($container->getId());
|
||||
$privileged = $inspectResult?->getHostConfig()?->getPrivileged();
|
||||
|
||||
self::assertTrue($privileged);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ use Testcontainers\Modules\MariaDBContainer;
|
||||
|
||||
class MariaDBContainerTest extends ContainerTestCase
|
||||
{
|
||||
public static function setUpBeforeClass(): void
|
||||
public function setUp(): void
|
||||
{
|
||||
self::$container = (new MariaDBContainer())
|
||||
$this->container = (new MariaDBContainer())
|
||||
->withMariaDBDatabase('foo')
|
||||
->withMariaDBUser('bar', 'baz')
|
||||
->start();
|
||||
@@ -21,8 +21,8 @@ class MariaDBContainerTest extends ContainerTestCase
|
||||
$pdo = new \PDO(
|
||||
sprintf(
|
||||
'mysql:host=%s;port=%d',
|
||||
self::$container->getHost(),
|
||||
self::$container->getFirstMappedPort()
|
||||
$this->container->getHost(),
|
||||
$this->container->getFirstMappedPort()
|
||||
),
|
||||
'bar',
|
||||
'baz',
|
||||
|
||||
@@ -8,9 +8,9 @@ use Testcontainers\Modules\MySQLContainer;
|
||||
|
||||
class MySQLContainerTest extends ContainerTestCase
|
||||
{
|
||||
public static function setUpBeforeClass(): void
|
||||
public function setUp(): void
|
||||
{
|
||||
self::$container = (new MySQLContainer())
|
||||
$this->container = (new MySQLContainer())
|
||||
->withMySQLDatabase('foo')
|
||||
->withMySQLUser('bar', 'baz')
|
||||
->start();
|
||||
@@ -21,8 +21,8 @@ class MySQLContainerTest extends ContainerTestCase
|
||||
$pdo = new \PDO(
|
||||
sprintf(
|
||||
'mysql:host=%s;port=%d',
|
||||
self::$container->getHost(),
|
||||
self::$container->getFirstMappedPort()
|
||||
$this->container->getHost(),
|
||||
$this->container->getFirstMappedPort()
|
||||
),
|
||||
'bar',
|
||||
'baz',
|
||||
|
||||
@@ -13,6 +13,7 @@ use Testcontainers\Container\PostgresContainer;
|
||||
use Testcontainers\Container\RedisContainer;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* Old test classes kept to check backward compatibility
|
||||
*/
|
||||
class ContainerTest extends TestCase
|
||||
|
||||
@@ -17,6 +17,7 @@ use Testcontainers\Wait\WaitForLog;
|
||||
use Testcontainers\Wait\WaitForTcpPortOpen;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* Old test classes kept to check backward compatibility
|
||||
*/
|
||||
class WaitStrategyTest extends TestCase
|
||||
|
||||
@@ -8,9 +8,9 @@ use Testcontainers\Modules\OpenSearchContainer;
|
||||
|
||||
class OpenSearchContainerTest extends ContainerTestCase
|
||||
{
|
||||
public static function setUpBeforeClass(): void
|
||||
public function setUp(): void
|
||||
{
|
||||
self::$container = (new OpenSearchContainer())
|
||||
$this->container = (new OpenSearchContainer())
|
||||
->withDisabledSecurityPlugin()
|
||||
->start();
|
||||
}
|
||||
@@ -23,8 +23,8 @@ class OpenSearchContainerTest extends ContainerTestCase
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, sprintf(
|
||||
'http://%s:%d',
|
||||
self::$container->getHost(),
|
||||
self::$container->getFirstMappedPort()
|
||||
$this->container->getHost(),
|
||||
$this->container->getFirstMappedPort()
|
||||
));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@ use Testcontainers\Modules\PostgresContainer;
|
||||
|
||||
class PostgreSQLContainerTest extends ContainerTestCase
|
||||
{
|
||||
public static function setUpBeforeClass(): void
|
||||
public function setUp(): void
|
||||
{
|
||||
self::$container = (new PostgresContainer())
|
||||
$this->container = (new PostgresContainer())
|
||||
->withPostgresUser('bar')
|
||||
->withPostgresDatabase('foo')
|
||||
->start();
|
||||
@@ -21,8 +21,8 @@ class PostgreSQLContainerTest extends ContainerTestCase
|
||||
$pdo = new \PDO(
|
||||
sprintf(
|
||||
'pgsql:host=%s;port=%d;dbname=foo',
|
||||
self::$container->getHost(),
|
||||
self::$container->getFirstMappedPort()
|
||||
$this->container->getHost(),
|
||||
$this->container->getFirstMappedPort()
|
||||
),
|
||||
'bar',
|
||||
'test',
|
||||
|
||||
@@ -9,17 +9,17 @@ use Testcontainers\Modules\RedisContainer;
|
||||
|
||||
class RedisContainerTest extends ContainerTestCase
|
||||
{
|
||||
public static function setUpBeforeClass(): void
|
||||
public function setUp(): void
|
||||
{
|
||||
self::$container = (new RedisContainer())
|
||||
$this->container = (new RedisContainer())
|
||||
->start();
|
||||
}
|
||||
|
||||
public function testRedisContainer(): void
|
||||
{
|
||||
$redisClient = new Client([
|
||||
'host' => self::$container->getHost(),
|
||||
'port' => self::$container->getFirstMappedPort(),
|
||||
'host' => $this->container->getHost(),
|
||||
'port' => $this->container->getFirstMappedPort(),
|
||||
]);
|
||||
|
||||
$redisClient->ping();
|
||||
|
||||
Reference in New Issue
Block a user