Skip to content

Commit

Permalink
Support Laravel 7
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickomeara committed Dec 3, 2024
1 parent 114c2ba commit 8570d28
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Watchers/DeleteQueryWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
}
2 changes: 1 addition & 1 deletion src/Watchers/InsertQueryWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
}
2 changes: 1 addition & 1 deletion src/Watchers/SelectQueryWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
}
2 changes: 1 addition & 1 deletion src/Watchers/UpdateQueryWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
}
13 changes: 13 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
32 changes: 16 additions & 16 deletions tests/Unit/ConditionalQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
$this->assertSame('[email protected]', $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' => '[email protected]']);
Expand All @@ -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' => '[email protected]']);
Expand All @@ -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' => '[email protected]']);
Expand All @@ -61,7 +61,7 @@
expect($this->client->sentPayloads())->toHaveCount(1);

$payload = $this->client->sentPayloads();
$this->assertStringContainsString('[email protected]', Arr::get($payload, '0.content.sql'));
$this->assertSqlContains(Arr::get($payload, '0.content'), '[email protected]');

ray()->stopShowingConditionalQueries();

Expand All @@ -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'
Expand All @@ -101,11 +101,11 @@ function (QueryExecuted $query) {
$payload = $this->client->sentPayloads();

// Assert that ray received the correct order
$this->assertStringContainsString('[email protected]', Arr::get($payload, '0.content.sql'));
$this->assertStringContainsString('[email protected]', Arr::get($payload, '1.content.sql'));
$this->assertSqlContains(Arr::get($payload, '0.content'), '[email protected]');
$this->assertSqlContains(Arr::get($payload, '1.content'), '[email protected]');

// Looking for joan has been disabled so this should not be sent
$joan = User::query()->where('email', '[email protected]')->sole();
$joan = User::query()->where('email', '[email protected]')->first();
expect($this->client->sentPayloads())->toHaveCount(2);

// Looking for john is still enabled so this should be sent
Expand Down Expand Up @@ -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');
});

0 comments on commit 8570d28

Please sign in to comment.