From 0d9e02013eb118a14e8cb7fa64f98a721cd7e61a Mon Sep 17 00:00:00 2001 From: esazykin Date: Mon, 27 Nov 2017 11:18:29 +0300 Subject: [PATCH 1/9] more fast and readable --- src/Query/Mapper/UnnamedMapper.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Query/Mapper/UnnamedMapper.php b/src/Query/Mapper/UnnamedMapper.php index 8e2c49e..5840573 100644 --- a/src/Query/Mapper/UnnamedMapper.php +++ b/src/Query/Mapper/UnnamedMapper.php @@ -18,19 +18,19 @@ class UnnamedMapper extends AbstractMapper implements QueryMapperInterface * Binds values to query. * * @param string $query - * @param array $bindings + * @param array $bindings * * @return string + * @throws QueryMapperException */ public function bind(string $query, array $bindings): string { $this->checkBindings($query, $bindings); $escapedBindings = $this->escapeBindings($bindings); - $query = str_replace('%', '%%', $query); - $query = str_replace('?', '%s', $query); + $query = str_replace(['%', '?'], ['%%', '%s'], $query); - $query = call_user_func_array('sprintf', array_merge([$query], $escapedBindings)); + $query = sprintf(...array_merge([$query], $escapedBindings)); return $query; } From e6461445f03303c71d798ea6fe52a2e324ea8372 Mon Sep 17 00:00:00 2001 From: esazykin Date: Mon, 27 Nov 2017 11:19:18 +0300 Subject: [PATCH 2/9] ignore composer.lock --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 297f9d4..5dcfd3e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor /.idea -/tests/coverage \ No newline at end of file +/tests/coverage +composer.lock From a15dc7196eb40c7671e177d6d7d553b2cf1699ba Mon Sep 17 00:00:00 2001 From: esazykin Date: Mon, 27 Nov 2017 11:23:30 +0300 Subject: [PATCH 3/9] review tests --- tests/ClientExceptionsTest.php | 12 +++--- tests/ClientTest.php | 22 +++++----- tests/ClusterExceptionsTest.php | 8 ++-- tests/ClusterTest.php | 8 ++-- tests/HttpTransportTest.php | 23 +++++------ tests/QueryMapperExceptionsTest.php | 2 +- tests/QueryMapperTest.php | 56 +++++++++++++------------- tests/QueryStatisticExceptionsTest.php | 2 +- tests/QueryStatisticTest.php | 2 +- tests/ResultExceptionsTest.php | 4 +- tests/ResultTest.php | 10 ++--- tests/SanitizerTest.php | 4 +- tests/ServerOptionsTest.php | 2 +- tests/ServerTest.php | 4 +- tests/TempTableTest.php | 2 +- 15 files changed, 80 insertions(+), 81 deletions(-) diff --git a/tests/ClientExceptionsTest.php b/tests/ClientExceptionsTest.php index c84851a..564e5b3 100644 --- a/tests/ClientExceptionsTest.php +++ b/tests/ClientExceptionsTest.php @@ -10,42 +10,42 @@ */ class ClientExceptionsTest extends TestCase { - public function testInvalidServerProvided() + public function testInvalidServerProvided(): void { $e = ClientException::invalidServerProvided('null'); $this->assertInstanceOf(ClientException::class, $e); } - public function testClusterIsNotProvided() + public function testClusterIsNotProvided(): void { $e = ClientException::clusterIsNotProvided(); $this->assertInstanceOf(ClientException::class, $e); } - public function testConnectionError() + public function testConnectionError(): void { $e = ClientException::connectionError(); $this->assertInstanceOf(ClientException::class, $e); } - public function testServerReturnedError() + public function testServerReturnedError(): void { $e = ClientException::serverReturnedError('Syntax error'); $this->assertInstanceOf(ClientException::class, $e); } - public function testMalformedResponseFromServer() + public function testMalformedResponseFromServer(): void { $e = ClientException::malformedResponseFromServer('some text'); $this->assertInstanceOf(ClientException::class, $e); } - public function testInsertFileNotFound() + public function testInsertFileNotFound(): void { $e = ClientException::insertFileNotFound('/tmp/test.csv'); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index d7aaf29..600cf35 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -18,7 +18,7 @@ */ class ClientTest extends TestCase { - public function testClickhouseUsingServer() + public function testClickhouseUsingServer(): void { $server = new Server('127.0.0.1'); $client = new Client($server); @@ -32,7 +32,7 @@ public function testClickhouseUsingServer() $client->using('h1'); } - public function testClickhouseUsingCluster() + public function testClickhouseUsingCluster(): void { $h1 = new Server('127.0.0.1'); $h2 = new Server('127.0.0.2'); @@ -69,7 +69,7 @@ public function testClickhouseUsingCluster() $client->using('h4'); } - public function testClickhouseUsingWrongServer() + public function testClickhouseUsingWrongServer(): void { $wrongServer = null; @@ -77,10 +77,10 @@ public function testClickhouseUsingWrongServer() $this->expectException(ClientException::class); $this->expectExceptionMessage($e->getMessage()); - $client = new Client($wrongServer); + new Client($wrongServer); } - protected function getMockedTransport(array $responses) + protected function getMockedTransport(array $responses): HttpTransport { $mock = new MockHandler($responses); @@ -91,9 +91,9 @@ protected function getMockedTransport(array $responses) ])); } - public function testClickhouseClientSyntaxError() + public function testClickhouseClientSyntaxError(): void { - $transport = $transport = $this->getMockedTransport([ + $transport = $this->getMockedTransport([ new Response(500, [], 'Syntax error'), ]); @@ -107,9 +107,9 @@ public function testClickhouseClientSyntaxError() $client->select('seelect 1'); } - public function testClickhouseQueries() + public function testClickhouseQueries(): void { - $transport = $transport = $this->getMockedTransport([ + $transport = $this->getMockedTransport([ new Response(200, [], json_encode([ 'data' => [ [ @@ -208,7 +208,7 @@ public function testClickhouseQueries() $result = $client->insertFiles('table', ['column', 'column'], $files); - $this->assertEquals(3, count($result)); + $this->assertCount(3, $result); $this->assertTrue($result[0]); $this->assertTrue($result[1]); $this->assertTrue($result[2]); @@ -222,7 +222,7 @@ public function testClickhouseQueries() $client->insertFiles('table', ['column', 'column'], $files, 'csv'); } - public function testClickhouseMapper() + public function testClickhouseMapper(): void { $mapper = new NamedMapper(); diff --git a/tests/ClusterExceptionsTest.php b/tests/ClusterExceptionsTest.php index 298b9a4..57eeae4 100644 --- a/tests/ClusterExceptionsTest.php +++ b/tests/ClusterExceptionsTest.php @@ -10,28 +10,28 @@ */ class ClusterExceptionsTest extends TestCase { - public function testMissingServerHostname() + public function testMissingServerHostname(): void { $e = ClusterException::missingServerHostname(); $this->assertInstanceOf(ClusterException::class, $e); } - public function testServerHostnameDuplicate() + public function testServerHostnameDuplicate(): void { $e = ClusterException::serverHostnameDuplicate('host-1'); $this->assertInstanceOf(ClusterException::class, $e); } - public function testInvalidServerProvided() + public function testInvalidServerProvided(): void { $e = ClusterException::invalidServerProvided(null); $this->assertInstanceOf(ClusterException::class, $e); } - public function testServerNotFound() + public function testServerNotFound(): void { $e = ClusterException::serverNotFound('host-1'); diff --git a/tests/ClusterTest.php b/tests/ClusterTest.php index b6626bc..023bc20 100644 --- a/tests/ClusterTest.php +++ b/tests/ClusterTest.php @@ -13,7 +13,7 @@ */ class ClusterTest extends TestCase { - public function testClusterConstructor() + public function testClusterConstructor(): void { $cluster = new Cluster(); @@ -37,7 +37,7 @@ public function testClusterConstructor() ); } - public function testClusterWrongHostname() + public function testClusterWrongHostname(): void { $servers = [ [ @@ -60,10 +60,10 @@ public function testClusterWrongHostname() $this->expectException(ClusterException::class); $this->expectExceptionMessage($e->getMessage()); - $cluster = new Cluster($servers); + new Cluster($servers); } - public function testClusterServers() + public function testClusterServers(): void { $options = (new ServerOptions())->setTimeout(10); diff --git a/tests/HttpTransportTest.php b/tests/HttpTransportTest.php index 4cb11c3..43c0022 100644 --- a/tests/HttpTransportTest.php +++ b/tests/HttpTransportTest.php @@ -8,7 +8,6 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use PHPUnit\Framework\TestCase; -use Tinderbox\Clickhouse\Common\Format; use Tinderbox\Clickhouse\Common\TempTable; use Tinderbox\Clickhouse\Exceptions\ClientException; use Tinderbox\Clickhouse\Query\Result; @@ -19,7 +18,7 @@ */ class HttpTransportTest extends TestCase { - protected function getMockedTransport(array $responses) + protected function getMockedTransport(array $responses): HttpTransport { $mock = new MockHandler($responses); @@ -35,7 +34,7 @@ protected function getServer() : Server return new Server('127.0.0.1', '8123', 'default', 'user', 'pass'); } - public function testGetWithFile() + public function testGetWithFile(): void { $file = sys_get_temp_dir().DIRECTORY_SEPARATOR.'numbers.csv'; file_put_contents($file, ''); @@ -132,7 +131,7 @@ public function testGetWithFile() unlink($file); } - public function testHttpTransportEmptyClient() + public function testHttpTransportEmptyClient(): void { $transport = new HttpTransport(); @@ -143,7 +142,7 @@ public function testHttpTransportEmptyClient() $transport->get($this->getServer(), 'select 1'); } - public function testHttpTransportSyntaxError() + public function testHttpTransportSyntaxError(): void { $server = $this->getServer(); @@ -158,7 +157,7 @@ public function testHttpTransportSyntaxError() $transport->get($server, 'seelect 1'); } - public function testHttpTransportSendError() + public function testHttpTransportSendError(): void { $server = $this->getServer(); @@ -173,7 +172,7 @@ public function testHttpTransportSendError() $transport->send($server, 'inseert into table'); } - public function testHttpTransportMalformedResponse() + public function testHttpTransportMalformedResponse(): void { $server = $this->getServer(); @@ -188,7 +187,7 @@ public function testHttpTransportMalformedResponse() $transport->get($server, 'select 1'); } - public function testHttpTransport() + public function testHttpTransport(): void { $server = $this->getServer(); @@ -286,7 +285,7 @@ public function testHttpTransport() unlink($files[0]); } - public function testSendConnectionError() + public function testSendConnectionError(): void { $server = $this->getServer(); @@ -299,7 +298,7 @@ public function testSendConnectionError() $transport->send($server, 'select 1'); } - public function testGetAsyncConnectionError() + public function testGetAsyncConnectionError(): void { $server = $this->getServer(); @@ -314,7 +313,7 @@ public function testGetAsyncConnectionError() ]); } - public function testGetAsyncError() + public function testGetAsyncError(): void { $transport = $this->getMockedTransport([ new Response(500, [], 'Syntax error'), @@ -329,7 +328,7 @@ public function testGetAsyncError() ]); } - public function testGetAsyncUnknownException() + public function testGetAsyncUnknownException(): void { $transport = $this->getMockedTransport([ new \Exception('Unknown exception'), diff --git a/tests/QueryMapperExceptionsTest.php b/tests/QueryMapperExceptionsTest.php index 73e6270..f44d64c 100644 --- a/tests/QueryMapperExceptionsTest.php +++ b/tests/QueryMapperExceptionsTest.php @@ -10,7 +10,7 @@ */ class QueryMapperExceptionsTest extends TestCase { - public function testMultipleBindingsType() + public function testMultipleBindingsType(): void { $e = QueryMapperException::multipleBindingsType(); diff --git a/tests/QueryMapperTest.php b/tests/QueryMapperTest.php index 151add1..a06e7aa 100644 --- a/tests/QueryMapperTest.php +++ b/tests/QueryMapperTest.php @@ -13,78 +13,78 @@ */ class QueryMapperTest extends TestCase { - public function testUnnamedMapperCheckMultipleBindings() + public function testUnnamedMapperCheckMultipleBindings(): void { $mapper = new UnnamedMapper(); $sql = 'SELECT * FROM table WHERE column = ? AND column2 IN (?, ?, ?, ?, ?)'; - $bindigns = [1, 2, 3, 4, 5, 'named' => 'test']; + $bindings = [1, 2, 3, 4, 5, 'named' => 'test']; $e = QueryMapperException::multipleBindingsType(); $this->expectException(QueryMapperException::class); $this->expectExceptionMessage($e->getMessage()); - $mapper->bind($sql, $bindigns); + $mapper->bind($sql, $bindings); } - public function testUnnamedNumericBindings() + public function testUnnamedNumericBindings(): void { $mapper = new UnnamedMapper(); $sql = 'SELECT * FROM table WHERE column = ? AND column2 IN (?, ?, ?, ?, ?)'; - $bindigns = [1, 2, 3, 4, 5, 6]; + $bindings = [1, 2, 3, 4, 5, 6]; - $result = $mapper->bind($sql, $bindigns); + $result = $mapper->bind($sql, $bindings); $this->assertEquals('SELECT * FROM table WHERE column = 1 AND column2 IN (2, 3, 4, 5, 6)', $result); } - public function testUnnamedStringBindings() + public function testUnnamedStringBindings(): void { $mapper = new UnnamedMapper(); $sql = 'SELECT * FROM table WHERE column = ? AND column2 IN (?, ?, ?, ?, ?)'; - $bindigns = ['test', 'a', 'b', 'c', 'd', 'e']; + $bindings = ['test', 'a', 'b', 'c', 'd', 'e']; - $result = $mapper->bind($sql, $bindigns); + $result = $mapper->bind($sql, $bindings); $this->assertEquals("SELECT * FROM table WHERE column = 'test' AND column2 IN ('a', 'b', 'c', 'd', 'e')", $result); - $bindigns = ["test with 'quotes' and / other \\bad stuff", 'a', 'b', 'c', 'd', 'e']; + $bindings = ["test with 'quotes' and / other \\bad stuff", 'a', 'b', 'c', 'd', 'e']; - $result = $mapper->bind($sql, $bindigns); + $result = $mapper->bind($sql, $bindings); $this->assertEquals("SELECT * FROM table WHERE column = 'test with \'quotes\' and / other \\\\bad stuff' AND column2 IN ('a', 'b', 'c', 'd', 'e')", $result); - $bindigns = ['test with ? mark', 'a', 'b and here ? too', 'c', 'd', 'e']; + $bindings = ['test with ? mark', 'a', 'b and here ? too', 'c', 'd', 'e']; - $result = $mapper->bind($sql, $bindigns); + $result = $mapper->bind($sql, $bindings); $this->assertEquals("SELECT * FROM table WHERE column = 'test with ? mark' AND column2 IN ('a', 'b and here ? too', 'c', 'd', 'e')", $result); - $bindigns = ['test with %s text', 'a', 'b', 'c', 'd', 'e']; + $bindings = ['test with %s text', 'a', 'b', 'c', 'd', 'e']; - $result = $mapper->bind($sql, $bindigns); + $result = $mapper->bind($sql, $bindings); $this->assertEquals("SELECT * FROM table WHERE column = 'test with %s text' AND column2 IN ('a', 'b', 'c', 'd', 'e')", $result); } - public function testNamedMapperCheckMultipleBindings() + public function testNamedMapperCheckMultipleBindings(): void { $mapper = new NamedMapper(); $sql = 'SELECT * FROM table WHERE column = :a AND column2 IN (:b, :c, :d, :e, :f)'; - $bindigns = [1, 2, 3, 4, 5, 'named' => 'test']; + $bindings = [1, 2, 3, 4, 5, 'named' => 'test']; $e = QueryMapperException::multipleBindingsType(); $this->expectException(QueryMapperException::class); $this->expectExceptionMessage($e->getMessage()); - $mapper->bind($sql, $bindigns); + $mapper->bind($sql, $bindings); } - public function testNamedNumericBindings() + public function testNamedNumericBindings(): void { $mapper = new NamedMapper(); $sql = 'SELECT * FROM table WHERE column = :a AND column2 IN (:b, :c, :d, :e, :f)'; - $bindigns = [ + $bindings = [ 'a' => 1, 'b' => 2, 'c' => 3, @@ -93,16 +93,16 @@ public function testNamedNumericBindings() 'f' => 6, ]; - $result = $mapper->bind($sql, $bindigns); + $result = $mapper->bind($sql, $bindings); $this->assertEquals('SELECT * FROM table WHERE column = 1 AND column2 IN (2, 3, 4, 5, 6)', $result); } - public function testNamedStringBindings() + public function testNamedStringBindings(): void { $mapper = new NamedMapper(); $sql = 'SELECT * FROM table WHERE column = :a AND column2 IN (:b, :c, :d, :e, :f)'; - $bindigns = [ + $bindings = [ 'a' => 'test', 'b' => 'a', 'c' => 'b', @@ -111,11 +111,11 @@ public function testNamedStringBindings() 'f' => 'e', ]; - $result = $mapper->bind($sql, $bindigns); + $result = $mapper->bind($sql, $bindings); $this->assertEquals("SELECT * FROM table WHERE column = 'test' AND column2 IN ('a', 'b', 'c', 'd', 'e')", $result); - $bindigns = [ + $bindings = [ 'a' => 'test with \'quotes\' and / other \\bad stuff', 'b' => 'a', 'c' => 'b', @@ -124,11 +124,11 @@ public function testNamedStringBindings() 'f' => 'e', ]; - $result = $mapper->bind($sql, $bindigns); + $result = $mapper->bind($sql, $bindings); $this->assertEquals("SELECT * FROM table WHERE column = 'test with \'quotes\' and / other \\\\bad stuff' AND column2 IN ('a', 'b', 'c', 'd', 'e')", $result); - $bindigns = [ + $bindings = [ 'a' => 'test with :name', 'b' => 'a', 'c' => 'b', @@ -137,7 +137,7 @@ public function testNamedStringBindings() 'f' => 'e', ]; - $result = $mapper->bind($sql, $bindigns); + $result = $mapper->bind($sql, $bindings); $this->assertEquals("SELECT * FROM table WHERE column = 'test with :name' AND column2 IN ('a', 'b', 'c', 'd', 'e')", $result); } diff --git a/tests/QueryStatisticExceptionsTest.php b/tests/QueryStatisticExceptionsTest.php index 950f5c9..5bd8ef1 100644 --- a/tests/QueryStatisticExceptionsTest.php +++ b/tests/QueryStatisticExceptionsTest.php @@ -10,7 +10,7 @@ */ class QueryStatisticExceptionsTest extends TestCase { - public function testPropertyNotExists() + public function testPropertyNotExists(): void { $e = QueryStatisticException::propertyNotExists('test'); diff --git a/tests/QueryStatisticTest.php b/tests/QueryStatisticTest.php index be90093..8d85c36 100644 --- a/tests/QueryStatisticTest.php +++ b/tests/QueryStatisticTest.php @@ -11,7 +11,7 @@ */ class QueryStatisticTest extends TestCase { - public function testQueryStatistic() + public function testQueryStatistic(): void { $statistic = new QueryStatistic(100, 1024, 0.122); diff --git a/tests/ResultExceptionsTest.php b/tests/ResultExceptionsTest.php index b39cf8f..583d452 100644 --- a/tests/ResultExceptionsTest.php +++ b/tests/ResultExceptionsTest.php @@ -10,14 +10,14 @@ */ class ResultExceptionsTest extends TestCase { - public function testPropertyNotExists() + public function testPropertyNotExists(): void { $e = ResultException::propertyNotExists('test'); $this->assertInstanceOf(ResultException::class, $e); } - public function testIsReadonly() + public function testIsReadonly(): void { $e = ResultException::isReadonly(); diff --git a/tests/ResultTest.php b/tests/ResultTest.php index c13715d..df3cd68 100644 --- a/tests/ResultTest.php +++ b/tests/ResultTest.php @@ -14,7 +14,7 @@ */ class ResultTest extends TestCase { - public function testResult() + public function testResult(): void { $rows = [ [ @@ -50,7 +50,7 @@ public function testResult() $result->miss; } - public function testResultCountable() + public function testResultCountable(): void { $statistic = new QueryStatistic(5, 1024, 0.122); @@ -59,7 +59,7 @@ public function testResultCountable() $this->assertEquals(3, count($result)); } - public function testResultArrayAccessSet() + public function testResultArrayAccessSet(): void { $rows = [ [ @@ -92,7 +92,7 @@ public function testResultArrayAccessSet() $result[1] = 'test'; } - public function testResultArrayAccessUnset() + public function testResultArrayAccessUnset(): void { $rows = [ [ @@ -122,7 +122,7 @@ public function testResultArrayAccessUnset() unset($result[1]); } - public function testResultIterator() + public function testResultIterator(): void { $rows = [ [ diff --git a/tests/SanitizerTest.php b/tests/SanitizerTest.php index 0fdb9a9..56a4e1c 100644 --- a/tests/SanitizerTest.php +++ b/tests/SanitizerTest.php @@ -10,7 +10,7 @@ */ class SanitizerTest extends TestCase { - public function testEscapeNumericValue() + public function testEscapeNumericValue(): void { $value = 1; $escaped = Sanitizer::escape($value); @@ -18,7 +18,7 @@ public function testEscapeNumericValue() $this->assertEquals($value, $escaped); } - public function testEscapeStringValue() + public function testEscapeStringValue(): void { $value = "some-test with 'quotes'"; $escaped = Sanitizer::escape($value); diff --git a/tests/ServerOptionsTest.php b/tests/ServerOptionsTest.php index e6e46fc..41fd217 100644 --- a/tests/ServerOptionsTest.php +++ b/tests/ServerOptionsTest.php @@ -10,7 +10,7 @@ */ class ServerOptionsTest extends TestCase { - public function testServerOptions() + public function testServerOptions(): void { $options = new ServerOptions(); diff --git a/tests/ServerTest.php b/tests/ServerTest.php index c681fda..b28f593 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -11,7 +11,7 @@ */ class ServerTest extends TestCase { - public function testServerDefaultOptions() + public function testServerDefaultOptions(): void { $server = new Server('127.0.0.1'); @@ -19,7 +19,7 @@ public function testServerDefaultOptions() $this->assertEquals(5.0, $server->getOptions()->getTimeout()); } - public function testClickhouseGettersSetters() + public function testClickhouseGettersSetters(): void { $options = new ServerOptions(); $options->setTimeout(10); diff --git a/tests/TempTableTest.php b/tests/TempTableTest.php index e79eb98..0c3cf3d 100644 --- a/tests/TempTableTest.php +++ b/tests/TempTableTest.php @@ -11,7 +11,7 @@ */ class TempTableTest extends TestCase { - public function testTempTable() + public function testTempTable(): void { $table = new TempTable('table', 'source', [ 'structure', From 8f62a710399961aa142d557ece883e6e1f12caa1 Mon Sep 17 00:00:00 2001 From: esazykin Date: Mon, 27 Nov 2017 12:03:26 +0300 Subject: [PATCH 4/9] review tests --- src/Exceptions/QueryMapperException.php | 4 ++-- tests/QueryMapperExceptionsTest.php | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Exceptions/QueryMapperException.php b/src/Exceptions/QueryMapperException.php index ff7d793..794c8e7 100644 --- a/src/Exceptions/QueryMapperException.php +++ b/src/Exceptions/QueryMapperException.php @@ -4,12 +4,12 @@ class QueryMapperException extends \Exception { - public static function wrongBindingsNumber($inQuery, $count) + public static function wrongBindingsNumber(int $inQuery, int $count): self { return new static('Wrong bindings count. Bindings found in query: '.$inQuery.' and given '.$count); } - public static function multipleBindingsType() + public static function multipleBindingsType(): self { return new static('Both named and unnamed bindings found'); } diff --git a/tests/QueryMapperExceptionsTest.php b/tests/QueryMapperExceptionsTest.php index f44d64c..6257bae 100644 --- a/tests/QueryMapperExceptionsTest.php +++ b/tests/QueryMapperExceptionsTest.php @@ -16,4 +16,11 @@ public function testMultipleBindingsType(): void $this->assertInstanceOf(QueryMapperException::class, $e); } + + public function testWrongBindingsNumber(): void + { + $e = QueryMapperException::wrongBindingsNumber(2, 1); + + $this->assertInstanceOf(QueryMapperException::class, $e); + } } From 7a83b4c8a82c201cbeb1b296d471e9d86075bd54 Mon Sep 17 00:00:00 2001 From: esazykin Date: Mon, 27 Nov 2017 12:16:39 +0300 Subject: [PATCH 5/9] styling --- src/Client.php | 8 ++--- src/Query/Mapper/UnnamedMapper.php | 5 +-- src/Query/Result.php | 2 +- .../ClickhouseCLIClientTransport.php | 36 ++++++++++--------- src/Transport/HttpTransport.php | 6 ++-- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/Client.php b/src/Client.php index da0281c..5be8adb 100644 --- a/src/Client.php +++ b/src/Client.php @@ -47,7 +47,7 @@ class Client protected $cluster; /** - * Tells client to send queries over whole cluster selecting server by random + * Tells client to send queries over whole cluster selecting server by random. * * @var bool */ @@ -85,7 +85,7 @@ public function __construct($server, QueryMapperInterface $mapper = null, Transp } /** - * Sets flag to use random server in cluster + * Sets flag to use random server in cluster. * * @param bool $flag */ @@ -95,7 +95,7 @@ public function useRandomServer(bool $flag) } /** - * Returns flag which tells client to use random server in cluster + * Returns flag which tells client to use random server in cluster. * * @return bool */ @@ -197,7 +197,7 @@ public function getServer(): Server } /** - * Returns random server from cluster + * Returns random server from cluster. * * @return \Tinderbox\Clickhouse\Server */ diff --git a/src/Query/Mapper/UnnamedMapper.php b/src/Query/Mapper/UnnamedMapper.php index 5840573..53f5090 100644 --- a/src/Query/Mapper/UnnamedMapper.php +++ b/src/Query/Mapper/UnnamedMapper.php @@ -18,10 +18,11 @@ class UnnamedMapper extends AbstractMapper implements QueryMapperInterface * Binds values to query. * * @param string $query - * @param array $bindings + * @param array $bindings * - * @return string * @throws QueryMapperException + * + * @return string */ public function bind(string $query, array $bindings): string { diff --git a/src/Query/Result.php b/src/Query/Result.php index 15d95a0..4ac8385 100644 --- a/src/Query/Result.php +++ b/src/Query/Result.php @@ -142,7 +142,7 @@ public function current() public function next() { - ++$this->current; + $this->current++; } public function key() diff --git a/src/Transport/ClickhouseCLIClientTransport.php b/src/Transport/ClickhouseCLIClientTransport.php index a7958b1..2a09e32 100644 --- a/src/Transport/ClickhouseCLIClientTransport.php +++ b/src/Transport/ClickhouseCLIClientTransport.php @@ -15,14 +15,14 @@ class ClickhouseCLIClientTransport implements TransportInterface { /** - * Path to executable clickhouse cli client + * Path to executable clickhouse cli client. * * @var string */ protected $executablePath; /** - * Last execute query + * Last execute query. * * @var string */ @@ -80,8 +80,9 @@ public function send(Server $server, string $query): bool * @param array $files * @param int $concurrency * - * @return array * @throws \Tinderbox\Clickhouse\Exceptions\ClientException + * + * @return array */ public function sendAsyncFilesWithQuery(Server $server, string $query, array $files, int $concurrency = 5): array { @@ -159,7 +160,7 @@ public function getAsync(Server $server, array $queries, int $concurrency = 5): } /** - * Puts query in tmp file + * Puts query in tmp file. * * @param string $query * @@ -178,7 +179,7 @@ protected function writeQueryInFile(string $query) : string } /** - * Removes tmp file with query + * Removes tmp file with query. * * @param string $fileName */ @@ -188,7 +189,7 @@ protected function removeQueryFile(string $fileName) } /** - * Builds command for write + * Builds command for write. * * @param \Tinderbox\Clickhouse\Server $server * @param string $query @@ -203,7 +204,7 @@ protected function buildCommandForWrite(Server $server, string $query, string $f $command = []; if (!is_null($file)) { - $command[] = "cat ".$file.' |'; + $command[] = 'cat '.$file.' |'; } $command = array_merge($command, [ @@ -211,14 +212,14 @@ protected function buildCommandForWrite(Server $server, string $query, string $f "--host='{$server->getHost()}'", "--port='{$server->getPort()}'", "--database='{$server->getDatabase()}'", - "--query={$query}" + "--query={$query}", ]); return implode(' ', $command); } /** - * Builds command to read + * Builds command to read. * * @param \Tinderbox\Clickhouse\Server $server * @param string $query @@ -236,7 +237,7 @@ protected function buildCommandForRead(Server $server, string $query, $tables = "--host='{$server->getHost()}'", "--port='{$server->getPort()}'", "--database='{$server->getDatabase()}'", - "--max_query_size=".strlen($query), + '--max_query_size='.strlen($query), ]; if ($tables instanceof TempTable || !empty($tables)) { @@ -264,21 +265,22 @@ protected function parseTempTable(TempTable $table) list($structure, $withColumns) = $this->assembleTempTableStructure($table); return [ - "--external", + '--external', ($withColumns ? '--structure=' : '--types=')."'{$structure}'", "--format='{$table->getFormat()}'", "--name='{$table->getName()}'", - "--file='{$table->getSource()}'" + "--file='{$table->getSource()}'", ]; } /** - * Executes command and catches result or error via stdout and stderr + * Executes command and catches result or error via stdout and stderr. * * @param string $command * - * @return string * @throws \Tinderbox\Clickhouse\Exceptions\ClientException + * + * @return string */ protected function executeCommand(string $command) : string { @@ -307,7 +309,7 @@ protected function executeCommand(string $command) : string } /** - * Sets last executed query + * Sets last executed query. * * @param string $query */ @@ -317,7 +319,7 @@ protected function setLastQuery(string $query) } /** - * Returns last executed query + * Returns last executed query. * * @return string */ @@ -327,7 +329,7 @@ protected function getLastQuery() : string } /** - * Cleans string from tabs and spaces + * Cleans string from tabs and spaces. * * @param string $string * diff --git a/src/Transport/HttpTransport.php b/src/Transport/HttpTransport.php index 05e44c1..e7ac97c 100644 --- a/src/Transport/HttpTransport.php +++ b/src/Transport/HttpTransport.php @@ -137,7 +137,7 @@ public function sendAsyncFilesWithQuery(Server $server, string $query, array $fi $requests = function ($files) use ($server, $query) { foreach ($files as $file) { $headers = array_merge($this->getHeaders(), [ - 'Content-Length' => null + 'Content-Length' => null, ]); @@ -160,7 +160,7 @@ public function sendAsyncFilesWithQuery(Server $server, string $query, array $fi 'rejected' => $this->parseReason(), 'options' => [ 'connect_timeout' => $server->getOptions()->getTimeout(), - 'expect' => false + 'expect' => false, ], ] ); @@ -361,7 +361,7 @@ public function getAsync(Server $server, array $queries, int $concurrency = 5): 'rejected' => $this->parseReason(), 'options' => [ 'connect_timeout' => $server->getOptions()->getTimeout(), - 'expect' => false + 'expect' => false ], ] ); From 57a9f1f3686893f699334db116d909fbcccf2313 Mon Sep 17 00:00:00 2001 From: esazykin Date: Mon, 27 Nov 2017 12:19:58 +0300 Subject: [PATCH 6/9] styling --- src/Transport/HttpTransport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transport/HttpTransport.php b/src/Transport/HttpTransport.php index e7ac97c..f5b4aa0 100644 --- a/src/Transport/HttpTransport.php +++ b/src/Transport/HttpTransport.php @@ -361,7 +361,7 @@ public function getAsync(Server $server, array $queries, int $concurrency = 5): 'rejected' => $this->parseReason(), 'options' => [ 'connect_timeout' => $server->getOptions()->getTimeout(), - 'expect' => false + 'expect' => false, ], ] ); From 2fade07eda0a93b7db2f650e83c46d083f7fa27a Mon Sep 17 00:00:00 2001 From: esazykin Date: Mon, 27 Nov 2017 12:22:50 +0300 Subject: [PATCH 7/9] styling --- src/Transport/HttpTransport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transport/HttpTransport.php b/src/Transport/HttpTransport.php index f5b4aa0..56da73a 100644 --- a/src/Transport/HttpTransport.php +++ b/src/Transport/HttpTransport.php @@ -414,7 +414,7 @@ protected function assembleResult(ResponseInterface $response): Result protected function buildRequestUri(Server $server, array $query = []): string { $uri = $server->getOptions()->getProtocol().'://'.$server->getHost().':'.$server->getPort(); - + if (!is_null($server->getDatabase())) { $query['database'] = $server->getDatabase(); } From d2f811c57ee82128b9b0c2bca7c50c1cd9ae0f00 Mon Sep 17 00:00:00 2001 From: esazykin Date: Mon, 27 Nov 2017 12:27:18 +0300 Subject: [PATCH 8/9] styling --- src/Transport/HttpTransport.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Transport/HttpTransport.php b/src/Transport/HttpTransport.php index 56da73a..de826c7 100644 --- a/src/Transport/HttpTransport.php +++ b/src/Transport/HttpTransport.php @@ -140,7 +140,6 @@ public function sendAsyncFilesWithQuery(Server $server, string $query, array $fi 'Content-Length' => null, ]); - $request = new Request( 'POST', $this->buildRequestUri($server, ['query' => $query]), $headers, $this->getFileHandle($file) ); From b9b087b0150fc18008300f3cc8abe588bda07d32 Mon Sep 17 00:00:00 2001 From: esazykin Date: Tue, 28 Nov 2017 18:36:47 +0300 Subject: [PATCH 9/9] improvement --- src/Query/Mapper/UnnamedMapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Query/Mapper/UnnamedMapper.php b/src/Query/Mapper/UnnamedMapper.php index 53f5090..d7b4024 100644 --- a/src/Query/Mapper/UnnamedMapper.php +++ b/src/Query/Mapper/UnnamedMapper.php @@ -31,7 +31,7 @@ public function bind(string $query, array $bindings): string $query = str_replace(['%', '?'], ['%%', '%s'], $query); - $query = sprintf(...array_merge([$query], $escapedBindings)); + $query = sprintf($query, ...$escapedBindings); return $query; }