test improvements

This commit is contained in:
Sergei Shitikov
2025-01-01 23:29:57 +01:00
parent 774743a7ce
commit dfff7ef282
11 changed files with 129 additions and 34 deletions
+3 -2
View File
@@ -23,7 +23,7 @@
"ext-pdo_mysql": "*",
"ext-pdo_pgsql": "*",
"phpunit/phpunit": "^9.5",
"brianium/paratest": "^6.6",
"brianium/paratest": "^6.11",
"friendsofphp/php-cs-fixer": "^3.12",
"phpstan/phpstan": "^1.8",
"phpstan/phpstan-phpunit": "^1.1",
@@ -41,7 +41,8 @@
}
},
"scripts": {
"integration": "paratest tests/ --bootstrap vendor/autoload.php -f",
"integration": "paratest tests/ --exclude-group=legacy --bootstrap vendor/autoload.php -f",
"integration:old": "phpunit tests/Integration/OldTests --bootstrap vendor/autoload.php",
"cs": "php-cs-fixer fix --dry-run",
"cs:fix": "php-cs-fixer fix",
"phpstan": "phpstan analyse"
+1
View File
@@ -0,0 +1 @@
hello world
+3 -2
View File
@@ -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();
}
}
+100 -10
View File
@@ -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);
}
}
+4 -4
View File
@@ -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',
+4 -4
View File
@@ -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',
+4 -4
View File
@@ -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();