From f95714fabc985309b3aa751525eef47fca7463de Mon Sep 17 00:00:00 2001 From: Grant Holle Date: Sat, 2 Sep 2023 08:55:13 +0400 Subject: [PATCH 1/2] Add ability to return the results of callable This adds the ability to return the results of a callable rather than an instance of `Ray`. If the closure has a return type, assume that the results of the callable should be return rather than `Ray`. --- src/Ray.php | 13 ++++++++++--- tests/Unit/QueryTest.php | 10 ++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Ray.php b/src/Ray.php index ae9c334..3437124 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -15,6 +15,7 @@ use Illuminate\Support\Testing\Fakes\MailFake; use Illuminate\Testing\TestResponse; use Illuminate\View\View; +use ReflectionFunction; use Spatie\LaravelRay\Payloads\EnvironmentPayload; use Spatie\LaravelRay\Payloads\ExecutedQueryPayload; use Spatie\LaravelRay\Payloads\LoggedMailPayload; @@ -304,7 +305,7 @@ public function countQueries(callable $callable) $watcher->doNotSendIndividualQueries(); } - $this->handleWatcherCallable($watcher, $callable); + $output = $this->handleWatcherCallable($watcher, $callable); $executedQueryStatistics = collect($watcher->getExecutedQueries()) @@ -324,6 +325,8 @@ public function countQueries(callable $callable) ->sendIndividualQueries(); $this->table($executedQueryStatistics, 'Queries'); + + return $output; } public function queries($callable = null) @@ -431,7 +434,7 @@ public function stopShowingHttpClientRequests(): self return $this; } - protected function handleWatcherCallable(Watcher $watcher, Closure $callable = null): RayProxy + protected function handleWatcherCallable(Watcher $watcher, Closure $callable = null) { $rayProxy = new RayProxy(); @@ -444,11 +447,15 @@ protected function handleWatcherCallable(Watcher $watcher, Closure $callable = n } if ($callable) { - $callable(); + $output = $callable(); if (! $wasEnabled) { $watcher->disable(); } + + if ($output && (new ReflectionFunction($callable))->hasReturnType()) { + return $output; + } } return $rayProxy; diff --git a/tests/Unit/QueryTest.php b/tests/Unit/QueryTest.php index 762f73f..1aabf50 100644 --- a/tests/Unit/QueryTest.php +++ b/tests/Unit/QueryTest.php @@ -53,6 +53,16 @@ expect($this->client->sentRequests())->toHaveCount(1); }); +it('can log all queries in a callable and gets results', function () { + $results = ray()->showQueries(function (): \Illuminate\Support\Collection { + // will be logged + return DB::table('users')->where('id', 1)->get(); + }); + expect($this->client->sentRequests())->toHaveCount(1); + expect($results)->toBeInstanceOf(\Illuminate\Support\Collection::class); + expect($results->count())->toEqual(0); +}); + it('show queries can be colorized', function () { $this->useRealUuid(); From db8293abfcff36a6be064d625ce0055ba3b2260c Mon Sep 17 00:00:00 2001 From: Grant Holle Date: Sat, 2 Sep 2023 10:12:53 +0400 Subject: [PATCH 2/2] Always return callable results --- src/Ray.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ray.php b/src/Ray.php index 3437124..964135b 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -453,7 +453,7 @@ protected function handleWatcherCallable(Watcher $watcher, Closure $callable = n $watcher->disable(); } - if ($output && (new ReflectionFunction($callable))->hasReturnType()) { + if ((new ReflectionFunction($callable))->hasReturnType()) { return $output; } }