Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

CacheWatcher hide values #1206

Merged
merged 4 commits into from
Mar 25, 2022
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
31 changes: 29 additions & 2 deletions src/Watchers/CacheWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function recordCacheHit(CacheHit $event)
Telescope::recordCache(IncomingEntry::make([
'type' => 'hit',
'key' => $event->key,
'value' => $event->value,
'value' => $this->formatValue($event),
]));
}

Expand Down Expand Up @@ -79,7 +79,7 @@ public function recordKeyWritten(KeyWritten $event)
Telescope::recordCache(IncomingEntry::make([
'type' => 'set',
'key' => $event->key,
'value' => $event->value,
'value' => $this->formatValue($event),
'expiration' => $this->formatExpiration($event),
]));
}
Expand All @@ -102,6 +102,33 @@ public function recordKeyForgotten(KeyForgotten $event)
]));
}

/**
* Determine the value of an event.
*
* @param mixed $event
* @return mixed
*/
private function formatValue($event)
{
return (! $this->shouldHideValue($event))
? $event->value
: '********';
}

/**
* Determine if the event value should be ignored.
*
* @param mixed $event
* @return bool
*/
private function shouldHideValue($event)
{
return Str::is(
$this->options['hidden'] ?? [],
$event->key
);
}

/**
* @param \Illuminate\Cache\Events\KeyWritten $event
* @return mixed
Expand Down
37 changes: 36 additions & 1 deletion tests/Watchers/CacheWatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ protected function getEnvironmentSetUp($app)
parent::getEnvironmentSetUp($app);

$app->get('config')->set('telescope.watchers', [
CacheWatcher::class => true,
CacheWatcher::class => [
'enabled' => true,
'hidden' => [
'my-hidden-value-key',
],
],
]);
}

Expand Down Expand Up @@ -76,4 +81,34 @@ public function test_cache_watcher_registers_forget_entries()
$this->assertSame('forget', $entry->content['type']);
$this->assertSame('outdated', $entry->content['key']);
}

public function test_cache_watcher_hides_hidden_values_when_set()
{
$this->app->get(Repository::class)->put('my-hidden-value-key', 'laravel', 1);

$entry = $this->loadTelescopeEntries()->first();

$this->assertSame(EntryType::CACHE, $entry->type);
$this->assertSame('set', $entry->content['type']);
$this->assertSame('my-hidden-value-key', $entry->content['key']);
$this->assertSame('********', $entry->content['value']);
}

public function test_cache_watcher_hides_hidden_values_when_retrieved()
{
$repository = $this->app->get(Repository::class);

Telescope::withoutRecording(function () use ($repository) {
$repository->put('my-hidden-value-key', 'laravel', 1);
});

$repository->get('my-hidden-value-key');

$entry = $this->loadTelescopeEntries()->first();

$this->assertSame(EntryType::CACHE, $entry->type);
$this->assertSame('hit', $entry->content['type']);
$this->assertSame('my-hidden-value-key', $entry->content['key']);
$this->assertSame('********', $entry->content['value']);
}
}