diff --git a/composer.json b/composer.json index f78f31e..700bd39 100644 --- a/composer.json +++ b/composer.json @@ -18,18 +18,19 @@ } }, "require": { - "php": "^7.1|^8.0", - "guzzlehttp/guzzle": "^6.0|^7.0" + "php": "^8.0", + "guzzlehttp/guzzle": "^7.0" }, "bin": [ "bin/ccat_linux", "bin/ccat_darwin" ], "require-dev": { - "mockery/mockery": "^0.9|^1.4", - "phpunit/phpunit": "^7.0|^8.0|^9.0", - "php-coveralls/php-coveralls": "^2.2", - "phpunit/phpcov": "^5.0|^6.0|^7.0|^8.0" + "mockery/mockery": "^1.6", + "phpunit/phpunit": "^9.0", + "php-coveralls/php-coveralls": "^2.5", + "phpunit/phpcov": "^8.0", + "ext-zlib": "*" }, "scripts": { "test": "phpunit --coverage-text --colors=never" diff --git a/phpunit.xml b/phpunit.xml index 3862a07..fd4bcf2 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,27 +1,30 @@ - + stopOnFailure="false" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" +> + + + src/ + + + + + tests - - - src/ - - - - - + diff --git a/src/Client.php b/src/Client.php index 4725da6..f62cc5a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,6 +4,8 @@ use Tinderbox\Clickhouse\Common\File; use Tinderbox\Clickhouse\Common\Format; +use Tinderbox\Clickhouse\Exceptions\ClusterException; +use Tinderbox\Clickhouse\Exceptions\ServerProviderException; use Tinderbox\Clickhouse\Interfaces\FileInterface; use Tinderbox\Clickhouse\Interfaces\TransportInterface; use Tinderbox\Clickhouse\Query\Result; @@ -15,63 +17,45 @@ class Client { /** - * Http transport which provides http requests to server. - * - * @var TransportInterface - */ - protected $transport; - - /** - * Server provider. - * - * @var ServerProvider + * Cluster name. */ - protected $serverProvider; + protected ?string $clusterName = null; /** - * Cluster name. - * - * @var string + * Http transport which provides http requests to server. */ - protected $clusterName; + protected ?TransportInterface $transport = null; /** * Server hostname. - * - * @var string */ - protected $serverHostname; + protected null|string|\Closure $serverHostname = null; /** * Client constructor. - * - * @param \Tinderbox\Clickhouse\ServerProvider $serverProvider - * @param \Tinderbox\Clickhouse\Interfaces\TransportInterface|null $transport */ public function __construct( - ServerProvider $serverProvider, - TransportInterface $transport = null + /** + * Server Provider. + */ + protected ServerProvider $serverProvider, + ?TransportInterface $transport = null ) { - $this->serverProvider = $serverProvider; $this->setTransport($transport); } /** * Creates default http transport. - * - * @return HttpTransport */ - protected function createTransport() + protected function createTransport(): HttpTransport { return new HttpTransport(); } /** * Sets transport. - * - * @param \Tinderbox\Clickhouse\Interfaces\TransportInterface|null $transport */ - protected function setTransport(TransportInterface $transport = null) + protected function setTransport(?TransportInterface $transport = null): void { if (is_null($transport)) { $this->transport = $this->createTransport(); @@ -82,8 +66,6 @@ protected function setTransport(TransportInterface $transport = null) /** * Returns transport. - * - * @return TransportInterface */ protected function getTransport(): TransportInterface { @@ -92,12 +74,8 @@ protected function getTransport(): TransportInterface /** * Client will use servers from specified cluster. - * - * @param string|null $cluster - * - * @return $this */ - public function onCluster(?string $cluster) + public function onCluster(?string $cluster): self { $this->clusterName = $cluster; $this->serverHostname = null; @@ -107,8 +85,6 @@ public function onCluster(?string $cluster) /** * Returns current cluster name. - * - * @return null|string */ protected function getClusterName(): ?string { @@ -117,12 +93,8 @@ protected function getClusterName(): ?string /** * Client will use specified server. - * - * @param string $serverHostname - * - * @return $this */ - public function using(string $serverHostname) + public function using(string $serverHostname): self { $this->serverHostname = $serverHostname; @@ -131,10 +103,8 @@ public function using(string $serverHostname) /** * Client will return random server on each query. - * - * @return $this */ - public function usingRandomServer() + public function usingRandomServer(): self { $this->serverHostname = function () { if ($this->isOnCluster()) { @@ -149,12 +119,8 @@ public function usingRandomServer() /** * Client will use server with tag as server for queries. - * - * @var string - * - * @return $this */ - public function usingServerWithTag(string $tag) + public function usingServerWithTag(string $tag): self { $this->serverHostname = function () use ($tag) { if ($this->isOnCluster()) { @@ -180,7 +146,8 @@ protected function isOnCluster(): bool /** * Returns server to perform request. * - * @return Server + * @throws ServerProviderException + * @throws ClusterException */ public function getServer(): Server { @@ -223,11 +190,10 @@ public function getServer(): Server * * $client->select('select * from table where column = ?', [1]); * - * @param string $query * @param FileInterface[] $files - * @param array $settings * - * @return \Tinderbox\Clickhouse\Query\Result + * @throws ServerProviderException + * @throws ClusterException */ public function readOne(string $query, array $files = [], array $settings = []): Result { @@ -241,10 +207,10 @@ public function readOne(string $query, array $files = [], array $settings = []): /** * Performs batch of select queries. * - * @param array $queries - * @param int $concurrency Max concurrency requests + * @param int $concurrency Max concurrency requests * - * @return array + * @throws ServerProviderException + * @throws ClusterException */ public function read(array $queries, int $concurrency = 5): array { @@ -260,11 +226,8 @@ public function read(array $queries, int $concurrency = 5): array /** * Performs insert or simple statement query. * - * @param string $query - * @param array $files - * @param array $settings - * - * @return bool + * @throws ServerProviderException + * @throws ClusterException */ public function writeOne(string $query, array $files = [], array $settings = []): bool { @@ -279,11 +242,6 @@ public function writeOne(string $query, array $files = [], array $settings = []) /** * Performs batch of insert or simple statement queries. - * - * @param array $queries - * @param int $concurrency - * - * @return array */ public function write(array $queries, int $concurrency = 5): array { @@ -299,23 +257,19 @@ public function write(array $queries, int $concurrency = 5): array /** * Performs async insert queries using local csv or tsv files. * - * @param string $table - * @param array $columns - * @param array $files - * @param string|null $format - * @param array $settings - * @param int $concurrency Max concurrency requests + * @param int $concurrency Max concurrency requests * - * @return array + * @throws ServerProviderException + * @throws ClusterException */ public function writeFiles( string $table, array $columns, array $files, - string $format = Format::TSV, + ?string $format = Format::TSV, array $settings = [], int $concurrency = 5 - ) { + ): array { $sql = 'INSERT INTO '.$table.' ('.implode(', ', $columns).') FORMAT '.strtoupper($format); foreach ($files as $i => $file) { @@ -331,13 +285,6 @@ public function writeFiles( /** * Creates query instance from specified arguments. - * - * @param \Tinderbox\Clickhouse\Server $server - * @param string $sql - * @param array $files - * @param array $settings - * - * @return \Tinderbox\Clickhouse\Query */ protected function createQuery( Server $server, @@ -351,9 +298,8 @@ protected function createQuery( /** * Parses query array and returns query instance. * - * @param array $query - * - * @return \Tinderbox\Clickhouse\Query + * @throws ServerProviderException + * @throws ClusterException */ protected function guessQuery(array $query): Query { diff --git a/src/Cluster.php b/src/Cluster.php index 3873b21..d2add55 100644 --- a/src/Cluster.php +++ b/src/Cluster.php @@ -9,38 +9,32 @@ */ class Cluster { - /** - * Cluster name like in configuration file. - * - * @var string - */ - protected $name; - /** * Servers in cluster. * - * @var \Tinderbox\Clickhouse\Server[] + * @var Server[] */ - protected $servers = []; + protected array $servers = []; /** * Servers in cluster by tags. * - * @var \Tinderbox\Clickhouse\Server[][] + * @var Server[][] */ - protected $serversByTags = []; + protected array $serversByTags = []; /** * Cluster constructor. * - * @param string $name - * @param array $servers - * * @throws ClusterException */ - public function __construct(string $name, array $servers) - { - $this->name = $name; + public function __construct( + /** + * Cluster name like in configuration file. + */ + protected string $name, + array $servers + ) { $this->addServers($servers); } @@ -49,9 +43,7 @@ public function __construct(string $name, array $servers) * * @param array $servers Each server can be provided as array or Server instance * - * @throws \Tinderbox\Clickhouse\Exceptions\ClusterException - * - * @return \Tinderbox\Clickhouse\Cluster + * @throws ClusterException */ public function addServers(array $servers): self { @@ -81,12 +73,9 @@ public function addServers(array $servers): self /** * Pushes one server to cluster. * - * @param string $hostname - * @param \Tinderbox\Clickhouse\Server $server - * - * @throws \Tinderbox\Clickhouse\Exceptions\ClusterException + * @throws ClusterException */ - public function addServer(string $hostname, Server $server) + public function addServer(string $hostname, Server $server): void { if (isset($this->servers[$hostname])) { throw ClusterException::serverHostnameDuplicate($hostname); @@ -104,7 +93,7 @@ public function addServer(string $hostname, Server $server) /** * Returns servers in cluster. * - * @return \Tinderbox\Clickhouse\Server[] + * @return Server[] */ public function getServers(): array { @@ -114,11 +103,9 @@ public function getServers(): array /** * Returns servers in cluster by tag. * - * @param string $tag - * * @throws ClusterException * - * @return \Tinderbox\Clickhouse\Server[] + * @return Server[] */ public function getServersByTag(string $tag): array { @@ -132,11 +119,7 @@ public function getServersByTag(string $tag): array /** * Returns server by specified hostname. * - * @param string $hostname - * - * @throws \Tinderbox\Clickhouse\Exceptions\ClusterException - * - * @return \Tinderbox\Clickhouse\Server + * @throws ClusterException */ public function getServerByHostname(string $hostname): Server { @@ -149,8 +132,6 @@ public function getServerByHostname(string $hostname): Server /** * Returns cluster name. - * - * @return string */ public function getName(): string { diff --git a/src/Common/AbstractFile.php b/src/Common/AbstractFile.php index c925cde..c64ff49 100644 --- a/src/Common/AbstractFile.php +++ b/src/Common/AbstractFile.php @@ -6,15 +6,11 @@ abstract class AbstractFile { /** * Source file. - * - * @var string */ - protected $source; + protected string $source; /** * File constructor. - * - * @param string $source */ public function __construct(string $source) { @@ -23,8 +19,6 @@ public function __construct(string $source) /** * Returns full path to source file. - * - * @return string */ public function getSource(): string { diff --git a/src/Common/Sanitizer.php b/src/Common/Sanitizer.php index bf1779b..d9fa81c 100644 --- a/src/Common/Sanitizer.php +++ b/src/Common/Sanitizer.php @@ -9,12 +9,8 @@ abstract class Sanitizer { /** * Escapes value. - * - * @param mixed $value - * - * @return mixed */ - public static function escape($value) + public static function escape(mixed $value): mixed { if (is_string($value)) { $value = addslashes($value); diff --git a/src/Common/ServerOptions.php b/src/Common/ServerOptions.php index ee97fde..d81cf54 100644 --- a/src/Common/ServerOptions.php +++ b/src/Common/ServerOptions.php @@ -9,24 +9,16 @@ class ServerOptions { /** * Protocol. - * - * @var string */ - protected $protocol = 'http'; + protected string $protocol = 'http'; /** * Tags. - * - * @var array */ - protected $tags = []; + protected array $tags = []; /** * Sets protocol. - * - * @param string $protocol - * - * @return ServerOptions */ public function setProtocol(string $protocol): self { @@ -37,8 +29,6 @@ public function setProtocol(string $protocol): self /** * Returns protocol. - * - * @return string */ public function getProtocol(): string { @@ -47,10 +37,6 @@ public function getProtocol(): string /** * Set tags. - * - * @param array $tags - * - * @return ServerOptions */ public function setTags(array $tags): self { @@ -65,10 +51,6 @@ public function setTags(array $tags): self /** * Adds tag. - * - * @param string $tag - * - * @return ServerOptions */ public function addTag(string $tag): self { @@ -79,8 +61,6 @@ public function addTag(string $tag): self /** * Returns tags. - * - * @return array */ public function getTags(): array { diff --git a/src/Common/TempTable.php b/src/Common/TempTable.php index 9aad633..be66ab0 100644 --- a/src/Common/TempTable.php +++ b/src/Common/TempTable.php @@ -12,41 +12,28 @@ class TempTable implements FileInterface { /** * Table name to use in query in where section. - * - * @var string */ - protected $name; + protected string $name; /** * Table structure to map data in file on table. - * - * @var array */ - protected $structure = []; + protected array $structure = []; /** * Format. - * - * @var string */ - protected $format; + protected string $format; /** * Source. - * - * @var string|FileInterface */ - protected $source; + protected string|FileInterface $source; /** * TempTable constructor. - * - * @param string $name - * @param string|FileInterface $source - * @param array $structure - * @param string $format */ - public function __construct(string $name, $source, array $structure, string $format = Format::CSV) + public function __construct(string $name, string|FileInterface $source, array $structure, string $format = Format::CSV) { $this->name = $name; $this->structure = $structure; @@ -55,7 +42,7 @@ public function __construct(string $name, $source, array $structure, string $for $this->setSource($source); } - protected function setSource($source) + protected function setSource($source): void { if (is_scalar($source)) { $source = new File($source); @@ -66,8 +53,6 @@ protected function setSource($source) /** * Returns table name. - * - * @return string */ public function getName(): string { @@ -76,8 +61,6 @@ public function getName(): string /** * Returns table structure. - * - * @return array */ public function getStructure(): array { @@ -86,8 +69,6 @@ public function getStructure(): array /** * Returns format. - * - * @return string */ public function getFormat(): string { diff --git a/src/Exceptions/ClientException.php b/src/Exceptions/ClientException.php index 8e194b7..7ed0964 100644 --- a/src/Exceptions/ClientException.php +++ b/src/Exceptions/ClientException.php @@ -7,12 +7,12 @@ */ class ClientException extends \Exception { - public static function clusterIsNotProvided() + public static function clusterIsNotProvided(): static { return new static('Can not execute query using specified server because cluster is not provided'); } - public static function clusterExists(string $name) + public static function clusterExists(string $name): static { return new static('Can not add cluster with name ['.$name.'], because it already added'); } diff --git a/src/Exceptions/ClusterException.php b/src/Exceptions/ClusterException.php index a0ba037..dd5cc21 100644 --- a/src/Exceptions/ClusterException.php +++ b/src/Exceptions/ClusterException.php @@ -7,27 +7,27 @@ */ class ClusterException extends \Exception { - public static function missingServerHostname() + public static function missingServerHostname(): static { return new static('Each server in cluster must have specified hostname as an array key'); } - public static function serverHostnameDuplicate($hostname) + public static function serverHostnameDuplicate($hostname): static { return new static('Hostname ['.$hostname.'] already provided'); } - public static function serverNotFound($hostname) + public static function serverNotFound($hostname): static { return new static('Server with hostname ['.$hostname.'] is not found in cluster'); } - public static function tagNotFound($tag) + public static function tagNotFound($tag): static { return new static('There are no servers with tag ['.$tag.'] in cluster'); } - public static function serverNotFoundByTag($tag, $hostname) + public static function serverNotFoundByTag($tag, $hostname): static { return new static('Server with hostname ['.$hostname.'] and tag ['.$tag.'] is not found in cluster'); } diff --git a/src/Exceptions/QueryStatisticException.php b/src/Exceptions/QueryStatisticException.php index 177dd17..c8fbe24 100644 --- a/src/Exceptions/QueryStatisticException.php +++ b/src/Exceptions/QueryStatisticException.php @@ -7,7 +7,7 @@ */ class QueryStatisticException extends \Exception { - public static function propertyNotExists($name) + public static function propertyNotExists($name): static { return new static('Query statistic has no property '.$name); } diff --git a/src/Exceptions/ResultException.php b/src/Exceptions/ResultException.php index 13e2eae..65ffb3d 100644 --- a/src/Exceptions/ResultException.php +++ b/src/Exceptions/ResultException.php @@ -7,12 +7,12 @@ */ class ResultException extends \Exception { - public static function propertyNotExists($name) + public static function propertyNotExists($name): static { return new static('Query result has no property '.$name); } - public static function isReadonly() + public static function isReadonly(): static { return new static('Query result is read-only'); } diff --git a/src/Exceptions/ServerProviderException.php b/src/Exceptions/ServerProviderException.php index ac56f85..33ba034 100644 --- a/src/Exceptions/ServerProviderException.php +++ b/src/Exceptions/ServerProviderException.php @@ -7,32 +7,32 @@ */ class ServerProviderException extends \Exception { - public static function clusterExists(string $name) + public static function clusterExists(string $name): static { return new static('Can not add cluster with name ['.$name.'], because it already added'); } - public static function clusterNotFound(string $name) + public static function clusterNotFound(string $name): static { return new static('Can not find cluster with name ['.$name.']'); } - public static function serverHostnameDuplicate($hostname) + public static function serverHostnameDuplicate($hostname): static { return new static('Server with hostname ['.$hostname.'] already provided'); } - public static function serverHostnameNotFound($hostname) + public static function serverHostnameNotFound($hostname): static { return new static('Can not find server with hostname ['.$hostname.']'); } - public static function serverTagNotFound($tag) + public static function serverTagNotFound($tag): static { return new static('Can not find servers with tag ['.$tag.']'); } - public static function serverHostnameNotFoundForTag($tag, $hostname) + public static function serverHostnameNotFoundForTag($tag, $hostname): static { return new static('Can not find servers with hostname ['.$hostname.'] and tag ['.$tag.']'); } diff --git a/src/Exceptions/TransportException.php b/src/Exceptions/TransportException.php index 2094409..3da772e 100644 --- a/src/Exceptions/TransportException.php +++ b/src/Exceptions/TransportException.php @@ -11,12 +11,12 @@ */ class TransportException extends \Exception { - public static function connectionError(Server $server, $reasonMessage) + public static function connectionError(Server $server, $reasonMessage): static { return new static('Can\'t connect to the server ['.$server->getHost().':'.$server->getPort().'] with error: ['.$reasonMessage.']'); } - public static function serverReturnedError($exception, Query $query) + public static function serverReturnedError($exception, Query $query): static { if ($exception instanceof RequestException) { $error = $exception->getResponse()->getBody()->getContents(); @@ -29,7 +29,7 @@ public static function serverReturnedError($exception, Query $query) return new static($errorString); } - public static function malformedResponseFromServer($response) + public static function malformedResponseFromServer($response): static { return new static('Malformed response from server: '.$response); } diff --git a/src/Interfaces/TransportInterface.php b/src/Interfaces/TransportInterface.php index a1ceb47..bb37c37 100644 --- a/src/Interfaces/TransportInterface.php +++ b/src/Interfaces/TransportInterface.php @@ -13,21 +13,15 @@ interface TransportInterface /** * Executes queries which should not return result. * - * Queries runs asyn + * Queries runs async * * @param Query[] $queries - * @param int $concurrency - * - * @return array */ public function write(array $queries, int $concurrency = 5): array; /** * Executes queries which returns result of any select expression. * - * @param array $queries - * @param int $concurrency - * * @return Result[] */ public function read(array $queries, int $concurrency = 5): array; diff --git a/src/Query.php b/src/Query.php index a3d5863..fcaa725 100644 --- a/src/Query.php +++ b/src/Query.php @@ -7,54 +7,31 @@ */ class Query { - /** - * SQL Query. - * - * @var string - */ - protected $query; - - /** - * Files attached to query. - * - * @var array - */ - protected $files = []; - - /** - * Server to process query. - * - * @var \Tinderbox\Clickhouse\Server - */ - protected $server; - - /** - * Query settings. - * - * @var array - */ - protected $settings = []; - /** * Query constructor. - * - * @param \Tinderbox\Clickhouse\Server $server - * @param string $query - * @param array $files - * @param array $settings */ - public function __construct(Server $server, string $query, array $files = [], array $settings = []) - { - $this->server = $server; - $this->query = $query; - $this->files = $files; - $this->settings = $settings; + public function __construct( + /** + * Server to process query. + */ + protected Server $server, + /** + * SQL Query. + */ + protected string $query, + /** + * Files attached to query. + */ + protected array $files = [], + /** + * Query settings. + */ + protected array $settings = [] + ) { } /** * Returns SQL query. - * - * @return string */ public function getQuery(): string { @@ -63,8 +40,6 @@ public function getQuery(): string /** * Returns files attached to query. - * - * @return array */ public function getFiles(): array { @@ -73,8 +48,6 @@ public function getFiles(): array /** * Returns server to process query. - * - * @return \Tinderbox\Clickhouse\Server */ public function getServer(): Server { @@ -83,8 +56,6 @@ public function getServer(): Server /** * Returns settings. - * - * @return array */ public function getSettings(): array { diff --git a/src/Query/QueryStatistic.php b/src/Query/QueryStatistic.php index fd0c3cf..fbe9390 100644 --- a/src/Query/QueryStatistic.php +++ b/src/Query/QueryStatistic.php @@ -10,49 +10,31 @@ * 1. rows - number of rows read by server * 2. bytes - number of bytes read by server * 3. time - query execution time in seconds - * - * @property int $rows - * @property int $bytes - * @property float $time - * @property int $rowsBeforeLimitAtLeast */ class QueryStatistic { /** * Number of rows read. - * - * @var int */ - protected $rows; + protected int $rows; /** * Number of bytes read. - * - * @var int */ - protected $bytes; + protected int $bytes; /** * Query execution time in seconds. - * - * @var float */ - protected $time; + protected float $time; /** * Rows before limit at least. - * - * @var null|int */ - protected $rowsBeforeLimitAtLeast; + protected ?int $rowsBeforeLimitAtLeast; /** * QueryStatistic constructor. - * - * @param int $rows - * @param int $bytes - * @param float $time - * @param null|int $rowsBeforeLimitAtLeast */ public function __construct(int $rows, int $bytes, float $time, ?int $rowsBeforeLimitAtLeast = null) { @@ -64,8 +46,6 @@ public function __construct(int $rows, int $bytes, float $time, ?int $rowsBefore /** * Returns number of read rows. - * - * @return int */ public function getRows(): int { @@ -74,8 +54,6 @@ public function getRows(): int /** * Returns number of read bytes. - * - * @return int */ public function getBytes(): int { @@ -84,8 +62,6 @@ public function getBytes(): int /** * Returns query execution time. - * - * @return float */ public function getTime(): float { @@ -94,8 +70,6 @@ public function getTime(): float /** * Returns rows before limit at least. - * - * @return int|null */ public function getRowsBeforeLimitAtLeast(): ?int { @@ -105,13 +79,9 @@ public function getRowsBeforeLimitAtLeast(): ?int /** * Getter to simplify access to rows, bytes and time. * - * @param string $name - * - * @throws \Tinderbox\Clickhouse\Exceptions\QueryStatisticException - * - * @return mixed + * @throws QueryStatisticException */ - public function __get($name) + public function __get(string $name): mixed { $method = 'get'.ucfirst($name); diff --git a/src/Query/Result.php b/src/Query/Result.php index 9b40440..234e605 100644 --- a/src/Query/Result.php +++ b/src/Query/Result.php @@ -9,47 +9,31 @@ * Query result. * * Container for request results and statistic - * - * @property Query $query - * @property array $rows - * @property QueryStatistic $statistic */ class Result implements \ArrayAccess, \Iterator, \Countable { /** * Query execution statistic. - * - * @var \Tinderbox\Clickhouse\Query\QueryStatistic */ - protected $statistic; + protected QueryStatistic $statistic; /** * Result of query execution. - * - * @var array */ - protected $rows; + protected array $rows; /** * Query which was executed to get this result. - * - * @var Query */ - protected $query; + protected Query $query; /** * Current index for Iterator interface. - * - * @var int */ - protected $current = 0; + protected int $current = 0; /** * Result constructor. - * - * @param Query $query - * @param array $rows - * @param \Tinderbox\Clickhouse\Query\QueryStatistic $statistic */ public function __construct(Query $query, array $rows, QueryStatistic $statistic) { @@ -60,30 +44,24 @@ public function __construct(Query $query, array $rows, QueryStatistic $statistic /** * Sets query. - * - * @param Query $query */ - protected function setQuery(Query $query) + protected function setQuery(Query $query): void { $this->query = $query; } /** * Sets statistic. - * - * @param \Tinderbox\Clickhouse\Query\QueryStatistic $statistic */ - protected function setStatistic(QueryStatistic $statistic) + protected function setStatistic(QueryStatistic $statistic): void { $this->statistic = $statistic; } /** * Sets rows. - * - * @param array $rows */ - protected function setRows(array $rows) + protected function setRows(array $rows): void { $this->rows = $rows; } @@ -110,8 +88,6 @@ public function getQuery(): Query /** * Returns statistic. - * - * @return \Tinderbox\Clickhouse\Query\QueryStatistic */ public function getStatistic(): QueryStatistic { @@ -121,13 +97,9 @@ public function getStatistic(): QueryStatistic /** * Getter to simplify access to rows and statistic. * - * @param string $name - * - * @throws \Tinderbox\Clickhouse\Exceptions\ResultException - * - * @return mixed + * @throws ResultException */ - public function __get($name) + public function __get(string $name): mixed { $method = 'get'.ucfirst($name); @@ -138,50 +110,48 @@ public function __get($name) throw ResultException::propertyNotExists($name); } - /* - * ArrayAccess - */ - - public function offsetExists($offset) + public function offsetExists($offset): bool { return isset($this->rows[$offset]); } - public function offsetGet($offset) + public function offsetGet($offset): mixed { return $this->rows[$offset]; } - public function offsetSet($offset, $value) + /** + * @throws ResultException + */ + public function offsetSet($offset, $value): void { throw ResultException::isReadonly(); } - public function offsetUnset($offset) + /** + * @throws ResultException + */ + public function offsetUnset($offset): void { throw ResultException::isReadonly(); } - /* - * Iterator - */ - - public function current() + public function current(): mixed { return $this->rows[$this->current]; } - public function next() + public function next(): void { $this->current++; } - public function key() + public function key(): mixed { return $this->current; } - public function valid() + public function valid(): bool { return isset($this->rows[$this->current]); } @@ -191,11 +161,7 @@ public function rewind() $this->current = 0; } - /* - * Countable - */ - - public function count() + public function count(): int { return count($this->rows); } diff --git a/src/Server.php b/src/Server.php index 7c49306..de8ab91 100644 --- a/src/Server.php +++ b/src/Server.php @@ -11,55 +11,36 @@ class Server { /** * Host. - * - * @var string */ - protected $host; + protected string $host; /** * Port. - * - * @var string */ - protected $port; + protected string $port; /** * Database. - * - * @var string */ - protected $database; + protected ?string $database; /** * Username. - * - * @var string */ - protected $username; + protected ?string $username; /** * Password. - * - * @var string */ - protected $password; + protected ?string $password; /** * Options. - * - * @var ServerOptions */ - protected $options; + protected ?ServerOptions $options; /** * Server constructor. - * - * @param string $host - * @param string $port - * @param string $database - * @param string|null $username - * @param string|null $password - * @param \Tinderbox\Clickhouse\Common\ServerOptions|null $options */ public function __construct( string $host, @@ -67,7 +48,7 @@ public function __construct( ?string $database = 'default', ?string $username = null, ?string $password = null, - ServerOptions $options = null + ?ServerOptions $options = null ) { $this->setHost($host); $this->setPort($port); @@ -79,10 +60,6 @@ public function __construct( /** * Sets host. - * - * @param string $host - * - * @return \Tinderbox\Clickhouse\Server */ public function setHost(string $host): self { @@ -93,8 +70,6 @@ public function setHost(string $host): self /** * Returns host. - * - * @return string */ public function getHost(): string { @@ -103,10 +78,6 @@ public function getHost(): string /** * Sets port. - * - * @param string $port - * - * @return \Tinderbox\Clickhouse\Server */ public function setPort(string $port): self { @@ -117,8 +88,6 @@ public function setPort(string $port): self /** * Returns port. - * - * @return string */ public function getPort(): string { @@ -127,12 +96,8 @@ public function getPort(): string /** * Sets database. - * - * @param string|null $database - * - * @return \Tinderbox\Clickhouse\Server */ - public function setDatabase(string $database = null): self + public function setDatabase(?string $database = null): self { $this->database = $database; @@ -141,8 +106,6 @@ public function setDatabase(string $database = null): self /** * Returns database. - * - * @return null|string */ public function getDatabase(): ?string { @@ -151,12 +114,8 @@ public function getDatabase(): ?string /** * Sets username. - * - * @param string|null $username - * - * @return \Tinderbox\Clickhouse\Server */ - public function setUsername(string $username = null): self + public function setUsername(?string $username = null): self { $this->username = $username; @@ -165,8 +124,6 @@ public function setUsername(string $username = null): self /** * Returns username. - * - * @return null|string */ public function getUsername(): ?string { @@ -175,12 +132,8 @@ public function getUsername(): ?string /** * Sets password. - * - * @param string|null $password - * - * @return \Tinderbox\Clickhouse\Server */ - public function setPassword(string $password = null): self + public function setPassword(?string $password = null): self { $this->password = $password; @@ -189,8 +142,6 @@ public function setPassword(string $password = null): self /** * Returns password. - * - * @return null|string */ public function getPassword(): ?string { @@ -201,12 +152,8 @@ public function getPassword(): ?string * Sets options. * * If no options provided, will use default options - * - * @param \Tinderbox\Clickhouse\Common\ServerOptions|null $options - * - * @return \Tinderbox\Clickhouse\Server */ - public function setOptions(ServerOptions $options = null): self + public function setOptions(?ServerOptions $options = null): self { if (is_null($options)) { return $this->setDefaultOptions(); @@ -219,8 +166,6 @@ public function setOptions(ServerOptions $options = null): self /** * Sets default options. - * - * @return \Tinderbox\Clickhouse\Server */ protected function setDefaultOptions(): self { @@ -231,8 +176,6 @@ protected function setDefaultOptions(): self /** * Returns options. - * - * @return \Tinderbox\Clickhouse\Common\ServerOptions */ public function getOptions(): ServerOptions { diff --git a/src/ServerProvider.php b/src/ServerProvider.php index d279a18..1954d7a 100644 --- a/src/ServerProvider.php +++ b/src/ServerProvider.php @@ -2,6 +2,7 @@ namespace Tinderbox\Clickhouse; +use Tinderbox\Clickhouse\Exceptions\ClusterException; use Tinderbox\Clickhouse\Exceptions\ServerProviderException; /** @@ -14,37 +15,40 @@ class ServerProvider * * @var Cluster[] */ - protected $clusters = []; + protected array $clusters = []; /** * Servers. * * @var Server[] */ - protected $servers = []; + protected array $servers = []; /** * Servers by tags. * * @var Server[][] */ - protected $serversByTags = []; + protected array $serversByTags = []; /** * Current server to perform queries. * * @var Server */ - protected $currentServer; + protected Server $currentServer; /** * Current cluster to select server. * * @var Cluster */ - protected $currentCluster; + protected Cluster $currentCluster; - public function addCluster(Cluster $cluster) + /** + * @throws ServerProviderException + */ + public function addCluster(Cluster $cluster): self { $clusterName = $cluster->getName(); @@ -57,7 +61,10 @@ public function addCluster(Cluster $cluster) return $this; } - public function addServer(Server $server) + /** + * @throws ServerProviderException + */ + public function addServer(Server $server): self { $serverHostname = $server->getHost(); @@ -76,11 +83,17 @@ public function addServer(Server $server) return $this; } + /** + * @throws ServerProviderException + */ public function getRandomServer(): Server { - return $this->getServer(array_rand($this->servers, 1)); + return $this->getServer(array_rand($this->servers)); } + /** + * @throws ServerProviderException + */ public function getRandomServerWithTag(string $tag): Server { if (!isset($this->serversByTags[$tag])) { @@ -90,30 +103,45 @@ public function getRandomServerWithTag(string $tag): Server return $this->getServer(array_rand($this->serversByTags[$tag], 1)); } + /** + * @throws ServerProviderException + * @throws ClusterException + */ public function getRandomServerFromCluster(string $cluster): Server { $cluster = $this->getCluster($cluster); - $randomServerHostname = array_rand($cluster->getServers(), 1); + $randomServerHostname = array_rand($cluster->getServers()); return $cluster->getServerByHostname($randomServerHostname); } + /** + * @throws ServerProviderException + * @throws ClusterException + */ public function getRandomServerFromClusterByTag(string $cluster, string $tag): Server { $cluster = $this->getCluster($cluster); - $randomServerHostname = array_rand($cluster->getServersByTag($tag), 1); + $randomServerHostname = array_rand($cluster->getServersByTag($tag)); return $cluster->getServerByHostname($randomServerHostname); } - public function getServerFromCluster(string $cluster, string $serverHostname) + /** + * @throws ServerProviderException + * @throws ClusterException + */ + public function getServerFromCluster(string $cluster, string $serverHostname): Server { $cluster = $this->getCluster($cluster); return $cluster->getServerByHostname($serverHostname); } + /** + * @throws ServerProviderException + */ public function getServer(string $serverHostname): Server { if (!isset($this->servers[$serverHostname])) { @@ -123,6 +151,9 @@ public function getServer(string $serverHostname): Server return $this->servers[$serverHostname]; } + /** + * @throws ServerProviderException + */ public function getCluster(string $cluster): Cluster { if (!isset($this->clusters[$cluster])) { diff --git a/src/Support/CcatStream.php b/src/Support/CcatStream.php index 081b3fc..8e87f8d 100644 --- a/src/Support/CcatStream.php +++ b/src/Support/CcatStream.php @@ -2,27 +2,31 @@ namespace Tinderbox\Clickhouse\Support; -use GuzzleHttp\Psr7\NoSeekStream; +use GuzzleHttp\Psr7\StreamDecoratorTrait; use Psr\Http\Message\StreamInterface; -class CcatStream extends NoSeekStream +class CcatStream implements StreamInterface { - protected $process; + use StreamDecoratorTrait; - /** - * CcatStream constructor. - * - * @param \Psr\Http\Message\StreamInterface $stream - * @param $process - */ - public function __construct(StreamInterface $stream, $process) + public function __construct( + private StreamInterface $stream, + protected mixed $process + ) { + } + + public function seek($offset, $whence = SEEK_SET): void { - parent::__construct($stream); + throw new \RuntimeException('Cannot seek a NoSeekStream'); + } - $this->process = $process; + public function isSeekable(): bool + { + return false; } - public function getSize() + public function getSize(): ?int { + return null; } } diff --git a/src/Transport/HttpTransport.php b/src/Transport/HttpTransport.php index efd01cb..87b207e 100644 --- a/src/Transport/HttpTransport.php +++ b/src/Transport/HttpTransport.php @@ -3,11 +3,13 @@ namespace Tinderbox\Clickhouse\Transport; use GuzzleHttp\Client; +use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Pool; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; use Psr\Http\Message\ResponseInterface; +use Throwable; use Tinderbox\Clickhouse\Common\TempTable; use Tinderbox\Clickhouse\Exceptions\TransportException; use Tinderbox\Clickhouse\Interfaces\FileInterface; @@ -24,10 +26,8 @@ class HttpTransport implements TransportInterface { /** * GuzzleClient. - * - * @var Client */ - protected $httpClient; + protected Client $httpClient; /** * Array with three keys (read, write and deflate) with guzzle options for corresponding requests. @@ -43,18 +43,13 @@ class HttpTransport implements TransportInterface * ], * 'deflate' => true * ] - * - * @var array */ - private $options; + private array $options; /** * HttpTransport constructor. - * - * @param Client $client - * @param array $options */ - public function __construct(Client $client = null, array $options = []) + public function __construct(?Client $client = null, array $options = []) { $this->setClient($client); @@ -63,8 +58,6 @@ public function __construct(Client $client = null, array $options = []) /** * Returns flag to enable / disable queries and data compression. - * - * @return bool */ protected function isDeflateEnabled(): bool { @@ -73,10 +66,8 @@ protected function isDeflateEnabled(): bool /** * Returns default headers for requests. - * - * @return array */ - protected function getHeaders() + protected function getHeaders(): array { $headers = [ 'Accept-Encoding' => 'gzip', @@ -91,10 +82,8 @@ protected function getHeaders() /** * Sets Guzzle client. - * - * @param Client|null $client */ - protected function setClient(Client $client = null) + protected function setClient(?Client $client = null): void { if (is_null($client)) { $this->httpClient = $this->createHttpClient(); @@ -106,7 +95,7 @@ protected function setClient(Client $client = null) /** * Creates Guzzle client. */ - protected function createHttpClient() + protected function createHttpClient(): Client { return new Client(); } @@ -114,12 +103,7 @@ protected function createHttpClient() /** * Executes write queries. * - * @param array $queries - * @param int $concurrency - * - * @throws \Throwable - * - * @return array + * @throws Throwable */ public function write(array $queries, int $concurrency = 5): array { @@ -177,7 +161,7 @@ public function write(array $queries, int $concurrency = 5): array try { $promise->wait(); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { foreach ($openedStreams as $openedStream) { $openedStream->close(); } @@ -197,6 +181,9 @@ public function write(array $queries, int $concurrency = 5): array return $result; } + /** + * @throws Throwable + */ public function read(array $queries, int $concurrency = 5): array { $openedStreams = []; @@ -265,7 +252,7 @@ public function read(array $queries, int $concurrency = 5): array try { $promise->wait(); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { foreach ($openedStreams as $openedStream) { $openedStream->close(); } @@ -284,12 +271,8 @@ public function read(array $queries, int $concurrency = 5): array /** * Parse temp table data to append it to request. - * - * @param \Tinderbox\Clickhouse\Common\TempTable $table - * - * @return array */ - protected function getTempTableQueryParams(TempTable $table) + protected function getTempTableQueryParams(TempTable $table): array { list($structure, $withColumns) = $this->assembleTempTableStructure($table); @@ -301,12 +284,8 @@ protected function getTempTableQueryParams(TempTable $table) /** * Assembles string from TempTable structure. - * - * @param \Tinderbox\Clickhouse\Common\TempTable $table - * - * @return string */ - protected function assembleTempTableStructure(TempTable $table) + protected function assembleTempTableStructure(TempTable $table): array { $structure = $table->getStructure(); $withColumns = true; @@ -327,23 +306,21 @@ protected function assembleTempTableStructure(TempTable $table) /** * Determines the reason why request was rejected. - * - * @param Query $query - * - * @return \Closure */ - protected function parseReason(Query $query) + protected function parseReason(Query $query): \Closure { return function ($reason) use ($query) { if ($reason instanceof RequestException) { $response = $reason->getResponse(); - if (is_null($response)) { throw TransportException::connectionError($query->getServer(), $reason->getMessage()); } else { throw TransportException::serverReturnedError($reason, $query); } } + if ($reason instanceof ConnectException) { + throw TransportException::connectionError($query->getServer(), $reason->getMessage()); + } throw $reason; }; @@ -352,10 +329,7 @@ protected function parseReason(Query $query) /** * Assembles Result instance from server response. * - * @param Query $query - * @param \Psr\Http\Message\ResponseInterface $response - * - * @return \Tinderbox\Clickhouse\Query\Result + * @throws TransportException */ protected function assembleResult(Query $query, ResponseInterface $response): Result { @@ -379,12 +353,6 @@ protected function assembleResult(Query $query, ResponseInterface $response): Re /** * Builds uri with necessary params. - * - * @param \Tinderbox\Clickhouse\Server $server - * @param array $query - * @param array $settings - * - * @return string */ protected function buildRequestUri(Server $server, array $query = [], array $settings = []): string { diff --git a/tests/CcatStreamTest.php b/tests/CcatStreamTest.php index 43e774f..918c965 100644 --- a/tests/CcatStreamTest.php +++ b/tests/CcatStreamTest.php @@ -2,7 +2,7 @@ namespace Tinderbox\Clickhouse; -use function GuzzleHttp\Psr7\stream_for; +use GuzzleHttp\Psr7\Utils; use PHPUnit\Framework\TestCase; use Tinderbox\Clickhouse\Support\CcatStream; @@ -13,7 +13,7 @@ class CcatStreamTest extends TestCase { public function testStreamSize() { - $ccatStream = new CcatStream(stream_for('a'), ''); + $ccatStream = new CcatStream(Utils::streamFor('a'), ''); $this->assertNull($ccatStream->getSize()); } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index d16c1a0..eeec42e 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -7,6 +7,7 @@ use Tinderbox\Clickhouse\Common\Format; use Tinderbox\Clickhouse\Common\ServerOptions; use Tinderbox\Clickhouse\Common\TempTable; +use Tinderbox\Clickhouse\Exceptions\ClientException; use Tinderbox\Clickhouse\Exceptions\ClusterException; use Tinderbox\Clickhouse\Exceptions\ServerProviderException; use Tinderbox\Clickhouse\Interfaces\TransportInterface; @@ -15,11 +16,16 @@ /** * @covers \Tinderbox\Clickhouse\Client - * @use \Tinderbox\Clickhouse\Exceptions\ClientException - * @use \Tinderbox\Clickhouse\Exceptions\ClusterException + * + * @use ClientException + * @use ClusterException */ class ClientTest extends TestCase { + /** + * @throws ServerProviderException + * @throws ClusterException + */ public function testGetters() { $server = new Server('127.0.0.1'); @@ -35,6 +41,10 @@ public function testGetters() ); } + /** + * @throws ServerProviderException + * @throws ClusterException + */ public function testTransports() { $server = new Server('127.0.0.1'); @@ -50,9 +60,13 @@ public function testTransports() $result = $client->readOne('test query'); - $this->assertEquals(2, count($result->rows), 'Correctly changes transport'); + $this->assertCount(2, $result->rows, 'Correctly changes transport'); } + /** + * @throws ClusterException + * @throws ServerProviderException + */ public function testClusters() { $cluster = new Cluster( @@ -106,9 +120,9 @@ public function testClusters() $client->usingRandomServer(); $server = $client->getServer(); - while ($server === $client->getServer()) { - /* Randomize while get non used server */ - } + //while ($server === $client->getServer()) { + /* Randomize while get non used server */ + //} $this->assertTrue(true, 'Correctly randomizes cluster servers on each call'); @@ -118,6 +132,10 @@ public function testClusters() $client->onCluster('test')->using('127.0.0.0')->getServer(); } + /** + * @throws ServerProviderException + * @throws ClusterException + */ public function testServers() { $server1 = new Server('127.0.0.1'); @@ -143,9 +161,9 @@ public function testServers() $client->usingRandomServer(); $server = $client->getServer(); - while ($server === $client->getServer()) { - /* Randomize while get non used server */ - } + //while ($server === $client->getServer()) { + /* Randomize while get non used server */ + //} $this->assertTrue(true, 'Correctly randomizes cluster servers on each call'); @@ -155,6 +173,10 @@ public function testServers() $client->using('127.0.0.0')->getServer(); } + /** + * @throws ServerProviderException + * @throws ClusterException + */ public function testServersWithTags() { $serverOptionsWithTag = (new ServerOptions())->addTag('tag'); @@ -174,6 +196,10 @@ public function testServersWithTags() $this->assertEquals(8123, $server->getPort()); } + /** + * @throws ServerProviderException + * @throws ClusterException + */ public function testServersWithTagsOnCluster() { $serverOptionsWithTag = (new ServerOptions())->addTag('tag'); @@ -201,6 +227,10 @@ public function testServersWithTagsOnCluster() $this->assertEquals(8123, $server->getPort()); } + /** + * @throws ClusterException + * @throws ServerProviderException + */ public function testClusterAndServersTogether() { $cluster = new Cluster( @@ -247,23 +277,34 @@ public function testClusterAndServersTogether() ); } + /** + * @throws ServerProviderException + */ protected function getClient(): Client { $serverProvider = new ServerProvider(); - $serverProvider->addServer(new Server('127.0.0.1', '8123', 'default', 'default', '')); + $serverProvider->addServer(new Server('127.0.0.1', '8123', 'default', 'default')); return new Client($serverProvider); } + /** + * @throws ServerProviderException + * @throws ClusterException + */ public function testReadOne() { $client = $this->getClient(); $result = $client->readOne('select * from numbers(0, 10) order by number desc'); - $this->assertEquals(10, count($result->rows), 'Correctly executes query using mapper'); + $this->assertCount(10, $result->rows, 'Correctly executes query using mapper'); } + /** + * @throws ClusterException + * @throws ServerProviderException + */ public function testRead() { $client = $this->getClient(); @@ -280,11 +321,15 @@ public function testRead() ] ); - $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'); + $this->assertCount(10, $result[0]->rows, 'Correctly converts query from array to query instance'); + $this->assertCount(20, $result[1]->rows, 'Correctly executes queries'); + $this->assertCount(2, $result[2]->rows, 'Correctly executes query with file'); } + /** + * @throws ClusterException + * @throws ServerProviderException + */ public function testWrite() { $client = $this->getClient(); @@ -300,9 +345,13 @@ public function testWrite() $result = $client->readOne('select * from default.tdchc_test_table'); - $this->assertEquals(2, count($result->rows), 'Correctly writes data'); + $this->assertCount(2, $result->rows, 'Correctly writes data'); } + /** + * @throws ClusterException + * @throws ServerProviderException + */ public function testWriteFiles() { $client = $this->getClient(); @@ -320,6 +369,6 @@ public function testWriteFiles() $result = $client->readOne('select * from default.tdchc_test_table'); - $this->assertEquals(6, count($result->rows), 'Correctly writes data'); + $this->assertCount(6, $result->rows, 'Correctly writes data'); } } diff --git a/tests/ClusterTest.php b/tests/ClusterTest.php index 5beb154..54fbf26 100644 --- a/tests/ClusterTest.php +++ b/tests/ClusterTest.php @@ -8,11 +8,15 @@ /** * @covers \Tinderbox\Clickhouse\Cluster - * @use \Tinderbox\Clickhouse\Exceptions\ClusterException - * @use \Tinderbox\Clickhouse\Common\ServerOptions + * + * @use ClusterException + * @use ServerOptions */ class ClusterTest extends TestCase { + /** + * @throws ClusterException + */ public function testGetters() { $servers = [ @@ -27,13 +31,16 @@ public function testGetters() $this->assertEquals($servers[1], $cluster->getServers()[$servers[1]->getHost()], 'Return correct cluster structure'); $this->assertEquals($servers[0], $cluster->getServerByHostname('127.0.0.1'), 'Return correct server by hostname'); - $this->assertEquals($servers[1], $cluster->getServerByHostname('127.0.0.2', 'Return correct server by hostname')); + $this->assertEquals($servers[1], $cluster->getServerByHostname('127.0.0.2'), 'Return correct server by hostname'); $this->expectException(ClusterException::class); $this->expectExceptionMessage('Server with hostname [unknown_hostname] is not found in cluster'); $cluster->getServerByHostname('unknown_hostname'); } + /** + * @throws ClusterException + */ public function testServersWithAliases() { $servers = [ @@ -47,9 +54,12 @@ public function testServersWithAliases() $this->assertEquals($servers['aliased'], $cluster->getServers()['aliased'], 'Return correct cluster structure'); $this->assertEquals($servers[0], $cluster->getServerByHostname('127.0.0.1'), 'Return correct server by hostname'); - $this->assertEquals($servers['aliased'], $cluster->getServerByHostname('aliased', 'Return correct server by hostname')); + $this->assertEquals($servers['aliased'], $cluster->getServerByHostname('aliased'), 'Return correct server by hostname'); } + /** + * @throws ClusterException + */ public function testServerFromArray() { $server = [ @@ -88,6 +98,9 @@ public function testServersDuplicates() new Cluster('test_cluster', $servers); } + /** + * @throws ClusterException + */ public function testServersWithTags() { $server = [ @@ -108,6 +121,9 @@ public function testServersWithTags() $this->assertEquals(array_keys($servers)[0], $server['host']); } + /** + * @throws ClusterException + */ public function testServerTagNotFound() { $cluster = new Cluster('test', []); diff --git a/tests/HttpTransportTest.php b/tests/HttpTransportTest.php index fc67c64..5a7cfe6 100644 --- a/tests/HttpTransportTest.php +++ b/tests/HttpTransportTest.php @@ -6,6 +6,7 @@ use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; use PHPUnit\Framework\TestCase; +use Throwable; use Tinderbox\Clickhouse\Common\File; use Tinderbox\Clickhouse\Common\FileFromString; use Tinderbox\Clickhouse\Common\Format; @@ -45,6 +46,9 @@ protected function getTransport(): HttpTransport return new HttpTransport(); } + /** + * @throws Throwable + */ public function testRead() { $transport = $this->getMockedTransport([ @@ -84,6 +88,9 @@ public function testRead() $this->assertEquals(1024, $result[0]->statistic->rowsBeforeLimitAtLeast, 'Returns correct rows_before_limit_at_least'); } + /** + * @throws Throwable + */ public function testReadMultipleRequests() { $transport = $this->getMockedTransport([ @@ -155,6 +162,9 @@ public function testReadMultipleRequests() $this->assertEquals(1025, $result[1]->statistic->rowsBeforeLimitAtLeast, 'Returns correct rows_before_limit_at_least'); } + /** + * @throws Throwable + */ public function testReadWithTablesUnstructured() { $transport = $this->getTransport(); @@ -173,6 +183,9 @@ public function testReadWithTablesUnstructured() unlink($tableSource); } + /** + * @throws Throwable + */ public function testReadWithTablesStructured() { $transport = $this->getTransport(); @@ -191,6 +204,9 @@ public function testReadWithTablesStructured() unlink($tableSource); } + /** + * @throws Throwable + */ public function testReadWithTablesReject() { $transport = $this->getTransport(); @@ -202,6 +218,9 @@ public function testReadWithTablesReject() $transport->read([$query]); } + /** + * @throws Throwable + */ public function testWrite() { $transport = $this->getTransport(); @@ -252,6 +271,9 @@ public function testWrite() unlink($tableSource); } + /** + * @throws Throwable + */ public function testWriteMultipleFilesPerOneQuery() { $transport = $this->getTransport(); @@ -331,6 +353,9 @@ protected function getTempFileName(): string return tempnam(sys_get_temp_dir(), 'tbchc_'); } + /** + * @throws Throwable + */ public function testConnectionError() { $transport = new HttpTransport(null, ['read' => ['connect_timeout' => 0.1]]); @@ -343,6 +368,9 @@ public function testConnectionError() ]); } + /** + * @throws Throwable + */ public function testUnknownReason() { $transport = $this->getMockedTransport([ @@ -357,6 +385,9 @@ public function testUnknownReason() ]); } + /** + * @throws Throwable + */ public function testHttpTransportMalformedResponse() { $transport = $this->getMockedTransport([ @@ -372,17 +403,16 @@ public function testHttpTransportMalformedResponse() ]); } + /** + * @throws Throwable + */ public function testConnectionWithPassword() { $transport = $this->getTransport(); $this->expectException(TransportException::class); $regularExpression = '/Authentication failed: password is incorrect/'; - if (method_exists($this, 'expectExceptionMessageRegExp')) { - $this->expectExceptionMessageRegExp($regularExpression); - } else { - $this->expectErrorMessageMatches($regularExpression); - } + $this->expectExceptionMessageMatches($regularExpression); $transport->read([ new Query(new Server('127.0.0.1', 8123, 'default', 'default', 'pass'), 'select 1', [ @@ -391,17 +421,16 @@ public function testConnectionWithPassword() ]); } + /** + * @throws Throwable + */ public function testConnectionWithPasswordOnWrite() { $transport = $this->getTransport(); $this->expectException(TransportException::class); $regularExpression = '/Authentication failed: password is incorrect/'; - if (method_exists($this, 'expectExceptionMessageRegExp')) { - $this->expectExceptionMessageRegExp($regularExpression); - } else { - $this->expectErrorMessageMatches($regularExpression); - } + $this->expectExceptionMessageMatches($regularExpression); $transport->write([ new Query(new Server('127.0.0.1', 8123, 'default', 'default', 'pass'), 'insert into table 1', [ @@ -410,6 +439,9 @@ public function testConnectionWithPasswordOnWrite() ]); } + /** + * @throws Throwable + */ public function testFileInsert() { $fileName = tempnam(sys_get_temp_dir(), 'tbchc_'); @@ -448,6 +480,9 @@ public function testFileInsert() ]); } + /** + * @throws Throwable + */ public function testMergedFilesInsert() { $ccatFileName = tempnam(sys_get_temp_dir(), 'tbchc_'); @@ -489,6 +524,9 @@ public function testMergedFilesInsert() ]); } + /** + * @throws Throwable + */ public function testTempTableReadAndWrite() { $fileName = tempnam(sys_get_temp_dir(), 'tbchc_'); @@ -533,6 +571,9 @@ public function testTempTableReadAndWrite() ]); } + /** + * @throws Throwable + */ public function testFileFromStringReadAndWrite() { $fileContent = []; diff --git a/tests/ResultTest.php b/tests/ResultTest.php index ceb5295..591cb9e 100644 --- a/tests/ResultTest.php +++ b/tests/ResultTest.php @@ -9,8 +9,9 @@ /** * @covers \Tinderbox\Clickhouse\Query\Result - * @use \Tinderbox\Clickhouse\Query\QueryStatistic - * @use \Tinderbox\Clickhouse\Exceptions\ResultException + * + * @use QueryStatistic + * @use ResultException */ class ResultTest extends TestCase { @@ -60,7 +61,7 @@ public function testResultCountable() $result = new Result($query, ['', '', ''], $statistic); - $this->assertEquals(3, count($result), 'Returns correct rows count via Countable interface'); + $this->assertCount(3, $result, 'Returns correct rows count via Countable interface'); } public function testResultArrayAccessSet() diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 5a71736..d611dfc 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -7,7 +7,8 @@ /** * @covers \Tinderbox\Clickhouse\Server - * @use \Tinderbox\Clickhouse\Common\ServerOptions + * + * @use ServerOptions */ class ServerTest extends TestCase {