Skip to content

Commit

Permalink
Ensure formatForTelescope method is used in request watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Feb 6, 2025
1 parent 3c82891 commit b6029f5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Watchers/RequestWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ protected function extractDataFromView($view)
} elseif (is_object($value)) {
return [
'class' => get_class($value),
'properties' => json_decode(json_encode($value), true),
'properties' => method_exists($value, 'formatForTelescope')
? $value->formatForTelescope()
: json_decode(json_encode($value), true),
];
} else {
return json_decode(json_encode($value), true);
Expand Down
28 changes: 28 additions & 0 deletions tests/Watchers/RequestWatchersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
use Laravel\Telescope\EntryType;
use Laravel\Telescope\Tests\FeatureTestCase;
use Laravel\Telescope\Watchers\RequestWatcher;
Expand Down Expand Up @@ -176,4 +177,31 @@ public function test_request_watcher_plain_text_response()
$this->assertSame(200, $entry->content['response_status']);
$this->assertSame('plain telescope response', $entry->content['response']);
}

public function test_request_watcher_calls_format_for_telescope_method_if_it_exists()
{
View::addNamespace('tests', __DIR__.'/../stubs/views');

Route::get('/fake-view', function () {
return Response::make(
View::make('tests::fake-view', ['items' => new FormatForTelescopeClass])
);
});

$this->get('/fake-view')->assertSuccessful();

$entry = $this->loadTelescopeEntries()->first();
$this->assertSame(EntryType::REQUEST, $entry->type);
$this->assertEquals(['Telescope', 'Laravel', 'PHP'], $entry->content['response']['data']['items']['properties']);
}
}

class FormatForTelescopeClass
{
public function formatForTelescope(): array
{
return [
'Telescope', 'Laravel', 'PHP',
];
}
}
3 changes: 3 additions & 0 deletions tests/stubs/views/fake-view.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@foreach($items as $item)
<div>{{ $item }}</div>
@endforeach

0 comments on commit b6029f5

Please # to comment.