From 3ddc75e717493c714a338aa035dcec2a64df8902 Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Sat, 13 Jul 2024 11:21:19 +0900 Subject: [PATCH] fix: phpunit tests --- .gitignore | 3 ++- composer.json | 3 ++- src/Container/OpenSearchContainer.php | 1 + tests/Integration/ContainerTest.php | 9 +++++--- tests/Integration/WaitStrategyTest.php | 29 ++++++++++++-------------- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 0970f44..adfa2e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ -.php-cs-fixer.cache \ No newline at end of file +.php-cs-fixer.cache +/composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json index c0e7861..e3d909d 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "friendsofphp/php-cs-fixer": "^3.12", "phpstan/phpstan": "^1.8", "phpstan/phpstan-phpunit": "^1.1", - "phpstan/extension-installer": "^1.2" + "phpstan/extension-installer": "^1.2", + "predis/predis": "^3.0 || ^2.0" }, "autoload": { "psr-4": { diff --git a/src/Container/OpenSearchContainer.php b/src/Container/OpenSearchContainer.php index 87c7f3a..e122264 100644 --- a/src/Container/OpenSearchContainer.php +++ b/src/Container/OpenSearchContainer.php @@ -12,6 +12,7 @@ private function __construct(string $version) { parent::__construct('opensearchproject/opensearch:' . $version); $this->withEnvironment('discovery.type', 'single-node'); + $this->withEnvironment('OPENSEARCH_INITIAL_ADMIN_PASSWORD', 'c3o_ZPHo!'); $this->withWait(WaitForHttp::make(9200)); } diff --git a/tests/Integration/ContainerTest.php b/tests/Integration/ContainerTest.php index c7cd88a..b673c07 100644 --- a/tests/Integration/ContainerTest.php +++ b/tests/Integration/ContainerTest.php @@ -5,7 +5,7 @@ namespace Testcontainer\Tests\Integration; use PHPUnit\Framework\TestCase; -use Redis; +use Predis\Client; use Testcontainer\Container\MariaDBContainer; use Testcontainer\Container\MySQLContainer; use Testcontainer\Container\OpenSearchContainer; @@ -66,8 +66,11 @@ public function testRedis(): void $container->run(); - $redis = new Redis(); - $redis->connect($container->getAddress(), 6379, 0.001); + $redis = new Client([ + 'scheme' => 'tcp', + 'host' => $container->getAddress(), + 'port' => 6379, + ]); $redis->ping(); diff --git a/tests/Integration/WaitStrategyTest.php b/tests/Integration/WaitStrategyTest.php index f7a814a..2424196 100644 --- a/tests/Integration/WaitStrategyTest.php +++ b/tests/Integration/WaitStrategyTest.php @@ -5,6 +5,8 @@ namespace Testcontainer\Tests\Integration; use PHPUnit\Framework\TestCase; +use Predis\Client; +use Predis\Connection\ConnectionException; use Symfony\Component\Process\Process; use Testcontainer\Container\Container; use Testcontainer\Wait\WaitForExec; @@ -51,8 +53,11 @@ public function testWaitForLog(): void $container->run(); - $redis = new \Redis(); - $redis->connect($container->getAddress(), 6379, 0.001); + $redis = new Client([ + 'scheme' => 'tcp', + 'host' => $container->getAddress(), + 'port' => 6379, + ]); $redis->set('foo', 'bar'); @@ -60,7 +65,7 @@ public function testWaitForLog(): void $container->stop(); - $this->expectException(\RedisException::class); + $this->expectException(ConnectionException::class); $redis->get('foo'); @@ -69,27 +74,20 @@ public function testWaitForLog(): void public function testWaitForHTTP(): void { - $container = Container::make('opensearchproject/opensearch') - ->withEnvironment('discovery.type', 'single-node') - ->withEnvironment('plugins.security.disabled', 'true') - ->withWait(WaitForHttp::make(9200)); + $container = Container::make('nginx:alpine') + ->withWait(WaitForHttp::make(80)); $container->run(); $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, sprintf('http://%s:%d', $container->getAddress(), 9200)); + curl_setopt($ch, CURLOPT_URL, sprintf('http://%s:%d', $container->getAddress(), 80)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = (string) curl_exec($ch); - $this->assertNotEmpty($response); - - /** @var array{cluster_name: string} $data */ - $data = json_decode($response, true); + curl_close($ch); - $this->assertArrayHasKey('cluster_name', $data); - - $this->assertEquals('docker-cluster', $data['cluster_name']); + $this->assertNotEmpty($response); } public function testWaitForHealthCheck(): void @@ -103,7 +101,6 @@ public function testWaitForHealthCheck(): void $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, sprintf('http://%s:%d', $container->getAddress(), 80)); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch);