diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e4e3f55..1a3baca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,13 +12,21 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} - name: Set up PHP uses: shivammathur/setup-php@v2 with: php-version: '8.3' + - name: "Duster Lint" + uses: tighten/duster-action@v3 + with: + args: lint + - name: Install ClickHouse run: | sudo apt-get update diff --git a/.gitignore b/.gitignore index 3e75b21..d906a22 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ vendor coverage clickhouse .phpunit.result.cache +.php_cs.cache +.DS_Store +.idea diff --git a/composer.json b/composer.json index 2510643..e0bc00c 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ }, "require-dev": { "orchestra/testbench": "^9.0", - "phpunit/phpunit": "^11.0" + "phpunit/phpunit": "^11.0", + "tightenco/duster": "^3.0" }, "autoload": { "psr-4": { @@ -37,6 +38,8 @@ } }, "scripts": { + "lint": "vendor/bin/duster lint", + "fix": "vendor/bin/duster fix", "up": "docker compose up -d", "down": "docker compose down", "in": "docker exec -it lc_php /bin/bash -c \"composer install\"", diff --git a/duster.json b/duster.json new file mode 100644 index 0000000..81b56de --- /dev/null +++ b/duster.json @@ -0,0 +1,5 @@ +{ + "exclude": [ + "tests/TestCase.php" + ] +} diff --git a/src/ClickhouseBuilder.php b/src/ClickhouseBuilder.php index d3f8a96..c2f9e90 100644 --- a/src/ClickhouseBuilder.php +++ b/src/ClickhouseBuilder.php @@ -12,7 +12,7 @@ class ClickhouseBuilder extends Builder /** * Retrieve the "count" result of the query. * - * @param string $columns + * @param string $columns * @return int */ public function count($columns = null) @@ -20,18 +20,15 @@ public function count($columns = null) return parent::count($columns ?: []); } - /** - * @return array - */ public function getBindings(): array { $bindings = []; - $keys = []; + $keys = []; foreach ($this->wheres as &$where) { - if (!empty($where['value']) && !$where['value'] instanceof Expression) { + if (! empty($where['value']) && ! $where['value'] instanceof Expression) { $col = $where['column']; - if (!isset($keys[$col])) { + if (! isset($keys[$col])) { $keys[$col] = 0; } @@ -48,9 +45,6 @@ public function getBindings(): array /** * Insert a new record into the database. - * - * @param array $values - * @return bool */ public function insert(array $values): bool { @@ -61,7 +55,7 @@ public function insert(array $values): bool return true; } - if (!is_array(reset($values))) { + if (! is_array(reset($values))) { $values = [$values]; } @@ -88,7 +82,6 @@ public function insert(array $values): bool /** * Update a record in the database. * - * @param array $values * @return int */ public function update(array $values) @@ -109,14 +102,11 @@ public function update(array $values) /** * Remove all of the expressions from a list of bindings. - * - * @param array $bindings - * @return array */ public function cleanBindings(array $bindings): array { return array_filter($bindings, static function ($binding) { - return !$binding instanceof Expression; + return ! $binding instanceof Expression; }); } } diff --git a/src/ClickhouseConnection.php b/src/ClickhouseConnection.php index 63886d8..23fe0c2 100644 --- a/src/ClickhouseConnection.php +++ b/src/ClickhouseConnection.php @@ -19,7 +19,6 @@ class ClickhouseConnection extends BaseConnection /** * Connection constructor. - * @param array $config */ public function __construct(array $config) { @@ -34,7 +33,6 @@ public function __construct(array $config) /** * Get SeasClick client - * @return SeasClick */ public function getClient(): SeasClick { @@ -44,19 +42,17 @@ public function getClient(): SeasClick /** * Begin a fluent query against a database table. * - * @param Closure|Builder|string $table - * @param string|null $as + * @param Closure|Builder|string $table + * @param string|null $as * @return Builder */ -// public function table($table, $as = null): Builder -// { -// $this->notImplementedException(); -// } + // public function table($table, $as = null): Builder + // { + // $this->notImplementedException(); + // } /** * Get a new query builder instance. - * - * @return ClickhouseBuilder */ public function query(): ClickhouseBuilder { @@ -67,45 +63,33 @@ public function query(): ClickhouseBuilder /** * Get the query post processor used by the connection. - * - * @return Processor */ public function getDefaultPostProcessor(): Processor { - return new ClickhouseProcessor(); - } - - /** - * Get the default query grammar instance. - * - * @return ClickhouseGrammar - */ - protected function getDefaultQueryGrammar(): ClickhouseGrammar - { - return new ClickhouseGrammar(); + return new ClickhouseProcessor; } /** * Run a select statement and return a single result. * - * @param string $query - * @param array $bindings - * @param bool $useReadPdo + * @param string $query + * @param array $bindings + * @param bool $useReadPdo * @return mixed */ public function selectOne($query, $bindings = [], $useReadPdo = true) { $records = $this->db->select($query, $bindings); + return array_shift($records); } /** * Run a select statement against the database. * - * @param string $query - * @param array $bindings - * @param bool $useReadPdo - * @return array + * @param string $query + * @param array $bindings + * @param bool $useReadPdo */ public function select($query, $bindings = [], $useReadPdo = true): array { @@ -115,10 +99,9 @@ public function select($query, $bindings = [], $useReadPdo = true): array /** * Run a select statement against the database and returns a generator. * - * @param string $query - * @param array $bindings - * @param bool $useReadPdo - * @return Generator + * @param string $query + * @param array $bindings + * @param bool $useReadPdo */ public function cursor($query, $bindings = [], $useReadPdo = true): Generator { @@ -128,22 +111,21 @@ public function cursor($query, $bindings = [], $useReadPdo = true): Generator /** * Run an insert statement against the database. * - * @param string $query Query is table name - * @param array $bindings - * @return bool + * @param string $query Query is table name + * @param array $bindings */ public function insert($query, $bindings = []): bool { [$keys, $values] = $this->parseBindings($bindings); + return $this->db->insert($query, $keys, $values); } /** * Run an update statement against the database. * - * @param string $query - * @param array $bindings - * @return int + * @param string $query + * @param array $bindings */ public function update($query, $bindings = []): int { @@ -154,9 +136,8 @@ public function update($query, $bindings = []): int /** * Run a delete statement against the database. * - * @param string $query - * @param array $bindings - * @return int + * @param string $query + * @param array $bindings */ public function delete($query, $bindings = []): int { @@ -167,9 +148,8 @@ public function delete($query, $bindings = []): int /** * Execute an SQL statement and return the boolean result. * - * @param string $query - * @param array $bindings - * @return bool + * @param string $query + * @param array $bindings */ public function statement($query, $bindings = []): bool { @@ -179,9 +159,8 @@ public function statement($query, $bindings = []): bool /** * Run an SQL statement and get the number of rows affected. * - * @param string $query - * @param array $bindings - * @return int + * @param string $query + * @param array $bindings */ public function affectingStatement($query, $bindings = []): int { @@ -191,8 +170,7 @@ public function affectingStatement($query, $bindings = []): int /** * Run a raw, unprepared query against the PDO connection. * - * @param string $query - * @return bool + * @param string $query */ public function unprepared($query): bool { @@ -202,7 +180,6 @@ public function unprepared($query): bool /** * Prepare the query bindings for execution. * - * @param array $bindings * @return array */ public function prepareBindings(array $bindings) @@ -213,8 +190,8 @@ public function prepareBindings(array $bindings) /** * Execute a Closure within a transaction. * - * @param callback|Closure $callback - * @param int $attempts + * @param callable|Closure $callback + * @param int $attempts * @return mixed * * @throws Throwable @@ -226,8 +203,6 @@ public function transaction($callback, $attempts = 1) /** * Start a new database transaction. - * - * @return void */ public function beginTransaction(): void { @@ -236,8 +211,6 @@ public function beginTransaction(): void /** * Commit the active database transaction. - * - * @return void */ public function commit(): void { @@ -248,7 +221,6 @@ public function commit(): void * Rollback the active database transaction. * * @param int|null $toLevel - * @return void */ public function rollBack($toLevel = null): void { @@ -267,9 +239,6 @@ public function transactionLevel() /** * Execute the given callback in "dry run" mode. - * - * @param Closure $callback - * @return array */ public function pretend(Closure $callback): array { @@ -277,16 +246,24 @@ public function pretend(Closure $callback): array } /** - * @param array $bindings i.e. [['name' => 'John', 'user_id' => 321]] - * @return array [['name', 'user_id'], [['John', 321]]] + * Get the default query grammar instance. + */ + protected function getDefaultQueryGrammar(): ClickhouseGrammar + { + return new ClickhouseGrammar; + } + + /** + * @param array $bindings i.e. [['name' => 'John', 'user_id' => 321]] + * @return array [['name', 'user_id'], [['John', 321]]] */ private function parseBindings(array $bindings): array { - if (!$bindings) { + if (! $bindings) { return [[], []]; } - if (!is_string(current(array_flip($bindings)))) { + if (! is_string(current(array_flip($bindings)))) { throw new InvalidArgumentException( "Keys must be strings, i.e. ['name' => 'John', 'user_id' => 321]" ); diff --git a/src/ClickhouseGrammar.php b/src/ClickhouseGrammar.php index 21fa8dc..093e9ba 100644 --- a/src/ClickhouseGrammar.php +++ b/src/ClickhouseGrammar.php @@ -9,11 +9,88 @@ class ClickhouseGrammar extends Grammar { + /** + * Compile an insert statement into SQL. + */ + public function compileInsert(Builder $query, array $values): string + { + return $this->wrapTable($query->from); + } + + /** + * Create query parameter place-holders for an array. + */ + public function parameterize(array $values): string + { + $parameters = []; + $key_counts = []; + foreach ($values as $key => $value) { + $key_count = $key_counts[$key] ?? 0; + $parameters[] = $this->parameter($value, $key); + $key_counts[$key] = empty($key_counts[$key]) ? 1 : $key_counts[$key] + 1; + } + + return implode(', ', $parameters); + } + + /** + * Get the appropriate query parameter place-holder for a value. + * + * @param mixed $value + * @param null $key + */ + public function parameter($value, $key = null): string + { + if ($this->isExpression($value)) { + return $this->getValue($value); + } + + $param = '{' . $key . '}'; + + return is_string($value) ? "'{$param}'" : $param; + } + + /** + * Compile an update statement into SQL. + */ + public function compileUpdate(Builder $query, array $values): string + { + $table = $this->wrapTable($query->from); + + $columns = $this->compileUpdateColumns($query, $values); + + $where = $this->compileWheres($query); + + return trim( + isset($query->joins) + ? $this->compileUpdateWithJoins($query, $table, $columns, $where) + : $this->compileUpdateWithoutJoins($query, $table, $columns, $where) + ); + } + + /** + * Prepare the bindings for an update statement. + */ + public function prepareBindingsForUpdate(array $bindings, array $values): array + { + foreach ($bindings as $component => $component_bindings) { + if (! $component_bindings) { + continue; + } + foreach ($component_bindings as $key => $value) { + if (! isset($values[$key])) { + $values[$key] = $value; + } + } + } + + return $values; + } + /** * Compile a raw where clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereRaw(Builder $query, $where) @@ -24,8 +101,7 @@ protected function whereRaw(Builder $query, $where) /** * Compile a basic where clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereBasic(Builder $query, $where) @@ -38,13 +114,12 @@ protected function whereBasic(Builder $query, $where) /** * Compile a "where in" clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereIn(Builder $query, $where) { - if (!empty($where['values'])) { + if (! empty($where['values'])) { return $this->wrap($where['column']) . ' in (' . $this->parameterize($where['values']) . ')'; } @@ -54,13 +129,12 @@ protected function whereIn(Builder $query, $where) /** * Compile a "where not in" clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereNotIn(Builder $query, $where) { - if (!empty($where['values'])) { + if (! empty($where['values'])) { return $this->wrap($where['column']) . ' not in (' . $this->parameterize($where['values']) . ')'; } @@ -72,13 +146,12 @@ protected function whereNotIn(Builder $query, $where) * * For safety, whereIntegerInRaw ensures this method is only used with integer values. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereNotInRaw(Builder $query, $where) { - if (!empty($where['values'])) { + if (! empty($where['values'])) { return $this->wrap($where['column']) . ' not in (' . implode(', ', $where['values']) . ')'; } @@ -90,13 +163,12 @@ protected function whereNotInRaw(Builder $query, $where) * * For safety, whereIntegerInRaw ensures this method is only used with integer values. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereInRaw(Builder $query, $where) { - if (!empty($where['values'])) { + if (! empty($where['values'])) { return $this->wrap($where['column']) . ' in (' . implode(', ', $where['values']) . ')'; } @@ -106,8 +178,7 @@ protected function whereInRaw(Builder $query, $where) /** * Compile a "where null" clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereNull(Builder $query, $where) @@ -118,8 +189,7 @@ protected function whereNull(Builder $query, $where) /** * Compile a "where not null" clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereNotNull(Builder $query, $where) @@ -130,8 +200,7 @@ protected function whereNotNull(Builder $query, $where) /** * Compile a "between" where clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereBetween(Builder $query, $where) @@ -148,8 +217,7 @@ protected function whereBetween(Builder $query, $where) /** * Compile a "between" where clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereBetweenColumns(Builder $query, $where) @@ -166,8 +234,7 @@ protected function whereBetweenColumns(Builder $query, $where) /** * Compile a "where date" clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereDate(Builder $query, $where) @@ -178,8 +245,7 @@ protected function whereDate(Builder $query, $where) /** * Compile a "where time" clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereTime(Builder $query, $where) @@ -190,8 +256,7 @@ protected function whereTime(Builder $query, $where) /** * Compile a "where day" clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereDay(Builder $query, $where) @@ -202,8 +267,7 @@ protected function whereDay(Builder $query, $where) /** * Compile a "where month" clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereMonth(Builder $query, $where) @@ -214,8 +278,7 @@ protected function whereMonth(Builder $query, $where) /** * Compile a "where year" clause. * - * @param Builder $query - * @param array $where + * @param array $where * @return string */ protected function whereYear(Builder $query, $where) @@ -223,59 +286,10 @@ protected function whereYear(Builder $query, $where) return $this->dateBasedWhere('year', $query, $where); } - /** - * Compile an insert statement into SQL. - * - * @param Builder $query - * @param array $values - * @return string - */ - public function compileInsert(Builder $query, array $values): string - { - return $this->wrapTable($query->from); - } - - /** - * Create query parameter place-holders for an array. - * - * @param array $values - * @return string - */ - public function parameterize(array $values): string - { - $parameters = []; - $key_counts = []; - foreach ($values as $key => $value) { - $key_count = $key_counts[$key] ?? 0; - $parameters[] = $this->parameter($value, $key); - $key_counts[$key] = empty($key_counts[$key]) ? 1 : $key_counts[$key] + 1; - } - return implode(', ', $parameters); - } - - /** - * Get the appropriate query parameter place-holder for a value. - * - * @param mixed $value - * @param null $key - * @return string - */ - public function parameter($value, $key = null): string - { - if ($this->isExpression($value)) { - return $this->getValue($value); - } - - $param = '{' . $key . '}'; - - return is_string($value) ? "'{$param}'" : $param; - } - /** * Get an array of all the where clauses for the query. * - * @param Builder $query - * @return array + * @param Builder $query */ protected function compileWheresToArray($query): array { @@ -293,10 +307,6 @@ protected function compileWheresToArray($query): array /** * Compile the columns for an update statement. - * - * @param Builder $query - * @param array $values - * @return string */ protected function compileUpdateColumns(Builder $query, array $values): string { @@ -308,76 +318,26 @@ protected function compileUpdateColumns(Builder $query, array $values): string /** * Compile an update statement without joins into SQL. * - * @param Builder $query - * @param string $table - * @param string $columns - * @param string $where - * @return string + * @param string $table + * @param string $columns + * @param string $where */ protected function compileUpdateWithoutJoins(Builder $query, $table, $columns, $where): string { return "alter table {$table} update {$columns} {$where}"; } - /** - * Compile an update statement into SQL. - * - * @param Builder $query - * @param array $values - * @return string - */ - public function compileUpdate(Builder $query, array $values): string - { - $table = $this->wrapTable($query->from); - - $columns = $this->compileUpdateColumns($query, $values); - - $where = $this->compileWheres($query); - - return trim( - isset($query->joins) - ? $this->compileUpdateWithJoins($query, $table, $columns, $where) - : $this->compileUpdateWithoutJoins($query, $table, $columns, $where) - ); - } - - /** - * Prepare the bindings for an update statement. - * - * @param array $bindings - * @param array $values - * @return array - */ - public function prepareBindingsForUpdate(array $bindings, array $values): array - { - foreach ($bindings as $component => $component_bindings) { - if (!$component_bindings) { - continue; - } - foreach ($component_bindings as $key => $value) { - if (!isset($values[$key])) { - $values[$key] = $value; - } - } - } - - return $values; - } - /** * Format named parameter key to avoid duplicates - * @param Builder $query - * @param string $key - * @return string */ private function prepareKey(Builder $query, string $key): string { - if (!isset($query->binding_count[$key])) { + if (! isset($query->binding_count[$key])) { $query->binding_count[$key] = 0; } $key_count = ++$query->binding_count[$key]; $key_index = $key_count - 1; - $key .= ($key_index > 0 ? $key_index : ''); + $key .= ($key_index > 0 ? $key_index : ''); return $key; } diff --git a/src/ClickhouseModel.php b/src/ClickhouseModel.php index 2612926..7e61ae6 100644 --- a/src/ClickhouseModel.php +++ b/src/ClickhouseModel.php @@ -8,7 +8,7 @@ class ClickhouseModel extends Model { - public $timestamps = false; + public $timestamps = false; public $incrementing = false; public function getConnectionName() diff --git a/src/ClickhouseProcessor.php b/src/ClickhouseProcessor.php index 8edafab..1cf75ec 100644 --- a/src/ClickhouseProcessor.php +++ b/src/ClickhouseProcessor.php @@ -6,6 +6,4 @@ use Illuminate\Database\Query\Processors\Processor; -class ClickhouseProcessor extends Processor -{ -} +class ClickhouseProcessor extends Processor {} diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 4f82b48..86cb3ef 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -8,14 +8,20 @@ class ModelTest extends TestCase { - public function testCreate(): void + /** + * @test + */ + public function create(): void { // Arrange & Act & Assert $this->expectNotToPerformAssertions(); Analytic::create(['ts' => time(), 'analytic_id' => 321, 'status' => 204]); } - public function testCreateCount(): void + /** + * @test + */ + public function createCount(): void { // Arrange Analytic::create(['ts' => time(), 'analytic_id' => 321, 'status' => 204]); @@ -25,7 +31,10 @@ public function testCreateCount(): void self::assertEquals(2, Analytic::count()); } - public function testWhere(): void + /** + * @test + */ + public function where(): void { // Arrange Analytic::create(['ts' => time(), 'analytic_id' => mt_rand(1000, 9999), 'status' => mt_rand(200, 599)]); @@ -38,7 +47,10 @@ public function testWhere(): void ); } - public function testWhereString(): void + /** + * @test + */ + public function whereString(): void { // Arrange Analytic::create(['ts' => time(), 'analytic_id' => mt_rand(1000, 9999), 'status' => mt_rand(200, 599), 'name' => 'page_view']); @@ -51,7 +63,10 @@ public function testWhereString(): void ); } - public function testMultipleWhere(): void + /** + * @test + */ + public function multipleWhere(): void { // Arrange Analytic::create(['ts' => time(), 'analytic_id' => mt_rand(1000, 9999), 'status' => mt_rand(200, 599)]); @@ -61,12 +76,15 @@ public function testMultipleWhere(): void self::assertSame( 2, Analytic::where('ts', '>', strtotime('-1 day')) - ->where('ts', '<', strtotime('+1 day')) - ->count() + ->where('ts', '<', strtotime('+1 day')) + ->count() ); } - public function testUpdate(): void + /** + * @test + */ + public function update(): void { // Arrange Analytic::create(['ts' => time(), 'analytic_id' => 123, 'status' => 204, 'name' => 'page_view']); @@ -75,17 +93,17 @@ public function testUpdate(): void self::assertSame( 1, Analytic::where('name', 'page_view') - ->where('status', 204) - ->count() + ->where('status', 204) + ->count() ); // Act Analytic::where('name', 'page_view') - ->where('status', 204) - ->update([ - 'name' => 'page_visit', - 'status' => 200, - ]); + ->where('status', 204) + ->update([ + 'name' => 'page_visit', + 'status' => 200, + ]); // Needed to prevent race condition failure usleep(100000); @@ -94,25 +112,28 @@ public function testUpdate(): void self::assertSame( 1, Analytic::where('name', 'page_visit') - ->where('status', 200) - ->count() + ->where('status', 200) + ->count() ); } - public function testJsonExtract(): void + /** + * @test + */ + public function jsonExtract(): void { // Arrange Analytic::create([ - 'ts' => time(), + 'ts' => time(), 'analytic_id' => 123, - 'status' => 200, - 'name' => json_encode(['action' => 'page_visit', 'referer' => ['https://google.com/']]), + 'status' => 200, + 'name' => json_encode(['action' => 'page_visit', 'referer' => ['https://google.com/']]), ]); Analytic::create([ - 'ts' => time(), + 'ts' => time(), 'analytic_id' => 124, - 'status' => 200, - 'name' => json_encode(['action' => 'page_view', 'referer' => ['https://duckduckgo.com/']]), + 'status' => 200, + 'name' => json_encode(['action' => 'page_view', 'referer' => ['https://duckduckgo.com/']]), ]); // Act diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 932bab4..92510b0 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -8,7 +8,10 @@ class QueryTest extends TestCase { - public function testInsert(): void + /** + * @test + */ + public function insert(): void { // Arrange & Act & Assert $this->expectNotToPerformAssertions(); @@ -18,18 +21,24 @@ public function testInsert(): void ); } - public function testTableInsert(): void + /** + * @test + */ + public function tableInsert(): void { // Arrange & Act & Assert $this->expectNotToPerformAssertions(); DB::connection('clickhouse')->table('analytics')->insert([ - 'ts' => time(), + 'ts' => time(), 'analytic_id' => 321, - 'status' => 204, + 'status' => 204, ]); } - public function testWhere(): void + /** + * @test + */ + public function where(): void { // Arrange DB::connection('clickhouse')->insert( @@ -51,7 +60,10 @@ public function testWhere(): void ); } - public function testMultipleWheres(): void + /** + * @test + */ + public function multipleWheres(): void { // Arrange DB::connection('clickhouse')->insert( @@ -74,7 +86,10 @@ public function testMultipleWheres(): void ); } - public function testSelect(): void + /** + * @test + */ + public function select(): void { // Arrange DB::connection('clickhouse')->insert( @@ -88,15 +103,16 @@ public function testSelect(): void // Act $records = DB::connection('clickhouse') - ->table('analytics') - ->select('ts', 'status') - ->get() - ->toArray(); + ->table('analytics') + ->select('ts', 'status') + ->get() + ->toArray(); // ensure order of records usort($records, static function ($a, $b) { if ($a['status'] === $b['status']) { return 0; } + return ($a['status'] < $b['status']) ? -1 : 1; }); @@ -107,7 +123,10 @@ public function testSelect(): void self::assertEquals($row2['status'], $records[1]['status']); } - public function testSelectRaw(): void + /** + * @test + */ + public function selectRaw(): void { // Arrange DB::connection('clickhouse')->insert( @@ -117,15 +136,18 @@ public function testSelectRaw(): void // Act $record = DB::connection('clickhouse') - ->table('analytics') - ->selectRaw('toMonth(dt) as month_number') - ->first(); + ->table('analytics') + ->selectRaw('toMonth(dt) as month_number') + ->first(); // Assert self::assertEquals($record['month_number'], idate('m')); } - public function testJoin(): void + /** + * @test + */ + public function join(): void { // Arrange DB::connection('clickhouse')->statement('TRUNCATE TABLE IF EXISTS models'); @@ -151,10 +173,10 @@ public function testJoin(): void // Act $record = DB::connection('clickhouse') - ->table('analytics') - ->join('models', 'models.id', '=', 'analytics.analytic_id') - ->selectRaw('models.name as name, analytics.status as status') - ->first(); + ->table('analytics') + ->join('models', 'models.id', '=', 'analytics.analytic_id') + ->selectRaw('models.name as name, analytics.status as status') + ->first(); // Assert self::assertEquals($record['name'], 'Cool Name');