From 5ee4fdc80435a620093f6dcbb6a90c5c80da0962 Mon Sep 17 00:00:00 2001 From: Sergei Shitikov Date: Sun, 1 Sep 2024 14:45:04 +0200 Subject: [PATCH] some reordering and test optimizations to align new API. --- .../ContainerRuntimeClient.php | 45 +++++++++++++++++++ tests/Integration/ContainerTestCase.php | 18 ++++++++ tests/Integration/MariaDBContainerTest.php | 35 +++++++++++++++ tests/Integration/MySQLContainerTest.php | 35 +++++++++++++++ tests/Integration/OpenSearchContainerTest.php | 38 ++++++++++++++++ tests/Integration/PostgreSQLContainerTest.php | 35 +++++++++++++++ 6 files changed, 206 insertions(+) create mode 100644 src/ContainerRuntime/ContainerRuntimeClient.php create mode 100644 tests/Integration/ContainerTestCase.php create mode 100644 tests/Integration/MariaDBContainerTest.php create mode 100644 tests/Integration/MySQLContainerTest.php create mode 100644 tests/Integration/OpenSearchContainerTest.php create mode 100644 tests/Integration/PostgreSQLContainerTest.php diff --git a/src/ContainerRuntime/ContainerRuntimeClient.php b/src/ContainerRuntime/ContainerRuntimeClient.php new file mode 100644 index 0000000..5728217 --- /dev/null +++ b/src/ContainerRuntime/ContainerRuntimeClient.php @@ -0,0 +1,45 @@ +remove(); + } +} diff --git a/tests/Integration/MariaDBContainerTest.php b/tests/Integration/MariaDBContainerTest.php new file mode 100644 index 0000000..f0629a7 --- /dev/null +++ b/tests/Integration/MariaDBContainerTest.php @@ -0,0 +1,35 @@ +withMariaDBDatabase('foo') + ->withMariaDBUser('bar', 'baz') + ->start(); + } + + public function testMySQLContainer(): void + { + $pdo = new \PDO( + sprintf('mysql:host=%s;port=3306', self::$container->getAddress()), + 'bar', + 'baz', + ); + + $query = $pdo->query('SHOW databases'); + + $this->assertInstanceOf(\PDOStatement::class, $query); + + $databases = $query->fetchAll(\PDO::FETCH_COLUMN); + + $this->assertContains('foo', $databases); + } +} diff --git a/tests/Integration/MySQLContainerTest.php b/tests/Integration/MySQLContainerTest.php new file mode 100644 index 0000000..d625a90 --- /dev/null +++ b/tests/Integration/MySQLContainerTest.php @@ -0,0 +1,35 @@ +withMySQLDatabase('foo') + ->withMySQLUser('bar', 'baz') + ->start(); + } + + public function testMySQLContainer(): void + { + $pdo = new \PDO( + sprintf('mysql:host=%s;port=3306', self::$container->getAddress()), + 'bar', + 'baz', + ); + + $query = $pdo->query('SHOW databases'); + + $this->assertInstanceOf(\PDOStatement::class, $query); + + $databases = $query->fetchAll(\PDO::FETCH_COLUMN); + + $this->assertContains('foo', $databases); + } +} diff --git a/tests/Integration/OpenSearchContainerTest.php b/tests/Integration/OpenSearchContainerTest.php new file mode 100644 index 0000000..92fd1cb --- /dev/null +++ b/tests/Integration/OpenSearchContainerTest.php @@ -0,0 +1,38 @@ +withDisabledSecurityPlugin() + ->start(); + } + + /** + * @throws \JsonException + */ + public function testOpenSearch(): void + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, sprintf('http://%s:%d', self::$container->getAddress(), 9200)); + 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, JSON_THROW_ON_ERROR, JSON_THROW_ON_ERROR); + + $this->assertArrayHasKey('cluster_name', $data); + + $this->assertEquals('docker-cluster', $data['cluster_name']); + } +} diff --git a/tests/Integration/PostgreSQLContainerTest.php b/tests/Integration/PostgreSQLContainerTest.php new file mode 100644 index 0000000..7691435 --- /dev/null +++ b/tests/Integration/PostgreSQLContainerTest.php @@ -0,0 +1,35 @@ +withPostgresUser('test') + ->withPostgresDatabase('foo') + ->start(); + } + + public function testPostgreSQLContainer(): void + { + $pdo = new \PDO( + sprintf('pgsql:host=%s;port=5432;dbname=foo', self::$container->getAddress()), + 'test', + 'test', + ); + + $query = $pdo->query('SELECT datname FROM pg_database'); + + $this->assertInstanceOf(\PDOStatement::class, $query); + + $databases = $query->fetchAll(\PDO::FETCH_COLUMN); + + $this->assertContains('foo', $databases); + } +}