Skip to content

Commit

Permalink
[1.x] Rename highlighting prop (#362)
Browse files Browse the repository at this point in the history
* Rename highlighting prop

* Make test output verbose

* Formatting

* Require latest due to casing changes
  • Loading branch information
timacdonald authored May 6, 2024
1 parent 24c71bf commit 16949d5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions resources/views/livewire/slow-queries.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand Down Expand Up @@ -62,7 +62,7 @@
<x-pulse::td class="!p-0 truncate max-w-[1px]">
<div class="relative">
<div class="bg-gray-700 dark:bg-gray-800 py-4 rounded-md text-gray-100 block text-xs whitespace-nowrap overflow-x-auto [scrollbar-color:theme(colors.gray.500)_transparent] [scrollbar-width:thin]">
<code class="px-3">{!! $this->disableHighlighting ? $query->sql : $sqlFormatter->highlight($query->sql) !!}</code>
<code class="px-3">{!! $this->wantsHighlighting() ? $sqlFormatter->highlight($query->sql) : $query->sql !!}</code>
@if ($query->location)
<p class="px-3 mt-3 text-xs leading-none text-gray-400 dark:text-gray-500">
{{ $query->location }}
Expand Down
15 changes: 15 additions & 0 deletions src/Livewire/SlowQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
}
39 changes: 39 additions & 0 deletions tests/Feature/Livewire/SlowQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<code class="px-3"><span class="font-semibold">SELECT</span> <span class="text-cyan-200">*</span> <span class="font-semibold">FROM</span> <span class="text-purple-200">`users`</span></code>
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'
<code class="px-3">select * from `users`</code>
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'
<code class="px-3">select * from `users`</code>
HTML);
});

0 comments on commit 16949d5

Please sign in to comment.