72 lines
2.8 KiB
PHP
72 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\EcommerceFinance;
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Handler\MockHandler;
|
|
use GuzzleHttp\HandlerStack;
|
|
use GuzzleHttp\Middleware;
|
|
use GuzzleHttp\Psr7\Response;
|
|
use Modules\EcommerceFinance\Services\ClickHouseClient;
|
|
use RuntimeException;
|
|
use Tests\TestCase;
|
|
|
|
class ClickHouseClientTest extends TestCase
|
|
{
|
|
public function test_http_client_uses_parameters_and_json_each_row_without_putting_credentials_in_sql(): void
|
|
{
|
|
$history = [];
|
|
$handler = HandlerStack::create(new MockHandler([
|
|
new Response(200, [], "Ok.\n"),
|
|
new Response(200, [], "{\"value\":1}\n{\"value\":2}\n"),
|
|
new Response(200, [], ''),
|
|
new Response(200, [], 'PAR1'),
|
|
]));
|
|
$handler->push(Middleware::history($history));
|
|
$http = new Client(['handler' => $handler]);
|
|
$client = new ClickHouseClient($http, $this->configuration());
|
|
|
|
$this->assertTrue($client->ping());
|
|
$this->assertSame([['value' => 1], ['value' => 2]], $client->rows(
|
|
'SELECT value FROM numbers(2) WHERE value >= {minimum:UInt8}',
|
|
['minimum' => 0],
|
|
));
|
|
$client->insertJsonEachRow('fact_rows', ['id', 'name'], [
|
|
['id' => 1, 'name' => '测试'],
|
|
]);
|
|
|
|
parse_str($history[1]['request']->getUri()->getQuery(), $query);
|
|
$this->assertSame('0', (string) $query['param_minimum']);
|
|
$this->assertStringContainsString('{minimum:UInt8}', $query['query']);
|
|
$this->assertStringNotContainsString('fixture-password', $query['query']);
|
|
parse_str($history[2]['request']->getUri()->getQuery(), $insertQuery);
|
|
$this->assertStringContainsString('FORMAT JSONEachRow', $insertQuery['query']);
|
|
$this->assertSame("{\"id\":1,\"name\":\"测试\"}\n", (string) $history[2]['request']->getBody());
|
|
$this->assertSame('PAR1', (string) $client->stream('SELECT 1', 'Parquet'));
|
|
$this->assertSame(1800.0, $history[3]['options']['timeout']);
|
|
}
|
|
|
|
public function test_invalid_identifier_is_rejected_before_a_query_is_sent(): void
|
|
{
|
|
$client = new ClickHouseClient(new Client(['handler' => HandlerStack::create(new MockHandler())]), $this->configuration());
|
|
|
|
$this->expectException(RuntimeException::class);
|
|
$this->expectExceptionMessage('clickhouse_invalid_identifier');
|
|
$client->qualifiedTable('fact_rows; DROP TABLE fact_rows');
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function configuration(): array
|
|
{
|
|
return [
|
|
'enabled' => true,
|
|
'url' => 'http://clickhouse:8123',
|
|
'database' => 'analytics',
|
|
'username' => 'fixture-user',
|
|
'password' => 'fixture-password',
|
|
'connect_timeout_seconds' => 1,
|
|
'query_timeout_seconds' => 1,
|
|
];
|
|
}
|
|
}
|