From 06823e1aaad78e6cd3506902e311148cb9131908 Mon Sep 17 00:00:00 2001 From: Andrey Maltsev Date: Fri, 2 Aug 2019 16:09:40 +0300 Subject: [PATCH] fix cs --- tests/ClientTest.php | 136 +++++++++++++++++----------------- tests/HttpTransportTest.php | 72 +++++++++--------- tests/Support/ServerTrait.php | 4 +- 3 files changed, 106 insertions(+), 106 deletions(-) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index c4325c7..2847929 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -28,47 +28,47 @@ public function testGetters() $server = $this->getServer(); $serverProvider = new ServerProvider(); $serverProvider->addServer($server); - + $client = new Client($serverProvider); - + $this->assertEquals( $serverProvider->getRandomServer(), $client->getServer(), 'Correctly passes server provider' ); } - + public function testMappers() { $server = $this->getServer(); $serverProvider = new ServerProvider(); $serverProvider->addServer($server); - + $client = new Client($serverProvider, new NamedMapper()); - + $result = $client->readOne('select number from numbers(:min, :max)', ['min' => 0, 'max' => 10]); - + $this->assertEquals(10, count($result->rows), 'Correctly changes mapper'); } - + public function testTransports() { $server = $this->getServer(); $serverProvider = new ServerProvider(); $serverProvider->addServer($server); - + $transport = $this->createMock(TransportInterface::class); $transport->method('read')->willReturn([ - new Result(new Query($server, ''), [0,1], new QueryStatistic(0,0,0,0)) + new Result(new Query($server, ''), [0, 1], new QueryStatistic(0, 0, 0, 0)), ]); - + $client = new Client($serverProvider, null, $transport); - + $result = $client->readOne('test query'); - + $this->assertEquals(2, count($result->rows), 'Correctly changes transport'); } - + public function testClusters() { $cluster = new Cluster( @@ -78,7 +78,7 @@ public function testClusters() new Server('127.0.0.3'), ] ); - + $cluster2 = new Cluster( 'test2', [ new Server('127.0.0.4'), @@ -86,89 +86,89 @@ public function testClusters() new Server('127.0.0.6'), ] ); - + $serverProvider = new ServerProvider(); $serverProvider->addCluster($cluster)->addCluster($cluster2); - + $client = new Client($serverProvider); - + $server = $client->onCluster('test')->getServer(); /* will return random server from cluster */ $this->assertContains( $server, $cluster->getServers(), 'Correctly returns random server from specified cluster' ); - + $this->assertEquals($server, $client->getServer(), 'Remembers firstly selected random server for next calls'); - + $client->using('127.0.0.3'); $server = $client->getServer(); - + $this->assertEquals( '127.0.0.3', $server->getHost(), 'Correctly returns specified server from specified cluster' ); - + $server = $client->onCluster('test2')->getServer(); /* will return random server from cluster */ $this->assertContains( $server, $cluster2->getServers(), 'Correctly returns random server from specified cluster' ); - + $client->usingRandomServer(); $server = $client->getServer(); - + while ($server === $client->getServer()) { /* Randomize while get non used server */ } - + $this->assertTrue(true, 'Correctly randomizes cluster servers on each call'); - + $this->expectException(ClusterException::class); $this->expectExceptionMessage('Server with hostname [127.0.0.0] is not found in cluster'); - + $client->onCluster('test')->using('127.0.0.0')->getServer(); } - + public function testServers() { $server1 = new Server('127.0.0.1'); $server2 = new Server('127.0.0.2'); $server3 = new Server('127.0.0.3'); - + $serverProvider = new ServerProvider(); $serverProvider->addServer($server1)->addServer($server2)->addServer($server3); - + $client = new Client($serverProvider); - + $server = $client->getServer(); $this->assertTrue( in_array($server->getHost(), ['127.0.0.1', '127.0.0.2', '127.0.0.3'], true), 'Correctly returns random server' ); - + $this->assertEquals($server, $client->getServer(), 'Remembers firstly selected random server for next calls'); - + $server = $client->using('127.0.0.3')->getServer(); $this->assertEquals('127.0.0.3', $server->getHost(), 'Correctly returns specified server'); - + $client->usingRandomServer(); $server = $client->getServer(); - + while ($server === $client->getServer()) { /* Randomize while get non used server */ } - + $this->assertTrue(true, 'Correctly randomizes cluster servers on each call'); - + $this->expectException(ServerProviderException::class); $this->expectExceptionMessage('Can not find server with hostname [127.0.0.0]'); - + $client->using('127.0.0.0')->getServer(); } - + public function testClusterAndServersTogether() { $cluster = new Cluster( @@ -178,116 +178,116 @@ public function testClusterAndServersTogether() new Server('127.0.0.3'), ] ); - + $server1 = new Server('127.0.0.4'); $server2 = new Server('127.0.0.5'); $server3 = new Server('127.0.0.6'); - + $serverProvider = new ServerProvider(); $serverProvider->addCluster($cluster)->addServer($server1)->addServer($server2)->addServer($server3); - + $client = new Client($serverProvider); - + $server = $client->getServer(); $this->assertTrue( in_array($server->getHost(), ['127.0.0.4', '127.0.0.5', '127.0.0.6'], true), 'Correctly returns random server not in cluster' ); - + $this->assertEquals($server, $client->getServer(), 'Remembers firstly selected random server for next calls'); - + $client->onCluster('test'); - + $server = $client->onCluster('test')->getServer(); /* will return random server from cluster */ $this->assertContains( $server, $cluster->getServers(), 'Correctly returns random server from specified cluster' ); - + $this->assertEquals($server, $client->getServer(), 'Remembers firstly selected random server for next calls'); - + $server = $client->onCluster(null)->getServer(); $this->assertTrue( in_array($server->getHost(), ['127.0.0.4', '127.0.0.5', '127.0.0.6'], true), 'Correctly returns random server after disabling cluster mode' ); } - + protected function getClient(): Client { $serverProvider = new ServerProvider(); $serverProvider->addServer($this->getServer()); - + return new Client($serverProvider); } - + public function testReadOne() { $client = $this->getClient(); - + $result = $client->readOne('select * from numbers(?, ?) order by number desc', [0, 10]); - + $this->assertEquals(10, count($result->rows), 'Correctly executes query using mapper'); } - + public function testRead() { $client = $this->getClient(); - + $result = $client->read( [ [ - 'query' => 'select * from numbers(?, ?) order by number desc', + 'query' => 'select * from numbers(?, ?) order by number desc', 'bindings' => [0, 10], ], new Query($client->getServer(), 'select * from numbers(0, 20) order by number desc'), new Query($client->getServer(), 'select * from numbers(0, 20) where number in tab order by number desc', [ - new TempTable('tab', new FileFromString('1'.PHP_EOL.'2'.PHP_EOL), ['number' => 'UInt64'], Format::TSV) + new TempTable('tab', new FileFromString('1'.PHP_EOL.'2'.PHP_EOL), ['number' => 'UInt64'], Format::TSV), ]), ] ); - + $this->assertEquals(10, count($result[0]->rows), 'Correctly converts query from array to query instance'); $this->assertEquals(20, count($result[1]->rows), 'Correctly executes queries'); $this->assertEquals(2, count($result[2]->rows), 'Correctly executes query with file'); } - + public function testWrite() { $client = $this->getClient(); - + $client->write([ new Query($client->getServer(), 'drop table if exists default.tdchc_test_table'), new Query($client->getServer(), 'create table default.tdchc_test_table (number UInt64) engine = Memory'), ], 1); - + $client->writeOne('insert into default.tdchc_test_table (number) FORMAT TSV', [], [ - new FileFromString('1'.PHP_EOL.'2'.PHP_EOL) + new FileFromString('1'.PHP_EOL.'2'.PHP_EOL), ]); - + $result = $client->readOne('select * from default.tdchc_test_table'); - + $this->assertEquals(2, count($result->rows), 'Correctly writes data'); } - + public function testWriteFiles() { $client = $this->getClient(); - + $client->write([ new Query($client->getServer(), 'drop table if exists default.tdchc_test_table'), new Query($client->getServer(), 'create table default.tdchc_test_table (number UInt64) engine = Memory'), ], 1); - + $client->writeFiles('default.tdchc_test_table', ['number'], [ new FileFromString('1'.PHP_EOL.'2'.PHP_EOL), new FileFromString('3'.PHP_EOL.'4'.PHP_EOL), new FileFromString('5'.PHP_EOL.'6'.PHP_EOL), ], Format::TSV); - + $result = $client->readOne('select * from default.tdchc_test_table'); - + $this->assertEquals(6, count($result->rows), 'Correctly writes data'); } } diff --git a/tests/HttpTransportTest.php b/tests/HttpTransportTest.php index a4bbab6..b67804c 100644 --- a/tests/HttpTransportTest.php +++ b/tests/HttpTransportTest.php @@ -22,7 +22,7 @@ class HttpTransportTest extends TestCase { use ServerTrait; - protected function getMockedTransport(array $responses) : HttpTransport + protected function getMockedTransport(array $responses): HttpTransport { $mock = new MockHandler($responses); @@ -33,12 +33,12 @@ protected function getMockedTransport(array $responses) : HttpTransport ])); } - protected function getQuery() : Query + protected function getQuery(): Query { return new Query($this->getServer(), 'select * from table'); } - protected function getTransport() : HttpTransport + protected function getTransport(): HttpTransport { return new HttpTransport(); } @@ -47,18 +47,18 @@ public function testRead() { $transport = $this->getMockedTransport([ new Response(200, [], json_encode([ - 'data' => [ + 'data' => [ [ '1' => 1, ], ], - 'statistics' => [ + 'statistics' => [ 'rows_read' => 1, 'bytes_read' => 1, 'elapsed' => 0.100, ], - 'rows_before_limit_at_least' => 1024 - ])) + 'rows_before_limit_at_least' => 1024, + ])), ]); $result = $transport->read([$this->getQuery()]); @@ -76,7 +76,7 @@ public function testRead() ], [ 'rows_read' => $result[0]->statistic->rows, 'bytes_read' => $result[0]->statistic->bytes, - 'elapsed' => $result[0]->statistic->time + 'elapsed' => $result[0]->statistic->time, ], 'Returns correct statistic from server'); $this->assertEquals(1024, $result[0]->statistic->rowsBeforeLimitAtLeast, 'Returns correct rows_before_limit_at_least'); @@ -86,32 +86,32 @@ public function testReadMultipleRequests() { $transport = $this->getMockedTransport([ new Response(200, [], json_encode([ - 'data' => [ + 'data' => [ [ '1' => 1, ], ], - 'statistics' => [ + 'statistics' => [ 'rows_read' => 1, 'bytes_read' => 1, 'elapsed' => 0.100, ], - 'rows_before_limit_at_least' => 1024 + 'rows_before_limit_at_least' => 1024, ])), new Response(200, [], json_encode([ - 'data' => [ + 'data' => [ [ '1' => 2, ], ], - 'statistics' => [ + 'statistics' => [ 'rows_read' => 2, 'bytes_read' => 2, 'elapsed' => 0.100, ], - 'rows_before_limit_at_least' => 1025 - ])) + 'rows_before_limit_at_least' => 1025, + ])), ]); $result = $transport->read([$this->getQuery(), $this->getQuery()]); @@ -129,7 +129,7 @@ public function testReadMultipleRequests() ], [ 'rows_read' => $result[0]->statistic->rows, 'bytes_read' => $result[0]->statistic->bytes, - 'elapsed' => $result[0]->statistic->time + 'elapsed' => $result[0]->statistic->time, ], 'Returns correct statistic from server'); $this->assertEquals(1024, $result[0]->statistic->rowsBeforeLimitAtLeast, 'Returns correct rows_before_limit_at_least'); @@ -147,7 +147,7 @@ public function testReadMultipleRequests() ], [ 'rows_read' => $result[1]->statistic->rows, 'bytes_read' => $result[1]->statistic->bytes, - 'elapsed' => $result[1]->statistic->time + 'elapsed' => $result[1]->statistic->time, ], 'Returns correct statistic from server'); $this->assertEquals(1025, $result[1]->statistic->rowsBeforeLimitAtLeast, 'Returns correct rows_before_limit_at_least'); @@ -166,7 +166,7 @@ public function testReadWithTablesUnstructured() $query = new Query($this->getServer(), 'select * from numbers(0, 10) where number in temp', [$table]); $result = $transport->read([$query]); - $this->assertEquals([1,2], array_column($result[0]->rows, 'number')); + $this->assertEquals([1, 2], array_column($result[0]->rows, 'number')); unlink($tableSource); } @@ -184,7 +184,7 @@ public function testReadWithTablesStructured() $query = new Query($this->getServer(), 'select number from temp', [$table]); $result = $transport->read([$query]); - $this->assertEquals([1,2], array_column($result[0]->rows, 'number'), 'Returns correct result when uses temp tables for read queries'); + $this->assertEquals([1, 2], array_column($result[0]->rows, 'number'), 'Returns correct result when uses temp tables for read queries'); unlink($tableSource); } @@ -229,12 +229,12 @@ public function testWrite() $this->assertEquals([ [ 'number' => 1, - 'string' => 'some' + 'string' => 'some', ], [ 'number' => 2, - 'string' => 'string' - ] + 'string' => 'string', + ], ], $result[0]->rows, 'Returns correct result from recently created table and filled with temp files'); $handle = $table->open(); @@ -288,31 +288,31 @@ public function testWriteMultipleFilesPerOneQuery() $this->assertEquals([ [ 'number' => 1, - 'string' => 'some' + 'string' => 'some', ], [ 'number' => 2, - 'string' => 'string' + 'string' => 'string', ], [ 'number' => 3, - 'string' => 'string' + 'string' => 'string', ], [ 'number' => 4, - 'string' => 'some' - ] + 'string' => 'some', + ], ], $result[0]->rows, 'Returns correct result from recently created table and filled with temp files'); $this->assertEquals([ [ 'number' => 3, - 'string' => 'string' + 'string' => 'string', ], [ 'number' => 4, - 'string' => 'some' - ] + 'string' => 'some', + ], ], $result[1]->rows, 'Returns correct result from recently created table and filled with temp files'); $transport->write([ @@ -337,7 +337,7 @@ public function testConnectionError() $this->expectExceptionMessage('Can\'t connect to the server [KHGIUYhakljsfnk:8123]'); $transport->read([ - new Query(new Server('KHGIUYhakljsfnk'), '') + new Query(new Server('KHGIUYhakljsfnk'), ''), ]); } @@ -351,7 +351,7 @@ public function testUnknownReason() $this->expectExceptionMessage('Unknown exception'); $transport->read([ - new Query($this->getServer(), '') + new Query($this->getServer(), ''), ]); } @@ -366,7 +366,7 @@ public function testHttpTransportMalformedResponse() $this->expectExceptionMessage($e->getMessage()); $transport->read([ - new Query($this->getServer(), '') + new Query($this->getServer(), ''), ]); } @@ -393,7 +393,7 @@ public function testConnectionWithPasswordOnWrite() $server = $this->getServer('default', 'default', 'pass'); $transport->write([ - new Query($server, 'insert into table ', [new FileFromString('aaa')]) + new Query($server, 'insert into table ', [new FileFromString('aaa')]), ]); } @@ -482,7 +482,7 @@ public function testTempTableReadAndWrite() $fileContent = []; for ($i = 0; $i < 100; $i++) { - $fileContent[] = $i."\t".($i >= 50 ? 'string' : 'some').PHP_EOL; + $fileContent[] = $i."\t".($i >= 50 ? 'string' : 'some' ).PHP_EOL; } file_put_contents($fileName, implode('', $fileContent)); @@ -525,7 +525,7 @@ public function testFileFromStringReadAndWrite() $fileContent = []; for ($i = 0; $i < 100; $i++) { - $fileContent[] = $i."\t".($i >= 50 ? 'string' : 'some').PHP_EOL; + $fileContent[] = $i."\t".($i >= 50 ? 'string' : 'some' ).PHP_EOL; } $file = new FileFromString(implode('', $fileContent)); diff --git a/tests/Support/ServerTrait.php b/tests/Support/ServerTrait.php index f7f99e2..dbbfa77 100644 --- a/tests/Support/ServerTrait.php +++ b/tests/Support/ServerTrait.php @@ -8,6 +8,6 @@ trait ServerTrait { protected function getServer($database = 'default', $username = 'default', $password = null) : Server { - return new Server(getenv('CH_HOST'), getenv('CH_PORT'), $database, $username, $password); + return new Server(getenv('CH_HOST'), getenv('CH_PORT'), $database, $username, $password); } -} \ No newline at end of file +}