Skip to content

Commit 2eddf10

Browse files
committed
Implement formatForTelescope for gates
1 parent 281bf6d commit 2eddf10

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

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/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)