Skip to content

Commit 52abdb7

Browse files
committed
test: add file driver tests for forgetKey with correct mock expectations
1 parent 70cc7c0 commit 52abdb7

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Abr4xas\CacheUiLaravel\CacheUiLaravel;
6+
use Illuminate\Support\Facades\Cache;
7+
use Illuminate\Support\Facades\Config;
8+
use Illuminate\Support\Facades\File;
9+
10+
describe('CacheUiLaravel File Driver Tests', function (): void {
11+
beforeEach(function (): void {
12+
$this->cacheUiLaravel = new CacheUiLaravel();
13+
});
14+
15+
describe('forgetKey with file driver', function (): void {
16+
it('successfully deletes a key from file cache by key content', function (): void {
17+
Config::set('cache.default', 'file');
18+
Config::set('cache.stores.file.driver', 'key-aware-file');
19+
Config::set('cache.stores.file.path', storage_path('framework/cache/data'));
20+
21+
$cachePath = storage_path('framework/cache/data');
22+
$testKey = 'test-cache-key';
23+
$mockFile = Mockery::mock();
24+
$mockFile->shouldReceive('getPathname')->andReturn($cachePath.'/test-file');
25+
26+
// Mock File::exists for deleteFileKeyByKey (checking cache directory)
27+
File::shouldReceive('exists')->with($cachePath)->once()->andReturn(true);
28+
// Mock File::allFiles to return a file
29+
File::shouldReceive('allFiles')->with($cachePath)->once()->andReturn([$mockFile]);
30+
// Mock File::get to return wrapped data with the key
31+
$wrappedData = serialize(['key' => $testKey, 'value' => 'test-value']);
32+
$expiration = time() + 3600;
33+
File::shouldReceive('get')->with($cachePath.'/test-file')->once()->andReturn($expiration.$wrappedData);
34+
// Mock File::delete to return true
35+
File::shouldReceive('delete')->with($cachePath.'/test-file')->once()->andReturn(true);
36+
37+
// Mock Cache facade for store validation
38+
$mockStore = Mockery::mock();
39+
$mockStore->shouldReceive('forget')->with($testKey)->andReturn(false);
40+
Cache::shouldReceive('store')->with('file')->andReturn($mockStore);
41+
42+
$result = $this->cacheUiLaravel->forgetKey($testKey, 'file');
43+
expect($result)->toBeTrue();
44+
});
45+
46+
it('successfully deletes a key from file cache by filename when key search fails', function (): void {
47+
Config::set('cache.default', 'file');
48+
Config::set('cache.stores.file.driver', 'key-aware-file');
49+
Config::set('cache.stores.file.path', storage_path('framework/cache/data'));
50+
51+
$cachePath = storage_path('framework/cache/data');
52+
$testKey = 'legacy-filename-key';
53+
$mockFile = Mockery::mock();
54+
$mockFile->shouldReceive('getPathname')->andReturn($cachePath.'/test-file');
55+
56+
// First call: deleteFileKeyByKey - check directory exists
57+
File::shouldReceive('exists')->with($cachePath)->once()->andReturn(true);
58+
// Mock File::allFiles to return a file
59+
File::shouldReceive('allFiles')->with($cachePath)->once()->andReturn([$mockFile]);
60+
// Mock File::get to return data without matching key
61+
$wrappedData = serialize(['key' => 'other-key', 'value' => 'test-value']);
62+
$expiration = time() + 3600;
63+
File::shouldReceive('get')->with($cachePath.'/test-file')->once()->andReturn($expiration.$wrappedData);
64+
65+
// Second call: deleteFileKeyByFilename - check file exists
66+
$filePath = $cachePath.'/'.$testKey;
67+
File::shouldReceive('exists')->with($filePath)->once()->andReturn(true);
68+
// Mock File::delete to return true
69+
File::shouldReceive('delete')->with($filePath)->once()->andReturn(true);
70+
71+
// Mock Cache facade for store validation
72+
$mockStore = Mockery::mock();
73+
$mockStore->shouldReceive('forget')->with($testKey)->andReturn(false);
74+
Cache::shouldReceive('store')->with('file')->andReturn($mockStore);
75+
76+
$result = $this->cacheUiLaravel->forgetKey($testKey, 'file');
77+
expect($result)->toBeTrue();
78+
});
79+
80+
it('returns false when file cache directory does not exist', function (): void {
81+
Config::set('cache.default', 'file');
82+
Config::set('cache.stores.file.driver', 'key-aware-file');
83+
Config::set('cache.stores.file.path', storage_path('framework/cache/data'));
84+
85+
$cachePath = storage_path('framework/cache/data');
86+
$testKey = 'test-key';
87+
88+
// Mock File::exists to return false (directory doesn't exist)
89+
File::shouldReceive('exists')->with($cachePath)->once()->andReturn(false);
90+
91+
// Mock Cache facade for store validation
92+
$mockStore = Mockery::mock();
93+
$mockStore->shouldReceive('forget')->with($testKey)->andReturn(false);
94+
Cache::shouldReceive('store')->with('file')->andReturn($mockStore);
95+
96+
$result = $this->cacheUiLaravel->forgetKey($testKey, 'file');
97+
expect($result)->toBeFalse();
98+
});
99+
100+
it('returns false when key is not found in file cache', function (): void {
101+
Config::set('cache.default', 'file');
102+
Config::set('cache.stores.file.driver', 'key-aware-file');
103+
Config::set('cache.stores.file.path', storage_path('framework/cache/data'));
104+
105+
$cachePath = storage_path('framework/cache/data');
106+
$testKey = 'non-existent-key';
107+
$mockFile = Mockery::mock();
108+
$mockFile->shouldReceive('getPathname')->andReturn($cachePath.'/test-file');
109+
110+
// First call: deleteFileKeyByKey - check directory exists
111+
File::shouldReceive('exists')->with($cachePath)->once()->andReturn(true);
112+
// Mock File::allFiles to return a file
113+
File::shouldReceive('allFiles')->with($cachePath)->once()->andReturn([$mockFile]);
114+
// Mock File::get to return data without matching key
115+
$wrappedData = serialize(['key' => 'other-key', 'value' => 'test-value']);
116+
$expiration = time() + 3600;
117+
File::shouldReceive('get')->with($cachePath.'/test-file')->once()->andReturn($expiration.$wrappedData);
118+
119+
// Second call: deleteFileKeyByFilename - check file exists (should return false)
120+
$filePath = $cachePath.'/'.$testKey;
121+
File::shouldReceive('exists')->with($filePath)->once()->andReturn(false);
122+
123+
// Mock Cache facade for store validation
124+
$mockStore = Mockery::mock();
125+
$mockStore->shouldReceive('forget')->with($testKey)->andReturn(false);
126+
Cache::shouldReceive('store')->with('file')->andReturn($mockStore);
127+
128+
$result = $this->cacheUiLaravel->forgetKey($testKey, 'file');
129+
expect($result)->toBeFalse();
130+
});
131+
});
132+
});
133+
134+
afterEach(function (): void {
135+
Mockery::close();
136+
});

0 commit comments

Comments
 (0)