|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Symfony\Component\Cache\Tests\Adapter; |
| 5 | + |
| 6 | +use Psr\Cache\CacheItemPoolInterface; |
| 7 | +use Symfony\Component\Cache\Adapter\AbstractAdapter; |
| 8 | +use Symfony\Component\Cache\Adapter\ProxyAdapter; |
| 9 | +use Symfony\Component\Cache\Adapter\RedisAdapter; |
| 10 | +use Symfony\Component\Cache\CacheItem; |
| 11 | + |
| 12 | +/** |
| 13 | + * @group integration |
| 14 | + */ |
| 15 | +class ProxyAdapterAndRedisAdapterTest extends AbstractRedisAdapterTest |
| 16 | +{ |
| 17 | + protected $skippedTests = [ |
| 18 | + 'testPrune' => 'RedisAdapter does not implement PruneableInterface.', |
| 19 | + ]; |
| 20 | + |
| 21 | + public static function setUpBeforeClass(): void |
| 22 | + { |
| 23 | + parent::setUpBeforeClass(); |
| 24 | + self::$redis = AbstractAdapter::createConnection('redis://'.getenv('REDIS_HOST'));; |
| 25 | + } |
| 26 | + |
| 27 | + public function createCachePool($defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface |
| 28 | + { |
| 29 | + return new ProxyAdapter(new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), 100), 'ProxyNS', $defaultLifetime); |
| 30 | + } |
| 31 | + |
| 32 | + public function testSaveItemPermanently() |
| 33 | + { |
| 34 | + $setCacheItemExpiry = \Closure::bind( |
| 35 | + static function (CacheItem $item, $expiry) { |
| 36 | + $item->expiry = $expiry; |
| 37 | + |
| 38 | + return $item; |
| 39 | + }, |
| 40 | + null, |
| 41 | + CacheItem::class |
| 42 | + ); |
| 43 | + |
| 44 | + $cache = $this->createCachePool(1); |
| 45 | + $value = rand(); |
| 46 | + $item = $cache->getItem('foo'); |
| 47 | + $setCacheItemExpiry($item, 0); |
| 48 | + $cache->save($item->set($value)); |
| 49 | + $item = $cache->getItem('bar'); |
| 50 | + $setCacheItemExpiry($item, 0.0); |
| 51 | + $cache->save($item->set($value)); |
| 52 | + $item = $cache->getItem('baz'); |
| 53 | + $cache->save($item->set($value)); |
| 54 | + |
| 55 | + $this->assertSame($value, $this->cache->getItem('foo')->get()); |
| 56 | + $this->assertSame($value, $this->cache->getItem('bar')->get()); |
| 57 | + $this->assertSame($value, $this->cache->getItem('baz')->get()); |
| 58 | + |
| 59 | + sleep(1); |
| 60 | + $this->assertSame($value, $this->cache->getItem('foo')->get()); |
| 61 | + $this->assertSame($value, $this->cache->getItem('bar')->get()); |
| 62 | + $this->assertFalse($this->cache->getItem('baz')->isHit()); |
| 63 | + } |
| 64 | +} |
0 commit comments