Skip to content

Commit

Permalink
Listen Method Cleanup. (#3)
Browse files Browse the repository at this point in the history
removed skip on test & cleaned up listen method on query watcher.
  • Loading branch information
yordadev authored Aug 22, 2022
1 parent 894b08d commit ddc5e63
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
16 changes: 9 additions & 7 deletions src/QueryWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace YorCreative\QueryWatcher;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use YorCreative\QueryWatcher\Events\QueryEvent;

Expand All @@ -12,19 +13,20 @@ class QueryWatcher
*/
public static function listen(): void
{
if (config('querywatcher.enabled')) {
DB::enableQueryLog();

! self::isQueryWatcherEnabled() ?:
DB::listen(function ($query) {
$time_exceeds_ms_enabled = self::timeExceedsMsEnabled();

if (isset($time_exceeds_ms_enabled)
&& self::getTimeExceedsMs() < $query->time
|| ! isset($time_exceeds_ms_enabled)) {
if ($time_exceeds_ms_enabled && self::getTimeExceedsMs() < $query->time
|| ! $time_exceeds_ms_enabled) {
event(new QueryEvent($query));
}
});
}
}

public static function isQueryWatcherEnabled()
{
return Config::get('querywatcher.enabled') ?? false;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions tests/Feature/CaptureQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ class CaptureQueryTest extends TestCase
*/
public function it_can_capture_a_query()
{
$this->markTestSkipped('Uncomment for local development testing. Fails Github Pipeline Test.');

HTTP::fake();

(new Test())
->newQuery()
->get();

$this->assertEventBroadcasted(
'query.event',
'private-query.event.'.config('querywatcher.token'),
1
'query.event'
);
}

Expand Down

0 comments on commit ddc5e63

Please sign in to comment.