Skip to content

Commit a06c897

Browse files
committed
Remove value preview functionality and enhance test coverage
- Remove getValuePreview() method and related value display functionality - Remove ValuePreviewTest.php and related test cases - Simplify command output to show only cache keys without values - Add comprehensive unit tests for CacheUiLaravelCommand private methods - Add tests for CacheUiLaravelServiceProvider and CacheUiLaravel Facade - Improve test coverage across all package components - Update command interface to focus on key listing and deletion only
1 parent 7660c1f commit a06c897

File tree

7 files changed

+193
-240
lines changed

7 files changed

+193
-240
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Are you sure you want to delete this cache key? › No / Yes
103103
🗑️ The key 'user_1_profile' has been successfully deleted
104104
```
105105
106-
### Programmatic Usage
106+
### Programmatic Usage (optional)
107107
108108
You can also use the `CacheUiLaravel` class directly in your code:
109109

src/Commands/CacheUiLaravelCommand.php

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,8 @@ public function handle(): int
6161
return self::SUCCESS;
6262
}
6363

64-
// Get the key value to show information
65-
// For Redis, we need to add the prefix back since we removed it when listing keys
66-
if ($this->driver === 'redis') {
67-
$prefix = config('database.redis.options.prefix', '');
68-
$fullKey = $prefix ? $prefix.$selectedKey : $selectedKey;
69-
$value = $this->store->get($fullKey);
70-
} else {
71-
$value = $this->store->get($selectedKey);
72-
}
73-
74-
// If value is still null, try different approaches based on driver
75-
if ($value === null && $this->driver === 'file') {
76-
// For file driver, we need to find the file that contains this key
77-
$value = $this->getFileKeyValueByKey($selectedKey);
78-
}
79-
80-
$valuePreview = $this->getValuePreview($value);
81-
8264
$this->newLine();
8365
$this->line("📝 <fg=cyan>Key:</> {$selectedKey}");
84-
$this->line("💾 <fg=cyan>Value:</> {$valuePreview}");
8566
$this->newLine();
8667

8768
$confirmed = confirm(
@@ -236,80 +217,6 @@ private function handleUnsupportedDriver(): array
236217
return [];
237218
}
238219

239-
private function getValuePreview(mixed $value): string
240-
{
241-
$previewLimit = config('cache-ui-laravel.preview_limit', 100);
242-
243-
if (is_null($value)) {
244-
return '<fg=gray>(null)</>';
245-
}
246-
247-
if (is_bool($value)) {
248-
return $value ? '<fg=green>true</>' : '<fg=red>false</>';
249-
}
250-
251-
if (is_array($value) || is_object($value)) {
252-
$json = json_encode($value, JSON_UNESCAPED_UNICODE);
253-
if (mb_strlen($json) > $previewLimit) {
254-
return mb_substr($json, 0, $previewLimit).'<fg=gray>...</>';
255-
}
256-
257-
return $json;
258-
}
259-
260-
$stringValue = (string) $value;
261-
if (mb_strlen($stringValue) > $previewLimit) {
262-
return mb_substr($stringValue, 0, $previewLimit).'<fg=gray>...</>';
263-
}
264-
265-
return $stringValue;
266-
}
267-
268-
private function getFileKeyValueByKey(string $key): mixed
269-
{
270-
try {
271-
$cachePath = config('cache.stores.file.path', storage_path('framework/cache/data'));
272-
273-
if (! File::exists($cachePath)) {
274-
return null;
275-
}
276-
277-
$files = File::allFiles($cachePath);
278-
279-
foreach ($files as $file) {
280-
try {
281-
$content = File::get($file->getPathname());
282-
283-
// Laravel file cache format: expiration_time + serialized_value
284-
if (mb_strlen($content) < 10) {
285-
continue;
286-
}
287-
288-
$expiration = mb_substr($content, 0, 10);
289-
$serialized = mb_substr($content, 10);
290-
291-
// Check if expired
292-
if (time() > $expiration) {
293-
continue;
294-
}
295-
296-
// Try to unserialize to get the data
297-
$data = unserialize($serialized);
298-
if (is_array($data) && isset($data['key']) && $data['key'] === $key) {
299-
return $data['value'] ?? null;
300-
}
301-
} catch (Exception) {
302-
// If we can't read this file, skip it
303-
continue;
304-
}
305-
}
306-
307-
return null;
308-
} catch (Exception) {
309-
return null;
310-
}
311-
}
312-
313220
private function getFileKeyValue(string $filename): mixed
314221
{
315222
try {

tests/Unit/CacheUiLaravelCommandSimpleTest.php

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
declare(strict_types=1);
44

55
use Abr4xas\CacheUiLaravel\Commands\CacheUiLaravelCommand;
6-
use Illuminate\Support\Facades\Config;
76

87
describe('CacheUiLaravelCommand Basic Tests', function (): void {
98
it('has correct signature and description', function (): void {
@@ -27,88 +26,6 @@
2726
});
2827
});
2928

30-
describe('getValuePreview method', function (): void {
31-
it('handles null values', function (): void {
32-
$command = new CacheUiLaravelCommand();
33-
$reflection = new ReflectionClass($command);
34-
$method = $reflection->getMethod('getValuePreview');
35-
36-
$result = $method->invoke($command, null);
37-
expect($result)->toBe('<fg=gray>(null)</>');
38-
});
39-
40-
it('handles boolean values', function (): void {
41-
$command = new CacheUiLaravelCommand();
42-
$reflection = new ReflectionClass($command);
43-
$method = $reflection->getMethod('getValuePreview');
44-
45-
$trueResult = $method->invoke($command, true);
46-
$falseResult = $method->invoke($command, false);
47-
48-
expect($trueResult)->toBe('<fg=green>true</>');
49-
expect($falseResult)->toBe('<fg=red>false</>');
50-
});
51-
52-
it('handles array values', function (): void {
53-
$command = new CacheUiLaravelCommand();
54-
$reflection = new ReflectionClass($command);
55-
$method = $reflection->getMethod('getValuePreview');
56-
57-
$array = ['key' => 'value', 'number' => 123];
58-
$result = $method->invoke($command, $array);
59-
60-
expect($result)->toBe('{"key":"value","number":123}');
61-
});
62-
63-
it('handles object values', function (): void {
64-
$command = new CacheUiLaravelCommand();
65-
$reflection = new ReflectionClass($command);
66-
$method = $reflection->getMethod('getValuePreview');
67-
68-
$object = (object) ['key' => 'value'];
69-
$result = $method->invoke($command, $object);
70-
71-
expect($result)->toBe('{"key":"value"}');
72-
});
73-
74-
it('handles string values within limit', function (): void {
75-
$command = new CacheUiLaravelCommand();
76-
$reflection = new ReflectionClass($command);
77-
$method = $reflection->getMethod('getValuePreview');
78-
79-
$shortString = 'Hello World';
80-
$result = $method->invoke($command, $shortString);
81-
82-
expect($result)->toBe('Hello World');
83-
});
84-
85-
it('handles string values exceeding limit', function (): void {
86-
Config::set('cache-ui-laravel.preview_limit', 10);
87-
88-
$command = new CacheUiLaravelCommand();
89-
$reflection = new ReflectionClass($command);
90-
$method = $reflection->getMethod('getValuePreview');
91-
92-
$longString = 'This is a very long string that exceeds the limit';
93-
$result = $method->invoke($command, $longString);
94-
95-
expect($result)->toBe('This is a <fg=gray>...</>');
96-
});
97-
98-
it('handles array values exceeding limit', function (): void {
99-
Config::set('cache-ui-laravel.preview_limit', 20);
100-
101-
$command = new CacheUiLaravelCommand();
102-
$reflection = new ReflectionClass($command);
103-
$method = $reflection->getMethod('getValuePreview');
104-
105-
$largeArray = ['very' => 'long', 'array' => 'with', 'many' => 'keys', 'and' => 'values'];
106-
$result = $method->invoke($command, $largeArray);
107-
108-
expect($result)->toContain('<fg=gray>...</>');
109-
});
110-
});
111-
11229
describe('getCacheKeys method', function (): void {
11330
it('returns array for any driver type', function (): void {
11431
$command = new CacheUiLaravelCommand();
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Abr4xas\CacheUiLaravel\Commands\CacheUiLaravelCommand;
6+
7+
describe('CacheUiLaravelCommand Method Tests', function (): void {
8+
describe('getArrayKeys method', function (): void {
9+
it('returns empty array and shows warning', function (): void {
10+
$command = new CacheUiLaravelCommand();
11+
$reflection = new ReflectionClass($command);
12+
$method = $reflection->getMethod('getArrayKeys');
13+
14+
$result = $method->invoke($command);
15+
expect($result)->toBeArray();
16+
expect($result)->toBeEmpty();
17+
});
18+
});
19+
20+
describe('handleUnsupportedDriver method', function (): void {
21+
it('exists and is private', function (): void {
22+
$command = new CacheUiLaravelCommand();
23+
$reflection = new ReflectionClass($command);
24+
$method = $reflection->getMethod('handleUnsupportedDriver');
25+
26+
expect($method->isPrivate())->toBeTrue();
27+
expect($method->getReturnType()->getName())->toBe('array');
28+
});
29+
});
30+
31+
describe('getFileKeyValue method', function (): void {
32+
it('returns null when file does not exist', function (): void {
33+
$command = new CacheUiLaravelCommand();
34+
$reflection = new ReflectionClass($command);
35+
$method = $reflection->getMethod('getFileKeyValue');
36+
37+
$result = $method->invoke($command, 'nonexistent_file');
38+
expect($result)->toBeNull();
39+
});
40+
});
41+
42+
describe('deleteFileKey method', function (): void {
43+
it('returns false when file does not exist', function (): void {
44+
$command = new CacheUiLaravelCommand();
45+
$reflection = new ReflectionClass($command);
46+
$method = $reflection->getMethod('deleteFileKey');
47+
48+
$result = $method->invoke($command, 'nonexistent_file');
49+
expect($result)->toBeFalse();
50+
});
51+
});
52+
53+
describe('method existence', function (): void {
54+
it('has all required private methods', function (): void {
55+
$command = new CacheUiLaravelCommand();
56+
$reflection = new ReflectionClass($command);
57+
58+
$expectedMethods = [
59+
'getCacheKeys',
60+
'getRedisKeys',
61+
'getFileKeys',
62+
'getDatabaseKeys',
63+
'getArrayKeys',
64+
'handleUnsupportedDriver',
65+
'getFileKeyValue',
66+
'deleteFileKeyByKey',
67+
'deleteFileKey',
68+
];
69+
70+
foreach ($expectedMethods as $methodName) {
71+
expect($reflection->hasMethod($methodName))->toBeTrue();
72+
73+
$method = $reflection->getMethod($methodName);
74+
expect($method->isPrivate())->toBeTrue();
75+
}
76+
});
77+
});
78+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Abr4xas\CacheUiLaravel\Facades\CacheUiLaravel;
6+
use Illuminate\Support\Facades\Facade;
7+
8+
describe('CacheUiLaravel Facade Tests', function (): void {
9+
describe('Facade structure', function (): void {
10+
it('extends Laravel Facade', function (): void {
11+
$reflection = new ReflectionClass(CacheUiLaravel::class);
12+
expect($reflection->getParentClass()->getName())->toBe(Facade::class);
13+
});
14+
15+
it('is final class', function (): void {
16+
$reflection = new ReflectionClass(CacheUiLaravel::class);
17+
expect($reflection->isFinal())->toBeTrue();
18+
});
19+
20+
it('has correct facade accessor', function (): void {
21+
$reflection = new ReflectionClass(CacheUiLaravel::class);
22+
$method = $reflection->getMethod('getFacadeAccessor');
23+
24+
$accessor = $method->invoke(null);
25+
expect($accessor)->toBe(Abr4xas\CacheUiLaravel\CacheUiLaravel::class);
26+
});
27+
});
28+
29+
describe('Facade functionality', function (): void {
30+
it('has correct facade accessor method', function (): void {
31+
$reflection = new ReflectionClass(CacheUiLaravel::class);
32+
$method = $reflection->getMethod('getFacadeAccessor');
33+
34+
expect($method->isProtected())->toBeTrue();
35+
expect($method->isStatic())->toBeTrue();
36+
});
37+
});
38+
39+
describe('Documentation', function (): void {
40+
it('has proper docblock', function (): void {
41+
$reflection = new ReflectionClass(CacheUiLaravel::class);
42+
$docComment = $reflection->getDocComment();
43+
44+
expect($docComment)->toContain('@see');
45+
expect($docComment)->toContain('CacheUiLaravel');
46+
});
47+
});
48+
});

0 commit comments

Comments
 (0)