Skip to content

Commit

Permalink
fix: phpunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Jul 13, 2024
1 parent 1b15815 commit 3ddc75e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
.php-cs-fixer.cache
.php-cs-fixer.cache
/composer.lock
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions src/Container/OpenSearchContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
9 changes: 6 additions & 3 deletions tests/Integration/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
29 changes: 13 additions & 16 deletions tests/Integration/WaitStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,16 +53,19 @@ 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');

$this->assertEquals('bar', $redis->get('foo'));

$container->stop();

$this->expectException(\RedisException::class);
$this->expectException(ConnectionException::class);

$redis->get('foo');

Expand All @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 3ddc75e

Please sign in to comment.