diff --git a/src/ExtractProperties.php b/src/ExtractProperties.php index a61f26910..47e1f274b 100644 --- a/src/ExtractProperties.php +++ b/src/ExtractProperties.php @@ -31,7 +31,9 @@ public static function from($target) return [ $property->getName() => [ '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 { diff --git a/src/Watchers/GateWatcher.php b/src/Watchers/GateWatcher.php index 3ee42a646..431613597 100644 --- a/src/Watchers/GateWatcher.php +++ b/src/Watchers/GateWatcher.php @@ -99,7 +99,15 @@ private function gateResult($result) private function formatArguments($arguments) { return collect($arguments)->map(function ($argument) { - return $argument instanceof Model ? FormatModel::given($argument) : $argument; + if (is_object($argument) && method_exists($argument, 'formatForTelescope')) { + return $argument->formatForTelescope(); + } + + if ($argument instanceof Model) { + return FormatModel::given($argument); + } + + return $argument; })->toArray(); } } diff --git a/tests/Watchers/EventWatcherTest.php b/tests/Watchers/EventWatcherTest.php index fcd7cd1b3..b7cbd8b3b 100644 --- a/tests/Watchers/EventWatcherTest.php +++ b/tests/Watchers/EventWatcherTest.php @@ -5,7 +5,9 @@ use Dummies\DummyEvent; use Dummies\DummyEventListener; use Dummies\DummyEventSubscriber; +use Dummies\DummyEventWithObject; use Dummies\DummyInvokableEventListener; +use Dummies\DummyObject; use Dummies\IgnoredEvent; use Illuminate\Support\Facades\Event; use Laravel\Telescope\EntryType; @@ -60,6 +62,25 @@ public function test_event_watcher_stores_payloads() $this->assertContains('PHP', $entry->content['payload']['data']); } + public function test_event_watcher_with_object_property_calls_format_for_telescope_method_if_it_exists() + { + Event::listen(DummyEventWithObject::class, function ($payload) { + // + }); + + event(new DummyEventWithObject()); + + $entry = $this->loadTelescopeEntries()->first(); + + $this->assertSame(EntryType::EVENT, $entry->type); + $this->assertSame(DummyEventWithObject::class, $entry->content['name']); + $this->assertArrayHasKey('thing', $entry->content['payload']); + $this->assertSame(DummyObject::class, $entry->content['payload']['thing']['class']); + $this->assertContains('Telescope', $entry->content['payload']['thing']['properties']); + $this->assertContains('Laravel', $entry->content['payload']['thing']['properties']); + $this->assertContains('PHP', $entry->content['payload']['thing']['properties']); + } + public function test_event_watcher_registers_events_and_stores_payloads_with_subscriber_methods() { Event::listen(DummyEvent::class, DummyEventSubscriber::class.'@handleDummyEvent'); @@ -174,6 +195,26 @@ public function handle() } } +class DummyEventWithObject +{ + public $thing; + + public function __construct() + { + $this->thing = new DummyObject; + } +} + +class DummyObject +{ + public function formatForTelescope(): array + { + return [ + 'Telescope', 'Laravel', 'PHP', + ]; + } +} + class DummyEventSubscriber { public function handleDummyEvent($event) diff --git a/tests/Watchers/GateWatcherTest.php b/tests/Watchers/GateWatcherTest.php index ce30d0349..e6fdf5375 100644 --- a/tests/Watchers/GateWatcherTest.php +++ b/tests/Watchers/GateWatcherTest.php @@ -113,7 +113,7 @@ public function test_gate_watcher_registers_allowed_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(271, $entry->content['line']); + $this->assertSame(303, $entry->content['line']); $this->assertSame('create', $entry->content['ability']); $this->assertSame('allowed', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); @@ -156,12 +156,34 @@ public function test_gate_watcher_registers_denied_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(276, $entry->content['line']); + $this->assertSame(308, $entry->content['line']); $this->assertSame('update', $entry->content['ability']); $this->assertSame('denied', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); } + public function test_gate_watcher_calls_format_for_telescope_method_if_it_exists() + { + $this->app->setBasePath(dirname(__FILE__, 3)); + + Gate::policy(TestResourceWithFormatForTelescope::class, TestPolicy::class); + + try { + (new TestController())->update(new TestResourceWithFormatForTelescope()); + } catch (\Exception $ex) { + // ignore + } + + $entry = $this->loadTelescopeEntries()->first(); + + $this->assertSame(EntryType::GATE, $entry->type); + $this->assertSame(__FILE__, $entry->content['file']); + $this->assertSame(308, $entry->content['line']); + $this->assertSame('update', $entry->content['ability']); + $this->assertSame('denied', $entry->content['result']); + $this->assertSame([['Telescope', 'Laravel', 'PHP']], $entry->content['arguments']); + } + public function test_gate_watcher_registers_allowed_response_policy_entries() { $this->app->setBasePath(dirname(__FILE__, 3)); @@ -178,7 +200,7 @@ public function test_gate_watcher_registers_allowed_response_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(266, $entry->content['line']); + $this->assertSame(298, $entry->content['line']); $this->assertSame('view', $entry->content['ability']); $this->assertSame('allowed', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); @@ -200,7 +222,7 @@ public function test_gate_watcher_registers_denied_response_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(281, $entry->content['line']); + $this->assertSame(313, $entry->content['line']); $this->assertSame('delete', $entry->content['ability']); $this->assertSame('denied', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); @@ -257,6 +279,16 @@ class TestResource // } +class TestResourceWithFormatForTelescope +{ + public function formatForTelescope(): array + { + return [ + 'Telescope', 'Laravel', 'PHP', + ]; + } +} + class TestController { use AuthorizesRequests;