From 16949d58a662da03a45ba864214eb486182820ea Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Tue, 7 May 2024 03:11:04 +1000 Subject: [PATCH] [1.x] Rename highlighting prop (#362) * Rename highlighting prop * Make test output verbose * Formatting * Require latest due to casing changes --- .github/workflows/tests.yml | 8 ++-- composer.json | 2 +- .../views/livewire/slow-queries.blade.php | 4 +- src/Livewire/SlowQueries.php | 15 +++++++ tests/Feature/Livewire/SlowQueriesTest.php | 39 +++++++++++++++++++ 5 files changed, 61 insertions(+), 7 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 19d52514..1cf30892 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -60,7 +60,7 @@ jobs: composer update --prefer-dist --no-interaction --no-progress --${{ matrix.stability }} - name: Execute tests - run: vendor/bin/pest + run: vendor/bin/pest -vvv env: DB_CONNECTION: mysql DB_DATABASE: pulse @@ -118,7 +118,7 @@ jobs: composer update --prefer-dist --no-interaction --no-progress --${{ matrix.stability }} - name: Execute tests - run: vendor/bin/pest + run: vendor/bin/pest -vvv env: DB_CONNECTION: mariadb DB_DATABASE: pulse @@ -175,7 +175,7 @@ jobs: composer update --prefer-dist --no-interaction --no-progress - name: Execute tests - run: vendor/bin/pest + run: vendor/bin/pest -vvv env: DB_CONNECTION: pgsql DB_DATABASE: pulse @@ -222,6 +222,6 @@ jobs: composer update --prefer-dist --no-interaction --no-progress --${{ matrix.stability }} - name: Execute tests - run: vendor/bin/pest + run: vendor/bin/pest -vvv env: DB_CONNECTION: sqlite diff --git a/composer.json b/composer.json index a1b416e4..d2853718 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ ], "require": { "php": "^8.1", - "doctrine/sql-formatter": "^1.1", + "doctrine/sql-formatter": "^1.2", "guzzlehttp/promises": "^1.0|^2.0", "illuminate/auth": "^10.48.4|^11.0.8", "illuminate/cache": "^10.48.4|^11.0.8", diff --git a/resources/views/livewire/slow-queries.blade.php b/resources/views/livewire/slow-queries.blade.php index fba7a72e..0b3672b6 100644 --- a/resources/views/livewire/slow-queries.blade.php +++ b/resources/views/livewire/slow-queries.blade.php @@ -2,7 +2,7 @@ use \Doctrine\SqlFormatter\HtmlHighlighter; use \Doctrine\SqlFormatter\SqlFormatter; -if (! $this->disableHighlighting) { +if ($this->wantsHighlighting()) { $sqlFormatter = new SqlFormatter(new HtmlHighlighter([ HtmlHighlighter::HIGHLIGHT_RESERVED => 'class="font-semibold"', HtmlHighlighter::HIGHLIGHT_QUOTE => 'class="text-purple-200"', @@ -62,7 +62,7 @@
- {!! $this->disableHighlighting ? $query->sql : $sqlFormatter->highlight($query->sql) !!} + {!! $this->wantsHighlighting() ? $sqlFormatter->highlight($query->sql) : $query->sql !!} @if ($query->location)

{{ $query->location }} diff --git a/src/Livewire/SlowQueries.php b/src/Livewire/SlowQueries.php index 39df62ea..e8ff3a5d 100644 --- a/src/Livewire/SlowQueries.php +++ b/src/Livewire/SlowQueries.php @@ -26,6 +26,13 @@ class SlowQueries extends Card /** * Indicates that SQL highlighting should be disabled. */ + public bool $withoutHighlighting = false; + + /** + * Indicates that SQL highlighting should be disabled. + * + * @deprecated + */ public bool $disableHighlighting = false; /** @@ -61,4 +68,12 @@ public function render(): Renderable 'slowQueries' => $slowQueries, ]); } + + /** + * Determine if the view should highlight SQL queries. + */ + protected function wantsHighlighting(): bool + { + return ! ($this->withoutHighlighting || $this->disableHighlighting); + } } diff --git a/tests/Feature/Livewire/SlowQueriesTest.php b/tests/Feature/Livewire/SlowQueriesTest.php index 207db56b..faf981c5 100644 --- a/tests/Feature/Livewire/SlowQueriesTest.php +++ b/tests/Feature/Livewire/SlowQueriesTest.php @@ -40,3 +40,42 @@ (object) ['sql' => 'select * from `users` where `id` = ?', 'location' => 'app/Bar.php:456', 'count' => 2, 'slowest' => 1234], ])); }); + +it('highlights SQL queries', function () { + Carbon::setTestNow('2000-01-01 13:00:00'); + $query = json_encode(['select * from `users`', 'app/Foo.php:123']); + + Pulse::record('slow_query', $query, 1000)->max()->count(); + Pulse::ingest(); + + Livewire::test(SlowQueries::class, ['lazy' => false]) + ->assertSeeHtml(<<<'HTML' + SELECT * FROM `users` + HTML); +}); + +it('can opt out of syntax highlighting', function () { + Carbon::setTestNow('2000-01-01 13:00:00'); + $query = json_encode(['select * from `users`', 'app/Foo.php:123']); + + Pulse::record('slow_query', $query, 1000)->max()->count(); + Pulse::ingest(); + + Livewire::test(SlowQueries::class, ['lazy' => false, 'withoutHighlighting' => true]) + ->assertSeeHtml(<<<'HTML' + select * from `users` + HTML); +}); + +it('can opt out of syntax highlighting with deprecated property', function () { + Carbon::setTestNow('2000-01-01 13:00:00'); + $query = json_encode(['select * from `users`', 'app/Foo.php:123']); + + Pulse::record('slow_query', $query, 1000)->max()->count(); + Pulse::ingest(); + + Livewire::test(SlowQueries::class, ['lazy' => false, 'disableHighlighting' => true]) + ->assertSeeHtml(<<<'HTML' + select * from `users` + HTML); +});