Skip to content

Commit

Permalink
Add MySQL Visual Explain macro's for the query builders.
Browse files Browse the repository at this point in the history
  • Loading branch information
timvandijck committed Jul 23, 2024
1 parent c2bedfd commit 30a0e4b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions src/Payloads/MySqlVisualExplainPayload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Spatie\LaravelRay\Payloads;

use Spatie\Ray\Payloads\Payload;

class MySqlVisualExplainPayload extends Payload
{
protected string $url;

public function __construct(string $url)
{
$this->url = $url;

}

public function getType(): string
{
return 'mysql_visual_explain';
}

public function getContent(): array
{
return [
'url' => $this->url,
];
}
}
7 changes: 7 additions & 0 deletions src/RayServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -221,6 +222,12 @@ protected function registerMacros(): self
return $this;
});

Builder::macro('rayVisualExplain', function () {
$payload = new MySqlVisualExplainPayload($this->visualExplain());

Check failure on line 226 in src/RayServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Query\Builder::visualExplain().

ray()->sendRequest($payload);
});

return $this;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/MySqlVisualExplainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Spatie\LaravelRay\Tests\TestClasses\User;
use Tpetry\MysqlExplain\Facades\MysqlExplain;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Tpetry\MysqlExplain\Mixins\BuilderMixin;

beforeEach(function() {
EloquentBuilder::mixin(new BuilderMixin());
QueryBuilder::mixin(new BuilderMixin());
});

it('works', function ($builder) {
MysqlExplain::shouldReceive('submitBuilder')
->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', '[email protected]'),
fn() => DB::table('users')->where('email', '[email protected]')
]);

0 comments on commit 30a0e4b

Please sign in to comment.