16
16
use Symfony \Component \Cache \Adapter \ArrayAdapter ;
17
17
use Symfony \Component \Cache \Adapter \ChainAdapter ;
18
18
use Symfony \Component \Cache \Adapter \FilesystemAdapter ;
19
+ use Symfony \Component \Cache \CacheItem ;
19
20
use Symfony \Component \Cache \Tests \Fixtures \ExternalAdapter ;
20
21
use Symfony \Component \Cache \Tests \Fixtures \PrunableAdapter ;
22
+ use Symfony \Contracts \Cache \ItemInterface ;
21
23
22
24
/**
23
25
* @author Kévin Dunglas <[email protected] >
@@ -34,6 +36,11 @@ public function createCachePool(int $defaultLifetime = 0, string $testMethod = n
34
36
return new ChainAdapter ([new ArrayAdapter ($ defaultLifetime ), new ExternalAdapter ($ defaultLifetime ), new FilesystemAdapter ('' , $ defaultLifetime )], $ defaultLifetime );
35
37
}
36
38
39
+ public static function tearDownAfterClass (): void
40
+ {
41
+ FilesystemAdapterTest::rmdir (sys_get_temp_dir ().'/symfony-cache ' );
42
+ }
43
+
37
44
public function testEmptyAdaptersException ()
38
45
{
39
46
$ this ->expectException ('Symfony\Component\Cache\Exception\InvalidArgumentException ' );
@@ -187,6 +194,48 @@ public function testMultipleCachesExpirationWhenCommonTtlIsSet()
187
194
$ this ->assertFalse ($ item ->isHit ());
188
195
}
189
196
197
+ public function testExpirationOnAllAdapters ()
198
+ {
199
+ if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
200
+ $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
201
+ }
202
+
203
+ $ itemValidator = function (CacheItem $ item ) {
204
+ $ refl = new \ReflectionObject ($ item );
205
+ $ propExpiry = $ refl ->getProperty ('expiry ' );
206
+ $ propExpiry ->setAccessible (true );
207
+ $ expiry = $ propExpiry ->getValue ($ item );
208
+ $ this ->assertGreaterThan (10 , $ expiry - time (), 'Item should be saved with the given ttl, not the default for the adapter. ' );
209
+
210
+ return true ;
211
+ };
212
+
213
+ $ adapter1 = $ this ->getMockBuilder (FilesystemAdapter::class)
214
+ ->setConstructorArgs (['' , 2 ])
215
+ ->setMethods (['save ' ])
216
+ ->getMock ();
217
+ $ adapter1 ->expects ($ this ->once ())
218
+ ->method ('save ' )
219
+ ->with ($ this ->callback ($ itemValidator ))
220
+ ->willReturn (true );
221
+
222
+ $ adapter2 = $ this ->getMockBuilder (FilesystemAdapter::class)
223
+ ->setConstructorArgs (['' , 4 ])
224
+ ->setMethods (['save ' ])
225
+ ->getMock ();
226
+ $ adapter2 ->expects ($ this ->once ())
227
+ ->method ('save ' )
228
+ ->with ($ this ->callback ($ itemValidator ))
229
+ ->willReturn (true );
230
+
231
+ $ cache = new ChainAdapter ([$ adapter1 , $ adapter2 ], 6 );
232
+ $ cache ->get ('test_key ' , function (ItemInterface $ item ) {
233
+ $ item ->expiresAfter (15 );
234
+
235
+ return 'chain ' ;
236
+ });
237
+ }
238
+
190
239
private function getPruneableMock (): AdapterInterface
191
240
{
192
241
$ pruneable = $ this ->createMock (PrunableAdapter::class);
0 commit comments