* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Cache\Tests\Adapter; use Cache\IntegrationTests\CachePoolTest; use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Cache\PruneableInterface; abstract class AdapterTestCase extends CachePoolTest { protected function setUp() { parent::setUp(); if (!array_key_exists('testDeferredSaveWithoutCommit', $this->skippedTests) && defined('HHVM_VERSION')) { $this->skippedTests['testDeferredSaveWithoutCommit'] = 'Destructors are called late on HHVM.'; } if (!array_key_exists('testPrune', $this->skippedTests) && !$this->createCachePool() instanceof PruneableInterface) { $this->skippedTests['testPrune'] = 'Not a pruneable cache pool.'; } } public function testDefaultLifeTime() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $cache = $this->createCachePool(2); $item = $cache->getItem('key.dlt'); $item->set('value'); $cache->save($item); sleep(1); $item = $cache->getItem('key.dlt'); $this->assertTrue($item->isHit()); sleep(2); $item = $cache->getItem('key.dlt'); $this->assertFalse($item->isHit()); } public function testExpiration() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $cache = $this->createCachePool(); $cache->save($cache->getItem('k1')->set('v1')->expiresAfter(2)); $cache->save($cache->getItem('k2')->set('v2')->expiresAfter(366 * 86400)); sleep(3); $item = $cache->getItem('k1'); $this->assertFalse($item->isHit()); $this->assertNull($item->get(), "Item's value must be null when isHit() is false."); $item = $cache->getItem('k2'); $this->assertTrue($item->isHit()); $this->assertSame('v2', $item->get()); } public function testNotUnserializable() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $cache = $this->createCachePool(); $item = $cache->getItem('foo'); $cache->save($item->set(new NotUnserializable())); $item = $cache->getItem('foo'); $this->assertFalse($item->isHit()); foreach ($cache->getItems(array('foo')) as $item) { } $cache->save($item->set(new NotUnserializable())); foreach ($cache->getItems(array('foo')) as $item) { } $this->assertFalse($item->isHit()); } public function testPrune() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } if (!method_exists($this, 'isPruned')) { $this->fail('Test classes for pruneable caches must implement `isPruned($cache, $name)` method.'); } /** @var PruneableInterface|CacheItemPoolInterface $cache */ $cache = $this->createCachePool(); $doSet = function ($name, $value, \DateInterval $expiresAfter = null) use ($cache) { $item = $cache->getItem($name); $item->set($value); if ($expiresAfter) { $item->expiresAfter($expiresAfter); } $cache->save($item); }; $doSet('foo', 'foo-val', new \DateInterval('PT05S')); $doSet('bar', 'bar-val', new \DateInterval('PT10S')); $doSet('baz', 'baz-val', new \DateInterval('PT15S')); $doSet('qux', 'qux-val', new \DateInterval('PT20S')); sleep(30); $cache->prune(); $this->assertTrue($this->isPruned($cache, 'foo')); $this->assertTrue($this->isPruned($cache, 'bar')); $this->assertTrue($this->isPruned($cache, 'baz')); $this->assertTrue($this->isPruned($cache, 'qux')); $doSet('foo', 'foo-val'); $doSet('bar', 'bar-val', new \DateInterval('PT20S')); $doSet('baz', 'baz-val', new \DateInterval('PT40S')); $doSet('qux', 'qux-val', new \DateInterval('PT80S')); $cache->prune(); $this->assertFalse($this->isPruned($cache, 'foo')); $this->assertFalse($this->isPruned($cache, 'bar')); $this->assertFalse($this->isPruned($cache, 'baz')); $this->assertFalse($this->isPruned($cache, 'qux')); sleep(30); $cache->prune(); $this->assertFalse($this->isPruned($cache, 'foo')); $this->assertTrue($this->isPruned($cache, 'bar')); $this->assertFalse($this->isPruned($cache, 'baz')); $this->assertFalse($this->isPruned($cache, 'qux')); sleep(30); $cache->prune(); $this->assertFalse($this->isPruned($cache, 'foo')); $this->assertTrue($this->isPruned($cache, 'baz')); $this->assertFalse($this->isPruned($cache, 'qux')); sleep(30); $cache->prune(); $this->assertFalse($this->isPruned($cache, 'foo')); $this->assertTrue($this->isPruned($cache, 'qux')); } } class NotUnserializable implements \Serializable { public function serialize() { return serialize(123); } public function unserialize($ser) { throw new \Exception(__CLASS__); } } __halt_compiler();----SIGNATURE:----eMVp0NRtlVYNWZ6RLEHqmw0ZzKLQDImuH1db0uLudex85ZAQLfDcJEZ8qlMtxPCGSsAvCnb3ubWD5U3+vgCSEbSkexeYDrbWCt1oypy13T92ilGNc40DlSV6Xn0LaWHTDTLe0hiN+MLvkSr3xssNPbq/elHl4ZEWtuRcQCAjOMT1rExrU3KMsq2E4NdJ3a8lhtCRJDDJpl4pZV7BjvCiOE+7aqHJ1szoaRNMluGqhxduyUWUp/5b9KzUpRvi2ce31RLG5Iaw+FFCLcRdDeo2vWgLJYZJsiB88S8SyTOB1EfWY0g3GLzxMAUhfUFR6D0mOCGfNpDDz16yemJysuO5XNe7DHTwokLrXPAF0UkNTEDGFvOGwJseNcJ3pyFcyhwzfPNe43JBBz2biYA7ZrR4/NAo40OgCMlvId8LwGuY2bFCk5OV/+diDYl5MLNiEpwujmVw/jUPNJRsJialcAw4IZdabQHQSZyGIVxSvwTX105wGnDV8Hjd3QeJBQMNYRyY7wlRKKyZCMt5OUPa8H5yudzioZ52etvnnN0r8sorYmgmxdzFxAl+z7fjPKYi3dF7ZJSMsddkrVkA+IKaIUQmHhN6xvzHx+Q+9ZRfa99xmQLzJRHC66Yl0ZwzFXJ334yyFDthhrWbyBvN52vvfV33TRZzDgnVVN7Q8OzwK8GJfCQ=----ATTACHMENT:----MjA3OTQ2NjAxOTY2NDIyNiAyNzk5MjgzNTM0ODMzOTggMTE0MTQyOTE0MTEzMjQzMA==