diff --git a/src/Client/Http/RequestFactory.php b/src/Client/Http/RequestFactory.php index 5e3b000..820c018 100644 --- a/src/Client/Http/RequestFactory.php +++ b/src/Client/Http/RequestFactory.php @@ -82,7 +82,11 @@ static function (array $acc, string|int $k) use ($matches) { $streamElements = [['name' => 'query', 'contents' => $requestOptions->sql]]; foreach ($requestOptions->params as $name => $value) { - $type = $typeToParam[$name]; + $type = $typeToParam[$name] ?? null; + if ($type === null) { + continue; + } + $streamElements[] = [ 'name' => 'param_' . $name, 'contents' => $this->paramValueConverterRegistry->get($type)($value, $type, false), diff --git a/src/Exception/UnsupportedType.php b/src/Exception/UnsupportedType.php index fdcf295..0f00963 100644 --- a/src/Exception/UnsupportedType.php +++ b/src/Exception/UnsupportedType.php @@ -13,4 +13,9 @@ public static function fromType(Type $type): self { return new self($type->name); } + + public static function fromString(string $type): self + { + return new self($type); + } } diff --git a/tests/Client/InsertTest.php b/tests/Client/InsertTest.php index 3e3aff7..20a9a3a 100644 --- a/tests/Client/InsertTest.php +++ b/tests/Client/InsertTest.php @@ -33,11 +33,11 @@ public function testInsert(string $tableSql): void ['PageViews' => 6, 'UserID' => 4324182021466249494, 'Duration' => 185, 'Sign' => 1], ]; - $this->client->executeQuery($tableSql); + self::$client->executeQuery($tableSql); - $this->client->insert('UserActivity', $data); + self::$client->insert('UserActivity', $data); - $output = $this->client->select( + $output = self::$client->select( <<<'CLICKHOUSE' SELECT * FROM UserActivity CLICKHOUSE, @@ -57,9 +57,9 @@ public function testInsertUseColumns(string $tableSql): void ['PageViews' => 6, 'UserID' => '4324182021466249494', 'Duration' => 185, 'Sign' => 1], ]; - $this->client->executeQuery($tableSql); + self::$client->executeQuery($tableSql); - $this->client->insert( + self::$client->insert( 'UserActivity', [ [5, 4324182021466249494, 146, -1], @@ -68,7 +68,7 @@ public function testInsertUseColumns(string $tableSql): void ['PageViews', 'UserID', 'Duration', 'Sign'], ); - $output = $this->client->select( + $output = self::$client->select( <<<'CLICKHOUSE' SELECT * FROM UserActivity CLICKHOUSE, @@ -80,7 +80,7 @@ public function testInsertUseColumns(string $tableSql): void public function testInsertEscaping(): void { - $this->client->executeQuery( + self::$client->executeQuery( <<<'CLICKHOUSE' CREATE TABLE a ( b Nullable(String) @@ -94,9 +94,9 @@ public function testInsertEscaping(): void ["\t"], ]; - $this->client->insert('a', $expectedData); + self::$client->insert('a', $expectedData); - $output = $this->client->select( + $output = self::$client->select( <<<'CLICKHOUSE' SELECT * FROM a CLICKHOUSE, @@ -124,7 +124,7 @@ public static function providerInsert(): iterable public function testInsertWithFormat(): void { - $this->client->executeQuery( + self::$client->executeQuery( <<<'CLICKHOUSE' CREATE TABLE UserActivity ( PageViews UInt32, @@ -136,7 +136,7 @@ public function testInsertWithFormat(): void CLICKHOUSE, ); - $this->client->insertWithFormat( + self::$client->insertWithFormat( 'UserActivity', new JsonEachRow(), <<<'JSONEACHROW' @@ -145,7 +145,7 @@ public function testInsertWithFormat(): void JSONEACHROW, ); - $output = $this->client->select( + $output = self::$client->select( <<<'CLICKHOUSE' SELECT * FROM UserActivity CLICKHOUSE @@ -166,14 +166,14 @@ public function testInsertEmptyValuesThrowsException(): void { $this->expectException(CannotInsert::class); - $this->client->insert('table', []); + self::$client->insert('table', []); } public function testInsertToNonExistentTableExpectServerError(): void { $this->expectException(ServerError::class); - $this->client->insert('table', [[1]]); + self::$client->insert('table', [[1]]); } public function testInsertWithWrongColumns(): void @@ -188,12 +188,12 @@ public function testInsertWithWrongColumns(): void ENGINE Memory CLICKHOUSE; - $this->client->executeQuery($tableSql); + self::$client->executeQuery($tableSql); $this->expectException(ServerError::class); $this->expectExceptionMessage('SYNTAX_ERROR'); - $this->client->insert( + self::$client->insert( 'UserActivity', [ [5], diff --git a/tests/Client/SelectAsyncTest.php b/tests/Client/SelectAsyncTest.php index af24f40..309eff9 100644 --- a/tests/Client/SelectAsyncTest.php +++ b/tests/Client/SelectAsyncTest.php @@ -25,7 +25,7 @@ final class SelectAsyncTest extends TestCaseBase public function testAsyncSelect(): void { - $client = $this->asyncClient; + $client = self::$asyncClient; $sql = <<<'CLICKHOUSE' SELECT number FROM system.numbers LIMIT 2 @@ -56,6 +56,6 @@ public function testSelectFromNonExistentTableExpectServerError(): void { $this->expectException(ServerError::class); - $this->asyncClient->select('table', new TabSeparated())->wait(); + self::$asyncClient->select('table', new TabSeparated())->wait(); } } diff --git a/tests/Client/SelectTest.php b/tests/Client/SelectTest.php index e0bea1e..7de844a 100644 --- a/tests/Client/SelectTest.php +++ b/tests/Client/SelectTest.php @@ -34,7 +34,7 @@ final class SelectTest extends TestCaseBase public function testSelectWithParams(): void { - $client = $this->client; + $client = self::$client; $output = $client->selectWithParams('SELECT {p1:UInt8} AS data', ['p1' => 3], new TabSeparated()); self::assertSame("3\n", $output->contents); @@ -43,7 +43,7 @@ public function testSelectWithParams(): void #[DataProvider('providerJson')] public function testJson(mixed $expectedData, string $sql): void { - $client = $this->client; + $client = self::$client; $output = $client->select($sql, new Json()); self::assertSame($expectedData, $output->data); @@ -82,7 +82,7 @@ public static function providerJson(): iterable #[DataProvider('providerJsonCompact')] public function testJsonCompact(mixed $expectedData, string $sql): void { - $client = $this->client; + $client = self::$client; $output = $client->select($sql, new JsonCompact()); self::assertSame($expectedData, $output->data); @@ -121,7 +121,7 @@ public static function providerJsonCompact(): iterable #[DataProvider('providerJsonEachRow')] public function testJsonEachRow(mixed $expectedData, string $sql): void { - $client = $this->client; + $client = self::$client; $output = $client->select($sql, new JsonEachRow()); self::assertSame($expectedData, $output->data); @@ -159,7 +159,7 @@ public static function providerJsonEachRow(): iterable public function testNull(): void { - $client = $this->client; + $client = self::$client; $client->select('SELECT 1', new Null_()); self::assertTrue(true); @@ -170,6 +170,6 @@ public function testSettingsArePassed(): void self::expectException(ServerError::class); $this->expectExceptionMessageMatches("~DB::Exception: Database `non-existent` (doesn't|does not) exist~"); - $this->client->select('SELECT 1', new JsonCompact(), ['database' => 'non-existent']); + self::$client->select('SELECT 1', new JsonCompact(), ['database' => 'non-existent']); } } diff --git a/tests/Param/ParamValueConverterTest.php b/tests/Param/ParamValueConverterRegistryTest.php similarity index 91% rename from tests/Param/ParamValueConverterTest.php rename to tests/Param/ParamValueConverterRegistryTest.php index c2ce2cd..3f4682b 100644 --- a/tests/Param/ParamValueConverterTest.php +++ b/tests/Param/ParamValueConverterRegistryTest.php @@ -10,12 +10,12 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use Psr\Http\Client\ClientExceptionInterface; -use ReflectionFunction; use SimPod\ClickHouseClient\Exception\ServerError; use SimPod\ClickHouseClient\Format\JsonEachRow; use SimPod\ClickHouseClient\Format\TabSeparated; use SimPod\ClickHouseClient\Param\ParamValueConverterRegistry; use SimPod\ClickHouseClient\Sql\Type; +use SimPod\ClickHouseClient\Tests\ClickHouseVersion; use SimPod\ClickHouseClient\Tests\TestCaseBase; use SimPod\ClickHouseClient\Tests\WithClient; @@ -27,7 +27,7 @@ use function trim; #[CoversClass(ParamValueConverterRegistry::class)] -final class ParamValueConverterTest extends TestCaseBase +final class ParamValueConverterRegistryTest extends TestCaseBase { use WithClient; @@ -76,22 +76,20 @@ public function testAllTypesAreCovered(): void #[DataProvider('providerConvert')] public function testConvert(string $type, mixed $value, mixed $expected): void { - $registry = new ParamValueConverterRegistry(); - - $converter = $registry->get(Type::fromString($type)); - $reflection = new ReflectionFunction($converter); -// $argTypeName = $reflection->getParameters()[0]->getType()->getName(); - - self::assertSame( - $expected, - trim( - self::$client->selectWithParams( - sprintf('SELECT {p1:%s}', $type), - ['p1' => $value], - new TabSeparated(), - )->contents, - ), - ); + if (ClickHouseVersion::get() < 2307) { + self::markTestSkipped(); + } + + self::assertSame( + $expected, + trim( + self::$client->selectWithParams( + sprintf('SELECT {p1:%s}', $type), + ['p1' => $value], + new TabSeparated(), + )->contents, + ), + ); } /** @return Generator */ diff --git a/tests/Snippet/CurrentDatabaseTest.php b/tests/Snippet/CurrentDatabaseTest.php index 417959e..c9aac05 100644 --- a/tests/Snippet/CurrentDatabaseTest.php +++ b/tests/Snippet/CurrentDatabaseTest.php @@ -17,8 +17,8 @@ final class CurrentDatabaseTest extends TestCaseBase public function testRun(): void { self::assertSame( - $this->currentDbName, - CurrentDatabase::run($this->client), + self::$currentDbName, + CurrentDatabase::run(self::$client), ); } } diff --git a/tests/Snippet/DatabaseSizeTest.php b/tests/Snippet/DatabaseSizeTest.php index 1bc1e66..f779a22 100644 --- a/tests/Snippet/DatabaseSizeTest.php +++ b/tests/Snippet/DatabaseSizeTest.php @@ -18,7 +18,7 @@ final class DatabaseSizeTest extends TestCaseBase public function setUp(): void { - $this->client->executeQuery( + self::$client->executeQuery( <<<'CLICKHOUSE' CREATE TABLE test ( a_date DateTime, @@ -33,9 +33,9 @@ public function setUp(): void public function testRun(): void { - self::assertSame(0, DatabaseSize::run($this->client)); + self::assertSame(0, DatabaseSize::run(self::$client)); - $this->client->insert('test', [[new DateTimeImmutable('2020-08-01 00:11:22'), 1]]); + self::$client->insert('test', [[new DateTimeImmutable('2020-08-01 00:11:22'), 1]]); if (ClickHouseVersion::get() >= 2307) { $expectedSize = 316; @@ -45,11 +45,11 @@ public function testRun(): void $expectedSize = 150; } - self::assertSame($expectedSize, DatabaseSize::run($this->client)); + self::assertSame($expectedSize, DatabaseSize::run(self::$client)); } public function tearDown(): void { - $this->client->executeQuery('DROP TABLE test'); + self::$client->executeQuery('DROP TABLE test'); } } diff --git a/tests/Snippet/PartsTest.php b/tests/Snippet/PartsTest.php index 24bbb0b..0232430 100644 --- a/tests/Snippet/PartsTest.php +++ b/tests/Snippet/PartsTest.php @@ -16,6 +16,6 @@ final class PartsTest extends TestCaseBase public function testRun(): void { - self::assertSame([], Parts::run($this->client, 'system.query_log')); + self::assertSame([], Parts::run(self::$client, 'system.query_log')); } } diff --git a/tests/Snippet/ShowCreateTableTest.php b/tests/Snippet/ShowCreateTableTest.php index f44eb6c..47273dc 100644 --- a/tests/Snippet/ShowCreateTableTest.php +++ b/tests/Snippet/ShowCreateTableTest.php @@ -21,14 +21,14 @@ final class ShowCreateTableTest extends TestCaseBase public function testRun(): void { - $dbName = $this->currentDbName; + $dbName = self::$currentDbName; $sql = <<client->executeQuery($sql); + self::$client->executeQuery($sql); - $createTableSql = ShowCreateTable::run($this->client, 'test'); + $createTableSql = ShowCreateTable::run(self::$client, 'test'); // BC $replaced = preg_replace( diff --git a/tests/Snippet/ShowDatabasesTest.php b/tests/Snippet/ShowDatabasesTest.php index 9456245..04b59a6 100644 --- a/tests/Snippet/ShowDatabasesTest.php +++ b/tests/Snippet/ShowDatabasesTest.php @@ -23,13 +23,13 @@ final class ShowDatabasesTest extends TestCaseBase public function testRun(): void { - $databases = ShowDatabases::run($this->client); + $databases = ShowDatabases::run(self::$client); self::assertGreaterThan(2, count($databases)); // Default, system, at least one test database $databases = array_filter( $databases, - fn (string $database): bool => ! str_starts_with($database, 'clickhouse_client_test__') - || $database === $this->currentDbName + static fn (string $database): bool => ! str_starts_with($database, 'clickhouse_client_test__') + || $database === self::$currentDbName ); $databases = array_values($databases); @@ -42,13 +42,13 @@ public function testRun(): void $expected = ClickHouseVersion::get() >= 2111 ? [ 'INFORMATION_SCHEMA', - $this->currentDbName, + self::$currentDbName, 'default', 'information_schema', 'system', ] : [ - $this->currentDbName, + self::$currentDbName, 'default', 'system', ]; diff --git a/tests/Snippet/TableSizesTest.php b/tests/Snippet/TableSizesTest.php index 6e7e92b..16214c4 100644 --- a/tests/Snippet/TableSizesTest.php +++ b/tests/Snippet/TableSizesTest.php @@ -17,7 +17,7 @@ final class TableSizesTest extends TestCaseBase public function setUp(): void { - $this->client->executeQuery( + self::$client->executeQuery( <<<'CLICKHOUSE' CREATE TABLE test ( a_date DateTime, @@ -32,18 +32,18 @@ public function setUp(): void public function testRun(): void { - $this->client->insert('test', [[new DateTimeImmutable(), 1]]); + self::$client->insert('test', [[new DateTimeImmutable(), 1]]); - self::assertCount(1, TableSizes::run($this->client)); + self::assertCount(1, TableSizes::run(self::$client)); } public function testRunOnNonexistentDatabase(): void { - self::assertSame([], TableSizes::run($this->client, 'does not exist')); + self::assertSame([], TableSizes::run(self::$client, 'does not exist')); } public function tearDown(): void { - $this->client->executeQuery('DROP TABLE test'); + self::$client->executeQuery('DROP TABLE test'); } } diff --git a/tests/Snippet/VersionTest.php b/tests/Snippet/VersionTest.php index 205f870..f661c4a 100644 --- a/tests/Snippet/VersionTest.php +++ b/tests/Snippet/VersionTest.php @@ -16,6 +16,6 @@ final class VersionTest extends TestCaseBase public function testRun(): void { - self::assertMatchesRegularExpression('~(\d+\.)+\d+~', Version::run($this->client)); + self::assertMatchesRegularExpression('~(\d+\.)+\d+~', Version::run(self::$client)); } }