@@ -134,4 +134,113 @@ public function testInvalidCacheDirectory()
134134
135135 new FileCacheStorage ( $ InvalidPath );
136136 }
137+
138+ public function testGarbageCollection ()
139+ {
140+ // Create some cache entries with different TTLs
141+ $ this ->Storage ->write ( 'keep1 ' , 'content1 ' , 3600 ); // Keep for 1 hour
142+ $ this ->Storage ->write ( 'keep2 ' , 'content2 ' , 3600 ); // Keep for 1 hour
143+ $ this ->Storage ->write ( 'expire1 ' , 'content3 ' , 1 ); // Expire in 1 second
144+ $ this ->Storage ->write ( 'expire2 ' , 'content4 ' , 1 ); // Expire in 1 second
145+
146+ // All should exist initially
147+ $ this ->assertTrue ( $ this ->Storage ->exists ( 'keep1 ' ) );
148+ $ this ->assertTrue ( $ this ->Storage ->exists ( 'keep2 ' ) );
149+ $ this ->assertTrue ( $ this ->Storage ->exists ( 'expire1 ' ) );
150+ $ this ->assertTrue ( $ this ->Storage ->exists ( 'expire2 ' ) );
151+
152+ // Wait for some entries to expire
153+ sleep ( 2 );
154+
155+ // Run garbage collection
156+ $ Removed = $ this ->Storage ->gc ();
157+ $ this ->assertEquals ( 2 , $ Removed );
158+
159+ // Check that only non-expired entries remain
160+ $ this ->assertTrue ( $ this ->Storage ->exists ( 'keep1 ' ) );
161+ $ this ->assertTrue ( $ this ->Storage ->exists ( 'keep2 ' ) );
162+ $ this ->assertFalse ( $ this ->Storage ->exists ( 'expire1 ' ) );
163+ $ this ->assertFalse ( $ this ->Storage ->exists ( 'expire2 ' ) );
164+ }
165+
166+ public function testGarbageCollectionOnEmptyCache ()
167+ {
168+ $ Removed = $ this ->Storage ->gc ();
169+ $ this ->assertEquals ( 0 , $ Removed );
170+ }
171+
172+ public function testReadExpiredEntry ()
173+ {
174+ $ Key = 'test_read_expired ' ;
175+ $ this ->Storage ->write ( $ Key , 'content ' , 1 );
176+
177+ sleep ( 2 );
178+
179+ // Reading expired entry should return null and delete it
180+ $ this ->assertNull ( $ this ->Storage ->read ( $ Key ) );
181+ $ this ->assertFalse ( $ this ->Storage ->exists ( $ Key ) );
182+ }
183+
184+ public function testClearWithNonExistentDirectory ()
185+ {
186+ // Create storage with non-existent base path
187+ $ Storage = new FileCacheStorage ( vfsStream::url ( 'cache/newdir ' ) );
188+
189+ // Clear should still return true even if directory doesn't fully exist
190+ $ this ->assertTrue ( $ Storage ->clear () );
191+ }
192+
193+ public function testIsExpiredWithMissingMetaFile ()
194+ {
195+ $ Key = 'test_missing_meta ' ;
196+
197+ // Manually create cache file without meta file
198+ $ Hash = md5 ( $ Key );
199+ $ SubDir = substr ( $ Hash , 0 , 2 );
200+ $ Dir = vfsStream::newDirectory ( $ SubDir )->at ( $ this ->Root );
201+ vfsStream::newFile ( $ Hash . '.cache ' )
202+ ->at ( $ Dir )
203+ ->withContent ( 'content ' );
204+
205+ // Should be considered expired if meta file is missing
206+ $ this ->assertTrue ( $ this ->Storage ->isExpired ( $ Key ) );
207+ }
208+
209+ public function testIsExpiredWithCorruptedMetaFile ()
210+ {
211+ $ Key = 'test_corrupted_meta ' ;
212+
213+ // Manually create cache and corrupted meta file
214+ $ Hash = md5 ( $ Key );
215+ $ SubDir = substr ( $ Hash , 0 , 2 );
216+ $ Dir = vfsStream::newDirectory ( $ SubDir )->at ( $ this ->Root );
217+ vfsStream::newFile ( $ Hash . '.cache ' )
218+ ->at ( $ Dir )
219+ ->withContent ( 'content ' );
220+ vfsStream::newFile ( $ Hash . '.meta ' )
221+ ->at ( $ Dir )
222+ ->withContent ( 'not valid json ' );
223+
224+ // Should be considered expired if meta file is corrupted
225+ $ this ->assertTrue ( $ this ->Storage ->isExpired ( $ Key ) );
226+ }
227+
228+ public function testIsExpiredWithIncompleteMetaData ()
229+ {
230+ $ Key = 'test_incomplete_meta ' ;
231+
232+ // Manually create cache and meta file without expires field
233+ $ Hash = md5 ( $ Key );
234+ $ SubDir = substr ( $ Hash , 0 , 2 );
235+ $ Dir = vfsStream::newDirectory ( $ SubDir )->at ( $ this ->Root );
236+ vfsStream::newFile ( $ Hash . '.cache ' )
237+ ->at ( $ Dir )
238+ ->withContent ( 'content ' );
239+ vfsStream::newFile ( $ Hash . '.meta ' )
240+ ->at ( $ Dir )
241+ ->withContent ( json_encode ( [ 'created ' => time () ] ) );
242+
243+ // Should be considered expired if meta data is incomplete
244+ $ this ->assertTrue ( $ this ->Storage ->isExpired ( $ Key ) );
245+ }
137246}
0 commit comments