diff --git a/src/Watchers/DeleteQueryWatcher.php b/src/Watchers/DeleteQueryWatcher.php index 766c3ac..000cbdb 100644 --- a/src/Watchers/DeleteQueryWatcher.php +++ b/src/Watchers/DeleteQueryWatcher.php @@ -15,7 +15,7 @@ public function register(): void $this->enabled = $settings->send_delete_queries_to_ray ?? false; $this->setConditionalCallback(function (QueryExecuted $query) { - return Str::startsWith(strtolower($query->toRawSql()), 'delete'); + return Str::startsWith(strtolower($query->sql), 'delete'); }); } } diff --git a/src/Watchers/InsertQueryWatcher.php b/src/Watchers/InsertQueryWatcher.php index acc0419..ac581d1 100644 --- a/src/Watchers/InsertQueryWatcher.php +++ b/src/Watchers/InsertQueryWatcher.php @@ -15,7 +15,7 @@ public function register(): void $this->enabled = $settings->send_insert_queries_to_ray ?? false; $this->setConditionalCallback(function (QueryExecuted $query) { - return Str::startsWith(strtolower($query->toRawSql()), 'insert'); + return Str::startsWith(strtolower($query->sql), 'insert'); }); } } diff --git a/src/Watchers/SelectQueryWatcher.php b/src/Watchers/SelectQueryWatcher.php index a403dbb..2794c45 100644 --- a/src/Watchers/SelectQueryWatcher.php +++ b/src/Watchers/SelectQueryWatcher.php @@ -15,7 +15,7 @@ public function register(): void $this->enabled = $settings->send_select_queries_to_ray ?? false; $this->setConditionalCallback(function (QueryExecuted $query) { - return Str::startsWith(strtolower($query->toRawSql()), 'select'); + return Str::startsWith(strtolower($query->sql), 'select'); }); } } diff --git a/src/Watchers/UpdateQueryWatcher.php b/src/Watchers/UpdateQueryWatcher.php index 10d0398..f884434 100644 --- a/src/Watchers/UpdateQueryWatcher.php +++ b/src/Watchers/UpdateQueryWatcher.php @@ -15,7 +15,7 @@ public function register(): void $this->enabled = $settings->send_update_queries_to_ray ?? false; $this->setConditionalCallback(function (QueryExecuted $query) { - return Str::startsWith(strtolower($query->toRawSql()), 'update'); + return Str::startsWith(strtolower($query->sql), 'update'); }); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 34eafd9..d4f258a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,13 @@ namespace Spatie\LaravelRay\Tests; +use Illuminate\Database\Events\QueryExecuted; +use Illuminate\Database\Query\Builder; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\View; +use Illuminate\Support\Str; use Orchestra\Testbench\TestCase as Orchestra; use Spatie\LaravelRay\Ray; use Spatie\LaravelRay\RayServiceProvider; @@ -70,4 +74,13 @@ protected function useRealUuid() return Ray::create($this->client); }); } + + protected function assertSqlContains($queryContent, $needle): void + { + $sql = method_exists(Builder::class, 'toRawSql') + ? $queryContent['sql'] + : Str::replaceArray('?', $queryContent['bindings'], $queryContent['sql']); + + $this->assertStringContainsString($needle, $sql); + } } diff --git a/tests/Unit/ConditionalQueryTest.php b/tests/Unit/ConditionalQueryTest.php index 58c362d..06f6a8b 100644 --- a/tests/Unit/ConditionalQueryTest.php +++ b/tests/Unit/ConditionalQueryTest.php @@ -21,8 +21,8 @@ $this->assertSame('joan@example.com', $user->email); }); -it('can show only type queries', function (Closure $rayShowMethod, Closure $rayStopMethod, string $sqlCommand) { - $rayShowMethod(); +it('can show only type queries', function (string $rayShowMethod, string $rayStopMethod, string $sqlCommand) { + ray()->$rayShowMethod(); // Run all query types $user = User::query()->firstOrCreate(['email' => 'john@example.com']); @@ -33,9 +33,9 @@ // Assert the one we want is picked up. $payload = $this->client->sentPayloads(); - $this->assertStringStartsWith($sqlCommand, Arr::get($payload, '0.content.sql')); + $this->assertSqlContains(Arr::get($payload, '0.content'), $sqlCommand); - $rayStopMethod(); + ray()->$rayStopMethod(); // Assert that watcher has stopped. $user = User::query()->firstOrCreate(['email' => 'sam@example.com']); @@ -44,15 +44,15 @@ expect($this->client->sentPayloads())->toHaveCount(1); })->with([ - 'update' => [function () {ray()->showUpdateQueries();}, function () {ray()->stopShowingUpdateQueries();}, 'update'], - 'delete' => [function () {ray()->showDeleteQueries();}, function () {ray()->stopShowingDeleteQueries();}, 'delete'], - 'insert' => [function () {ray()->showInsertQueries();}, function () {ray()->stopShowingInsertQueries();}, 'insert'], - 'select' => [function () {ray()->showSelectQueries();}, function () {ray()->stopShowingSelectQueries();}, 'select'], + 'update' => ['showUpdateQueries', 'stopShowingUpdateQueries', 'update'], + 'delete' => ['showDeleteQueries', 'stopShowingDeleteQueries', 'delete'], + 'insert' => ['showInsertQueries', 'stopShowingInsertQueries', 'insert'], + 'select' => ['showSelectQueries', 'stopShowingSelectQueries', 'select'], ]); it('can take a custom condition and only return those queries', function () { ray()->showConditionalQueries(function (QueryExecuted $query) { - return Str::contains($query->toRawSql(), 'joan'); + return Arr::first($query->bindings, fn ($binding) => Str::contains($binding, 'joan')); }); User::query()->create(['email' => 'joan@example.com']); @@ -61,7 +61,7 @@ expect($this->client->sentPayloads())->toHaveCount(1); $payload = $this->client->sentPayloads(); - $this->assertStringContainsString('joan@example.com', Arr::get($payload, '0.content.sql')); + $this->assertSqlContains(Arr::get($payload, '0.content'), 'joan@example.com'); ray()->stopShowingConditionalQueries(); @@ -73,12 +73,12 @@ it('can handle multiple conditional query watchers', function () { $john = ray()->showConditionalQueries( function (QueryExecuted $query) { - return Str::contains($query->toRawSql(), 'joan'); + return Arr::first($query->bindings, fn ($binding) => Str::contains($binding, 'joan')); }, function (): User { ray()->showConditionalQueries( function (QueryExecuted $query) { - return Str::contains($query->toRawSql(), 'john'); + return Arr::first($query->bindings, fn ($binding) => Str::contains($binding, 'john')); }, null, 'look for john' @@ -101,11 +101,11 @@ function (QueryExecuted $query) { $payload = $this->client->sentPayloads(); // Assert that ray received the correct order - $this->assertStringContainsString('joan@example.com', Arr::get($payload, '0.content.sql')); - $this->assertStringContainsString('john@example.com', Arr::get($payload, '1.content.sql')); + $this->assertSqlContains(Arr::get($payload, '0.content'), 'joan@example.com'); + $this->assertSqlContains(Arr::get($payload, '1.content'), 'john@example.com'); // Looking for joan has been disabled so this should not be sent - $joan = User::query()->where('email', 'joan@example.com')->sole(); + $joan = User::query()->where('email', 'joan@example.com')->first(); expect($this->client->sentPayloads())->toHaveCount(2); // Looking for john is still enabled so this should be sent @@ -134,5 +134,5 @@ function (QueryExecuted $query) { expect($this->client->sentPayloads())->toHaveCount(1); $payload = $this->client->sentPayloads(); - $this->assertStringStartsWith('select', Arr::get($payload, '0.content.sql')); + $this->assertSqlContains(Arr::get($payload, '0.content'), 'select'); });