|
| 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 Deletion', function (): void { |
| 11 | + beforeEach(function (): void { |
| 12 | + $this->cacheUiLaravel = new CacheUiLaravel(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('deletes hashed file when standard forget fails for file driver', function (): void { |
| 16 | + // Mock Cache::forget to return false, simulating that it couldn't find the key by name |
| 17 | + Cache::shouldReceive('store')->withNoArgs()->andReturnSelf(); |
| 18 | + Cache::shouldReceive('forget')->with('008cb7ea48f292dd8b03d361a4c9f66085f77090')->andReturn(false); |
| 19 | + |
| 20 | + // Configure file driver |
| 21 | + Config::set('cache.default', 'file'); |
| 22 | + Config::set('cache.stores.file.driver', 'file'); |
| 23 | + $cachePath = storage_path('framework/cache/data'); |
| 24 | + Config::set('cache.stores.file.path', $cachePath); |
| 25 | + |
| 26 | + // The key is a SHA1 hash |
| 27 | + $key = '008cb7ea48f292dd8b03d361a4c9f66085f77090'; |
| 28 | + |
| 29 | + // Expected file path reconstruction |
| 30 | + // 00/8c/008cb7ea48f292dd8b03d361a4c9f66085f77090 |
| 31 | + $expectedPath = $cachePath.'/00/8c/'.$key; |
| 32 | + |
| 33 | + // Mock File existence and deletion |
| 34 | + File::shouldReceive('exists')->with($expectedPath)->andReturn(true); |
| 35 | + File::shouldReceive('delete')->with($expectedPath)->andReturn(true); |
| 36 | + |
| 37 | + $result = $this->cacheUiLaravel->forgetKey($key); |
| 38 | + |
| 39 | + expect($result)->toBeTrue(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('deletes hashed file when standard forget fails for key-aware-file driver', function (): void { |
| 43 | + // Mock Cache::forget to return false |
| 44 | + Cache::shouldReceive('store')->withNoArgs()->andReturnSelf(); |
| 45 | + Cache::shouldReceive('forget')->with('008cb7ea48f292dd8b03d361a4c9f66085f77090')->andReturn(false); |
| 46 | + |
| 47 | + // Configure key-aware-file driver |
| 48 | + Config::set('cache.default', 'file'); |
| 49 | + Config::set('cache.stores.file.driver', 'key-aware-file'); |
| 50 | + $cachePath = storage_path('framework/cache/data'); |
| 51 | + Config::set('cache.stores.file.path', $cachePath); |
| 52 | + |
| 53 | + $key = '008cb7ea48f292dd8b03d361a4c9f66085f77090'; |
| 54 | + $expectedPath = $cachePath.'/00/8c/'.$key; |
| 55 | + |
| 56 | + File::shouldReceive('exists')->with($expectedPath)->andReturn(true); |
| 57 | + File::shouldReceive('delete')->with($expectedPath)->andReturn(true); |
| 58 | + |
| 59 | + $result = $this->cacheUiLaravel->forgetKey($key); |
| 60 | + |
| 61 | + expect($result)->toBeTrue(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('does not attempt file deletion for non-hashed keys', function (): void { |
| 65 | + Cache::shouldReceive('store')->withNoArgs()->andReturnSelf(); |
| 66 | + Cache::shouldReceive('forget')->with('not-a-hash')->andReturn(false); |
| 67 | + |
| 68 | + Config::set('cache.default', 'file'); |
| 69 | + Config::set('cache.stores.file.driver', 'file'); |
| 70 | + |
| 71 | + // File::exists/delete should NOT be called |
| 72 | + File::shouldReceive('exists')->never(); |
| 73 | + File::shouldReceive('delete')->never(); |
| 74 | + |
| 75 | + $result = $this->cacheUiLaravel->forgetKey('not-a-hash'); |
| 76 | + |
| 77 | + expect($result)->toBeFalse(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('does not attempt file deletion for non-file drivers', function (): void { |
| 81 | + Cache::shouldReceive('store')->withNoArgs()->andReturnSelf(); |
| 82 | + Cache::shouldReceive('forget')->with('008cb7ea48f292dd8b03d361a4c9f66085f77090')->andReturn(false); |
| 83 | + |
| 84 | + Config::set('cache.default', 'redis'); |
| 85 | + Config::set('cache.stores.redis.driver', 'redis'); |
| 86 | + |
| 87 | + // File::exists/delete should NOT be called |
| 88 | + File::shouldReceive('exists')->never(); |
| 89 | + File::shouldReceive('delete')->never(); |
| 90 | + |
| 91 | + $result = $this->cacheUiLaravel->forgetKey('008cb7ea48f292dd8b03d361a4c9f66085f77090'); |
| 92 | + |
| 93 | + expect($result)->toBeFalse(); |
| 94 | + }); |
| 95 | +}); |
0 commit comments