Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ensure formatForTelescope method is used in request watcher #1566

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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