diff --git a/src/Ray.php b/src/Ray.php index ae9c334..964135b 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 ((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();