@@ -190,6 +190,18 @@ public function test_writeToCache_GetCached()
190190 );
191191 }
192192
193+ public function testGetCached_refreshesAccessTime ()
194+ {
195+ $ filePath = CachedEntry::writeToCache ('foo ' , []);
196+ $ accessTimeBefore = fileatime ($ filePath );
197+
198+ sleep (1 );
199+ CachedEntry::getCached ('foo ' , []);
200+ clearstatcache (true , $ filePath );
201+
202+ $ this ->assertGreaterThan ($ accessTimeBefore , fileatime ($ filePath ));
203+ }
204+
193205 public function test_getCachePath ()
194206 {
195207 $ path1 = CachedEntry::getCachePath ('foo ' );
@@ -224,42 +236,58 @@ public function test_deleteLeastAccessedFiles_nothingToDelete()
224236 public function test_deleteLeastAccessedFiles_deletesOnlyOldest ()
225237 {
226238 $ filePath1 = CachedEntry::writeToCache ('file ' , []);
227- sleep (1 ); // otherwise without sleep the sorting won't work properly
228239 $ filePath2 = CachedEntry::writeToCache ('bar ' , []);
229- sleep (1 );
230240 $ filePath3 = CachedEntry::writeToCache ('baz ' , []);
231- sleep (1 );
232241 $ filePath4 = CachedEntry::writeToCache ('foo ' , []);
233- sleep (1 );
242+
243+ // Set explicit timestamps so the eviction order does not depend on filesystem precision.
244+ $ this ->setFileTimestamp ($ filePath1 , 100 );
245+ $ this ->setFileTimestamp ($ filePath2 , 200 );
246+ $ this ->setFileTimestamp ($ filePath3 , 300 );
247+ $ this ->setFileTimestamp ($ filePath4 , 400 );
234248
235249 CachedEntry::deleteLeastAccessedFiles (2 );
236250
237- $ this ->assertFileNotExists ($ filePath1 );
238- $ this ->assertFileNotExists ($ filePath2 );
251+ $ this ->assertFileMissing ($ filePath1 );
252+ $ this ->assertFileMissing ($ filePath2 );
239253 $ this ->assertFileExists ($ filePath3 );
240254 $ this ->assertFileExists ($ filePath4 );
241255 }
242256
243257 public function test_deleteLeastAccessedFiles_deletesOnlyOldest2 ()
244258 {
245259 $ filePath1 = CachedEntry::writeToCache ('file ' , []);
246- sleep (1 );
247260 $ filePath2 = CachedEntry::writeToCache ('bar ' , []);
248- sleep (1 );
249261 $ filePath3 = CachedEntry::writeToCache ('baz ' , []);
250- sleep (1 );
251262 $ filePath4 = CachedEntry::writeToCache ('foo ' , []);
252- sleep (1 );
253263
254- touch ($ filePath1 );
255- sleep (1 );
256- touch ($ filePath3 );
264+ // Simulate cache hits by moving selected files to the newest timestamps explicitly.
265+ $ this ->setFileTimestamp ($ filePath1 , 300 );
266+ $ this ->setFileTimestamp ($ filePath2 , 100 );
267+ $ this ->setFileTimestamp ($ filePath3 , 400 );
268+ $ this ->setFileTimestamp ($ filePath4 , 200 );
257269
258270 CachedEntry::deleteLeastAccessedFiles (2 );
259271
260- $ this ->assertFileNotExists ($ filePath2 );
261- $ this ->assertFileNotExists ($ filePath4 );
272+ $ this ->assertFileMissing ($ filePath2 );
273+ $ this ->assertFileMissing ($ filePath4 );
262274 $ this ->assertFileExists ($ filePath1 );
263275 $ this ->assertFileExists ($ filePath3 );
264276 }
277+
278+ private function assertFileMissing (string $ path ): void
279+ {
280+ if (method_exists ($ this , 'assertFileDoesNotExist ' )) {
281+ $ this ->assertFileDoesNotExist ($ path );
282+ return ;
283+ }
284+
285+ $ this ->assertFileNotExists ($ path );
286+ }
287+
288+ private function setFileTimestamp (string $ path , int $ timestamp ): void
289+ {
290+ touch ($ path , $ timestamp , $ timestamp );
291+ clearstatcache (true , $ path );
292+ }
265293}
0 commit comments