From 5380a7eef0d0181599ae1978fe28e5b446fcb6eb Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Mon, 1 Apr 2024 20:19:57 +0300 Subject: [PATCH 1/3] fix Method Bavix\LaravelClickHouse\Database\Query\Builder::getCountForPagination does not exist #7 --- phpunit.xml | 12 ++--- src/Database/Query/Builder.php | 24 +++++++++ tests/FirstTableEntry.php | 22 ++++++++ tests/TestCase.php | 27 ++++++++++ tests/Unit/Database/ConnectionTest.php | 8 +-- .../Unit/Database/Eloquent/FirstTableTest.php | 50 +++++++++++++++++++ 6 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 tests/FirstTableEntry.php create mode 100644 tests/TestCase.php create mode 100644 tests/Unit/Database/Eloquent/FirstTableTest.php diff --git a/phpunit.xml b/phpunit.xml index 4b8b29f..716d0a0 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,17 +7,17 @@ - + tests - - - - - + + + + + diff --git a/src/Database/Query/Builder.php b/src/Database/Query/Builder.php index 9a9d090..4f1b700 100644 --- a/src/Database/Query/Builder.php +++ b/src/Database/Query/Builder.php @@ -10,6 +10,7 @@ use Illuminate\Support\Traits\Macroable; use Tinderbox\Clickhouse\Common\Format; use Tinderbox\ClickhouseBuilder\Query\BaseBuilder; +use Tinderbox\ClickhouseBuilder\Query\Expression; use Tinderbox\ClickhouseBuilder\Query\Grammar; class Builder extends BaseBuilder @@ -125,6 +126,29 @@ public function insert(array $values): bool return $this->connection->insert($this->grammar->compileInsert($this, $values), Arr::flatten($values)); } + public function getCountForPagination() + { + return (int) $this->getConnection() + ->table( + $this + ->cloneWithout(['columns' => [], 'orders' => [], 'limit' => null]) + ->select(new Expression('1')) + ) + ->count(); + } + + /** + * Set the limit and offset for a given page. + * + * @param int $page + * @param int $perPage + * @return $this + */ + public function forPage($page, $perPage = 15) + { + return $this->limit($perPage, ($page - 1) * $perPage); + } + public function getConnection(): Connection { return $this->connection; diff --git a/tests/FirstTableEntry.php b/tests/FirstTableEntry.php new file mode 100644 index 0000000..ba83fce --- /dev/null +++ b/tests/FirstTableEntry.php @@ -0,0 +1,22 @@ +set('database.connections.bavix::clickhouse', [ + 'host' => env('CLICKHOUSE_HOST', 'localhost'), + 'port' => env('CLICKHOUSE_PORT', '8123'), + 'driver' => env('CLICKHOUSE_DRIVER', 'bavix::clickhouse'), + 'database' => env('CLICKHOUSE_DATABASE', 'default'), + 'username' => env('CLICKHOUSE_USERNAME', 'default'), + 'password' => env('CLICKHOUSE_PASSWORD', ''), + ]); + } +} diff --git a/tests/Unit/Database/ConnectionTest.php b/tests/Unit/Database/ConnectionTest.php index 2d6d956..c5904ae 100644 --- a/tests/Unit/Database/ConnectionTest.php +++ b/tests/Unit/Database/ConnectionTest.php @@ -6,7 +6,7 @@ use Bavix\LaravelClickHouse\Database\Connection; use Bavix\LaravelClickHouse\Database\Query\Builder; -use PHPUnit\Framework\TestCase; +use Bavix\LaravelClickHouse\Tests\TestCase; use Tinderbox\Clickhouse\Exceptions\ClientException; class ConnectionTest extends TestCase @@ -19,11 +19,7 @@ class ConnectionTest extends TestCase protected function setUp(): void { parent::setUp(); - $this->connection = new Connection([ - 'host' => 'localhost', - 'port' => '8123', - 'database' => 'default', - ]); + $this->connection = $this->getConnection('bavix::clickhouse'); } public function testQuery(): void diff --git a/tests/Unit/Database/Eloquent/FirstTableTest.php b/tests/Unit/Database/Eloquent/FirstTableTest.php new file mode 100644 index 0000000..2da6425 --- /dev/null +++ b/tests/Unit/Database/Eloquent/FirstTableTest.php @@ -0,0 +1,50 @@ +connection = $this->getConnection('bavix::clickhouse'); + } + + public function testPaginate(): void + { + $this->connection->statement('DROP TABLE IF EXISTS my_first_table'); + + $result = $this->connection->statement('CREATE TABLE my_first_table +( + user_id UInt32, + message String, + timestamp DateTime, + metric Float32 +) +ENGINE = MergeTree() +PRIMARY KEY (user_id, timestamp)'); + self::assertTrue($result); + + + + self::assertTrue(FirstTableEntry::query()->insert([ + 'user_id' => 1, + 'message' => 'hello world', + 'timestamp' => new \DateTime(), + 'metric' => 42, + ])); + + self::assertCount(1, FirstTableEntry::query()->paginate()->items()); + } +} From 2f59ed36e490f3717da9d6b2cc5c1ea98c9f16b6 Mon Sep 17 00:00:00 2001 From: Github bot Date: Mon, 1 Apr 2024 17:22:54 +0000 Subject: [PATCH 2/3] autofix --- src/Database/Query/Builder.php | 8 ++++++-- tests/FirstTableEntry.php | 7 +------ tests/Unit/Database/Eloquent/FirstTableTest.php | 8 +++----- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Database/Query/Builder.php b/src/Database/Query/Builder.php index 4f1b700..4550f0a 100644 --- a/src/Database/Query/Builder.php +++ b/src/Database/Query/Builder.php @@ -131,8 +131,12 @@ public function getCountForPagination() return (int) $this->getConnection() ->table( $this - ->cloneWithout(['columns' => [], 'orders' => [], 'limit' => null]) - ->select(new Expression('1')) + ->cloneWithout([ + 'columns' => [], + 'orders' => [], + 'limit' => null, + ]) + ->select(new Expression('1')), null ) ->count(); } diff --git a/tests/FirstTableEntry.php b/tests/FirstTableEntry.php index ba83fce..5dcb97b 100644 --- a/tests/FirstTableEntry.php +++ b/tests/FirstTableEntry.php @@ -13,10 +13,5 @@ class FirstTableEntry extends Model */ protected $table = 'my_first_table'; - protected $fillable = [ - 'user_id', - 'message', - 'timestamp', - 'metric', - ]; + protected $fillable = ['user_id', 'message', 'timestamp', 'metric']; } diff --git a/tests/Unit/Database/Eloquent/FirstTableTest.php b/tests/Unit/Database/Eloquent/FirstTableTest.php index 2da6425..ba220f7 100644 --- a/tests/Unit/Database/Eloquent/FirstTableTest.php +++ b/tests/Unit/Database/Eloquent/FirstTableTest.php @@ -36,13 +36,11 @@ public function testPaginate(): void PRIMARY KEY (user_id, timestamp)'); self::assertTrue($result); - - self::assertTrue(FirstTableEntry::query()->insert([ - 'user_id' => 1, - 'message' => 'hello world', + 'user_id' => 1, + 'message' => 'hello world', 'timestamp' => new \DateTime(), - 'metric' => 42, + 'metric' => 42, ])); self::assertCount(1, FirstTableEntry::query()->paginate()->items()); From b8c0cf9fcccb3bc12cf8940941b20f1fa486f827 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Mon, 1 Apr 2024 20:23:46 +0300 Subject: [PATCH 3/3] update phpstan.baseline.neon --- phpstan.baseline.neon | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/phpstan.baseline.neon b/phpstan.baseline.neon index 825e05f..a7ed06f 100644 --- a/phpstan.baseline.neon +++ b/phpstan.baseline.neon @@ -35,11 +35,6 @@ parameters: count: 5 path: src/Database/Eloquent/Builder.php - - - message: "#^Call to an undefined method Bavix\\\\LaravelClickHouse\\\\Database\\\\Eloquent\\\\Builder\\:\\:forPage\\(\\)\\.$#" - count: 1 - path: src/Database/Eloquent/Builder.php - - message: "#^Call to an undefined method Bavix\\\\LaravelClickHouse\\\\Database\\\\Eloquent\\\\Builder\\:\\:forPageAfterId\\(\\)\\.$#" count: 1 @@ -290,6 +285,11 @@ parameters: count: 1 path: src/Database/Eloquent/Builder.php + - + message: "#^Method Bavix\\\\LaravelClickHouse\\\\Database\\\\Query\\\\Builder\\:\\:get\\(\\) invoked with 1 parameter, 0 required\\.$#" + count: 1 + path: src/Database/Eloquent/Builder.php + - message: "#^PHPDoc tag @param for parameter \\$query with type Illuminate\\\\Database\\\\Query\\\\Builder is incompatible with native type Bavix\\\\LaravelClickHouse\\\\Database\\\\Query\\\\Builder\\.$#" count: 2 @@ -912,7 +912,7 @@ parameters: - message: "#^Cannot cast mixed to int\\.$#" - count: 1 + count: 2 path: src/Database/Query/Builder.php - @@ -925,11 +925,21 @@ parameters: count: 1 path: src/Database/Query/Builder.php + - + message: "#^Method Bavix\\\\LaravelClickHouse\\\\Database\\\\Query\\\\Builder\\:\\:forPage\\(\\) should return \\$this\\(Bavix\\\\LaravelClickHouse\\\\Database\\\\Query\\\\Builder\\) but returns static\\(Bavix\\\\LaravelClickHouse\\\\Database\\\\Query\\\\Builder\\)\\.$#" + count: 1 + path: src/Database/Query/Builder.php + - message: "#^Method Bavix\\\\LaravelClickHouse\\\\Database\\\\Query\\\\Builder\\:\\:get\\(\\) return type with generic class Illuminate\\\\Support\\\\Collection does not specify its types\\: TKey, TValue$#" count: 1 path: src/Database/Query/Builder.php + - + message: "#^Method Bavix\\\\LaravelClickHouse\\\\Database\\\\Query\\\\Builder\\:\\:getCountForPagination\\(\\) has no return type specified\\.$#" + count: 1 + path: src/Database/Query/Builder.php + - message: "#^Method Bavix\\\\LaravelClickHouse\\\\Database\\\\Query\\\\Builder\\:\\:insert\\(\\) has parameter \\$values with no value type specified in iterable type array\\.$#" count: 1 @@ -950,6 +960,11 @@ parameters: count: 1 path: src/Database/Query/Builder.php + - + message: "#^Parameter \\#1 \\$table of method Tinderbox\\\\ClickhouseBuilder\\\\Integrations\\\\Laravel\\\\Connection\\:\\:table\\(\\) expects Closure\\|string\\|Tinderbox\\\\ClickhouseBuilder\\\\Integrations\\\\Laravel\\\\Builder, static\\(Bavix\\\\LaravelClickHouse\\\\Database\\\\Query\\\\Builder\\) given\\.$#" + count: 1 + path: src/Database/Query/Builder.php + - message: "#^Property Bavix\\\\LaravelClickHouse\\\\Database\\\\Query\\\\Builder\\:\\:\\$connection has no type specified\\.$#" count: 1