Skip to content

Commit bc3df85

Browse files
[5.x] Ability to override how objects are serialized (#1562)
* Implement `formatForTelescope` for events * Implement `formatForTelescope` for gates * StyleCI
1 parent 9073cf8 commit bc3df85

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

src/ExtractProperties.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public static function from($target)
3131
return [
3232
$property->getName() => [
3333
'class' => get_class($value),
34-
'properties' => json_decode(json_encode($value), true),
34+
'properties' => method_exists($value, 'formatForTelescope')
35+
? $value->formatForTelescope()
36+
: json_decode(json_encode($value), true),
3537
],
3638
];
3739
} else {

src/Watchers/GateWatcher.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,15 @@ private function gateResult($result)
9999
private function formatArguments($arguments)
100100
{
101101
return collect($arguments)->map(function ($argument) {
102-
return $argument instanceof Model ? FormatModel::given($argument) : $argument;
102+
if (is_object($argument) && method_exists($argument, 'formatForTelescope')) {
103+
return $argument->formatForTelescope();
104+
}
105+
106+
if ($argument instanceof Model) {
107+
return FormatModel::given($argument);
108+
}
109+
110+
return $argument;
103111
})->toArray();
104112
}
105113
}

tests/Watchers/EventWatcherTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Dummies\DummyEvent;
66
use Dummies\DummyEventListener;
77
use Dummies\DummyEventSubscriber;
8+
use Dummies\DummyEventWithObject;
89
use Dummies\DummyInvokableEventListener;
10+
use Dummies\DummyObject;
911
use Dummies\IgnoredEvent;
1012
use Illuminate\Support\Facades\Event;
1113
use Laravel\Telescope\EntryType;
@@ -60,6 +62,25 @@ public function test_event_watcher_stores_payloads()
6062
$this->assertContains('PHP', $entry->content['payload']['data']);
6163
}
6264

65+
public function test_event_watcher_with_object_property_calls_format_for_telescope_method_if_it_exists()
66+
{
67+
Event::listen(DummyEventWithObject::class, function ($payload) {
68+
//
69+
});
70+
71+
event(new DummyEventWithObject());
72+
73+
$entry = $this->loadTelescopeEntries()->first();
74+
75+
$this->assertSame(EntryType::EVENT, $entry->type);
76+
$this->assertSame(DummyEventWithObject::class, $entry->content['name']);
77+
$this->assertArrayHasKey('thing', $entry->content['payload']);
78+
$this->assertSame(DummyObject::class, $entry->content['payload']['thing']['class']);
79+
$this->assertContains('Telescope', $entry->content['payload']['thing']['properties']);
80+
$this->assertContains('Laravel', $entry->content['payload']['thing']['properties']);
81+
$this->assertContains('PHP', $entry->content['payload']['thing']['properties']);
82+
}
83+
6384
public function test_event_watcher_registers_events_and_stores_payloads_with_subscriber_methods()
6485
{
6586
Event::listen(DummyEvent::class, DummyEventSubscriber::class.'@handleDummyEvent');
@@ -174,6 +195,26 @@ public function handle()
174195
}
175196
}
176197

198+
class DummyEventWithObject
199+
{
200+
public $thing;
201+
202+
public function __construct()
203+
{
204+
$this->thing = new DummyObject;
205+
}
206+
}
207+
208+
class DummyObject
209+
{
210+
public function formatForTelescope(): array
211+
{
212+
return [
213+
'Telescope', 'Laravel', 'PHP',
214+
];
215+
}
216+
}
217+
177218
class DummyEventSubscriber
178219
{
179220
public function handleDummyEvent($event)

tests/Watchers/GateWatcherTest.php

+36-4
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function test_gate_watcher_registers_allowed_policy_entries()
113113

114114
$this->assertSame(EntryType::GATE, $entry->type);
115115
$this->assertSame(__FILE__, $entry->content['file']);
116-
$this->assertSame(271, $entry->content['line']);
116+
$this->assertSame(303, $entry->content['line']);
117117
$this->assertSame('create', $entry->content['ability']);
118118
$this->assertSame('allowed', $entry->content['result']);
119119
$this->assertSame([[]], $entry->content['arguments']);
@@ -156,12 +156,34 @@ public function test_gate_watcher_registers_denied_policy_entries()
156156

157157
$this->assertSame(EntryType::GATE, $entry->type);
158158
$this->assertSame(__FILE__, $entry->content['file']);
159-
$this->assertSame(276, $entry->content['line']);
159+
$this->assertSame(308, $entry->content['line']);
160160
$this->assertSame('update', $entry->content['ability']);
161161
$this->assertSame('denied', $entry->content['result']);
162162
$this->assertSame([[]], $entry->content['arguments']);
163163
}
164164

165+
public function test_gate_watcher_calls_format_for_telescope_method_if_it_exists()
166+
{
167+
$this->app->setBasePath(dirname(__FILE__, 3));
168+
169+
Gate::policy(TestResourceWithFormatForTelescope::class, TestPolicy::class);
170+
171+
try {
172+
(new TestController())->update(new TestResourceWithFormatForTelescope());
173+
} catch (\Exception $ex) {
174+
// ignore
175+
}
176+
177+
$entry = $this->loadTelescopeEntries()->first();
178+
179+
$this->assertSame(EntryType::GATE, $entry->type);
180+
$this->assertSame(__FILE__, $entry->content['file']);
181+
$this->assertSame(308, $entry->content['line']);
182+
$this->assertSame('update', $entry->content['ability']);
183+
$this->assertSame('denied', $entry->content['result']);
184+
$this->assertSame([['Telescope', 'Laravel', 'PHP']], $entry->content['arguments']);
185+
}
186+
165187
public function test_gate_watcher_registers_allowed_response_policy_entries()
166188
{
167189
$this->app->setBasePath(dirname(__FILE__, 3));
@@ -178,7 +200,7 @@ public function test_gate_watcher_registers_allowed_response_policy_entries()
178200

179201
$this->assertSame(EntryType::GATE, $entry->type);
180202
$this->assertSame(__FILE__, $entry->content['file']);
181-
$this->assertSame(266, $entry->content['line']);
203+
$this->assertSame(298, $entry->content['line']);
182204
$this->assertSame('view', $entry->content['ability']);
183205
$this->assertSame('allowed', $entry->content['result']);
184206
$this->assertSame([[]], $entry->content['arguments']);
@@ -200,7 +222,7 @@ public function test_gate_watcher_registers_denied_response_policy_entries()
200222

201223
$this->assertSame(EntryType::GATE, $entry->type);
202224
$this->assertSame(__FILE__, $entry->content['file']);
203-
$this->assertSame(281, $entry->content['line']);
225+
$this->assertSame(313, $entry->content['line']);
204226
$this->assertSame('delete', $entry->content['ability']);
205227
$this->assertSame('denied', $entry->content['result']);
206228
$this->assertSame([[]], $entry->content['arguments']);
@@ -257,6 +279,16 @@ class TestResource
257279
//
258280
}
259281

282+
class TestResourceWithFormatForTelescope
283+
{
284+
public function formatForTelescope(): array
285+
{
286+
return [
287+
'Telescope', 'Laravel', 'PHP',
288+
];
289+
}
290+
}
291+
260292
class TestController
261293
{
262294
use AuthorizesRequests;

0 commit comments

Comments
 (0)