From 30a0e4bf69c029fc392c2a44722b73b51ca28a94 Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Tue, 23 Jul 2024 16:16:06 +0200 Subject: [PATCH] Add MySQL Visual Explain macro's for the query builders. --- composer.json | 4 ++- src/Payloads/MySqlVisualExplainPayload.php | 28 +++++++++++++++++++ src/RayServiceProvider.php | 7 +++++ tests/Unit/MySqlVisualExplainTest.php | 32 ++++++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/Payloads/MySqlVisualExplainPayload.php create mode 100644 tests/Unit/MySqlVisualExplainTest.php diff --git a/composer.json b/composer.json index caa7203..4f95b07 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,8 @@ } ], "require": { - "ext-json": "*", "php": "^7.4|^8.0", + "ext-json": "*", "illuminate/contracts": "^7.20|^8.19|^9.0|^10.0|^11.0", "illuminate/database": "^7.20|^8.19|^9.0|^10.0|^11.0", "illuminate/queue": "^7.20|^8.19|^9.0|^10.0|^11.0", @@ -26,11 +26,13 @@ "spatie/backtrace": "^1.0", "spatie/ray": "^1.41.1", "symfony/stopwatch": "4.2|^5.1|^6.0|^7.0", + "tpetry/laravel-mysql-explain": "^1.3", "zbateson/mail-mime-parser": "^1.3.1|^2.0|^3.0" }, "require-dev": { "guzzlehttp/guzzle": "^7.3", "laravel/framework": "^7.20|^8.19|^9.0|^10.0|^11.0", + "mockery/mockery": "^1.6", "orchestra/testbench-core": "^5.0|^6.0|^7.0|^8.0|^9.0", "pestphp/pest": "^1.22|^2.0", "phpstan/phpstan": "^1.10.57", diff --git a/src/Payloads/MySqlVisualExplainPayload.php b/src/Payloads/MySqlVisualExplainPayload.php new file mode 100644 index 0000000..034050f --- /dev/null +++ b/src/Payloads/MySqlVisualExplainPayload.php @@ -0,0 +1,28 @@ +url = $url; + + } + + public function getType(): string + { + return 'mysql_visual_explain'; + } + + public function getContent(): array + { + return [ + 'url' => $this->url, + ]; + } +} diff --git a/src/RayServiceProvider.php b/src/RayServiceProvider.php index 489185c..1c60ef2 100644 --- a/src/RayServiceProvider.php +++ b/src/RayServiceProvider.php @@ -15,6 +15,7 @@ use Spatie\LaravelRay\Commands\PublishConfigCommand; use Spatie\LaravelRay\Payloads\MailablePayload; use Spatie\LaravelRay\Payloads\ModelPayload; +use Spatie\LaravelRay\Payloads\MySqlVisualExplainPayload; use Spatie\LaravelRay\Payloads\QueryPayload; use Spatie\LaravelRay\Watchers\ApplicationLogWatcher; use Spatie\LaravelRay\Watchers\CacheWatcher; @@ -221,6 +222,12 @@ protected function registerMacros(): self return $this; }); + Builder::macro('rayVisualExplain', function () { + $payload = new MySqlVisualExplainPayload($this->visualExplain()); + + ray()->sendRequest($payload); + }); + return $this; } diff --git a/tests/Unit/MySqlVisualExplainTest.php b/tests/Unit/MySqlVisualExplainTest.php new file mode 100644 index 0000000..3e2cabb --- /dev/null +++ b/tests/Unit/MySqlVisualExplainTest.php @@ -0,0 +1,32 @@ +once() + ->andReturn('https://dummy-url-f6V7VImZnz.local'); + + $builder->rayVisualExplain(); + + expect($this->client->sentPayloads())->toHaveCount(1); + + $payload = $this->client->sentPayloads()[0]; + + expect(Arr::get($payload, 'type'))->toEqual('mysql_visual_explain'); + expect($payload['content']['url'])->toEqual('https://dummy-url-f6V7VImZnz.local'); +})->with([ + fn() => User::where('email', 'john@example.com'), + fn() => DB::table('users')->where('email', 'john@example.com') +]);