|
56 | 56 | $differentVersionResponse->assertOk(); |
57 | 57 | $differentVersionResponse->assertHeader('X-Cache', 'MISS'); |
58 | 58 | }); |
| 59 | + |
| 60 | +it('can disable the cache', function () { |
| 61 | + /** @var TestCase $this */ |
| 62 | + config()->set('image-transform-url.cache.enabled', false); |
| 63 | + |
| 64 | + for($i = 0; $i < 2; $i++) { |
| 65 | + $response = $this->get(route('image.transform.default', [ |
| 66 | + 'options' => 'width=500', |
| 67 | + 'path' => 'cat.jpg', |
| 68 | + ])); |
| 69 | + |
| 70 | + $response->assertOk(); |
| 71 | + $response->assertHeaderMissing('X-Cache'); |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +it('can manage cache size limit by cleaning up old files', function () { |
| 76 | + /** @var TestCase $this */ |
| 77 | + // Set a high cache size limit first |
| 78 | + config()->set('image-transform-url.cache.max_size_mb', 100); |
| 79 | + |
| 80 | + $responses = []; |
| 81 | + $totalSizeMB = 0; |
| 82 | + |
| 83 | + // Fill up the cache with some images |
| 84 | + for ($i = 0; $i < 10; $i++) { |
| 85 | + $response = $this->get(route('image.transform.default', [ |
| 86 | + 'options' => "width=1200,version={$i}", |
| 87 | + 'path' => 'cat.jpg', |
| 88 | + ])); |
| 89 | + |
| 90 | + $disk = Storage::disk(config()->string('image-transform-url.cache.disk')); |
| 91 | + $size = $disk->size($this->getCacheEndPath('test-data', 'cat.jpg', ['width' => 1200, 'version' => $i])); |
| 92 | + |
| 93 | + $totalSizeMB += $size / (1024 * 1024); |
| 94 | + |
| 95 | + $response->assertOk(); |
| 96 | + $response->assertHeader('X-Cache', 'MISS'); |
| 97 | + |
| 98 | + $responses[] = $response; |
| 99 | + } |
| 100 | + |
| 101 | + // Set the used cache size as the upper limit, so we can test the cleanup |
| 102 | + config()->set('image-transform-url.cache.max_size_mb', (int) ceil($totalSizeMB)); |
| 103 | + |
| 104 | + // Create one more image that should trigger cache cleanup |
| 105 | + $finalResponse = $this->get(route('image.transform.default', [ |
| 106 | + 'options' => 'width=2400,version=99', |
| 107 | + 'path' => 'cat.jpg', |
| 108 | + ])); |
| 109 | + |
| 110 | + $finalResponse->assertOk(); |
| 111 | + $finalResponse->assertHeader('X-Cache', 'MISS'); |
| 112 | + |
| 113 | + $disk = Storage::disk(config()->string('image-transform-url.cache.disk')); |
| 114 | + |
| 115 | + expect($disk->exists('_cache/image-transform-url'))->toBeTrue(); |
| 116 | + |
| 117 | + $allFiles = $disk->allFiles('_cache/image-transform-url'); |
| 118 | + |
| 119 | + $totalSizeAfterClearMB = 0; |
| 120 | + |
| 121 | + foreach ($allFiles as $filePath) { |
| 122 | + $size = $disk->size($filePath); |
| 123 | + $totalSizeAfterClearMB += $size / (1024 * 1024); |
| 124 | + } |
| 125 | + |
| 126 | + $maxAfterClearSizeMB = config()->integer('image-transform-url.cache.max_size_mb') * (config()->integer('image-transform-url.cache.clear_to_percent') / 100); |
| 127 | + |
| 128 | + expect($totalSizeAfterClearMB)->toBeLessThanOrEqual($maxAfterClearSizeMB); |
| 129 | +}); |
| 130 | + |
| 131 | +it('deletes files in the right order when cleaning up the cache', function () { |
| 132 | + /** @var TestCase $this */ |
| 133 | + config()->set('image-transform-url.cache.max_size_mb', 100); |
| 134 | + |
| 135 | + $responses = []; |
| 136 | + $cacheFilePaths = []; |
| 137 | + $totalSizeMB = 0; |
| 138 | + |
| 139 | + // Fill up the cache with some images |
| 140 | + for ($i = 0; $i < 10; $i++) { |
| 141 | + $response = $this->get(route('image.transform.default', [ |
| 142 | + 'options' => "width=1200,version={$i}", |
| 143 | + 'path' => 'cat.jpg', |
| 144 | + ])); |
| 145 | + |
| 146 | + $disk = Storage::disk(config()->string('image-transform-url.cache.disk')); |
| 147 | + $endPath = $this->getCacheEndPath('test-data', 'cat.jpg', ['width' => 1200, 'version' => $i]); |
| 148 | + |
| 149 | + $cacheFilePaths[] = $endPath; |
| 150 | + |
| 151 | + $size = $disk->size($endPath); |
| 152 | + |
| 153 | + $totalSizeMB += $size / (1024 * 1024); |
| 154 | + |
| 155 | + $response->assertOk(); |
| 156 | + $response->assertHeader('X-Cache', 'MISS'); |
| 157 | + |
| 158 | + $responses[] = $response; |
| 159 | + |
| 160 | + usleep(50_000); |
| 161 | + } |
| 162 | + |
| 163 | + $disk = Storage::disk(config()->string('image-transform-url.cache.disk')); |
| 164 | + |
| 165 | + expect($disk->exists('_cache/image-transform-url'))->toBeTrue(); |
| 166 | + |
| 167 | + // Set the used cache size as the upper limit, so we can test the cleanup |
| 168 | + config()->set('image-transform-url.cache.max_size_mb', (int) $totalSizeMB); |
| 169 | + |
| 170 | + // Create one more image that should trigger cache cleanup |
| 171 | + $finalResponse = $this->get(route('image.transform.default', [ |
| 172 | + 'options' => 'width=2400,version=99', |
| 173 | + 'path' => 'cat.jpg', |
| 174 | + ])); |
| 175 | + |
| 176 | + $lastCacheFilePath = $this->getCacheEndPath('test-data', 'cat.jpg', ['width' => 2400, 'version' => 99]); |
| 177 | + |
| 178 | + $finalResponse->assertOk(); |
| 179 | + $finalResponse->assertHeader('X-Cache', 'MISS'); |
| 180 | + |
| 181 | + expect($disk->exists('_cache/image-transform-url'))->toBeTrue(); |
| 182 | + |
| 183 | + expect($disk->exists($lastCacheFilePath))->toBeTrue(); // The last file should still exist |
| 184 | + |
| 185 | + expect($disk->exists($cacheFilePaths[0]))->toBeFalse(); // The oldest file should at be deleted |
| 186 | +}); |
0 commit comments