PdoAdapterTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Tests\Adapter;
  11. use Symfony\Component\Cache\Adapter\PdoAdapter;
  12. use Symfony\Component\Cache\Tests\Traits\PdoPruneableTrait;
  13. /**
  14. * @group time-sensitive
  15. */
  16. class PdoAdapterTest extends AdapterTestCase
  17. {
  18. use PdoPruneableTrait;
  19. protected static $dbFile;
  20. public static function setupBeforeClass()
  21. {
  22. if (!\extension_loaded('pdo_sqlite')) {
  23. self::markTestSkipped('Extension pdo_sqlite required.');
  24. }
  25. self::$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
  26. $pool = new PdoAdapter('sqlite:'.self::$dbFile);
  27. $pool->createTable();
  28. }
  29. public static function tearDownAfterClass()
  30. {
  31. @unlink(self::$dbFile);
  32. }
  33. public function createCachePool($defaultLifetime = 0)
  34. {
  35. return new PdoAdapter('sqlite:'.self::$dbFile, 'ns', $defaultLifetime);
  36. }
  37. public function testCleanupExpiredItems()
  38. {
  39. $pdo = new \PDO('sqlite:'.self::$dbFile);
  40. $getCacheItemCount = function () use ($pdo) {
  41. return (int) $pdo->query('SELECT COUNT(*) FROM cache_items')->fetch(\PDO::FETCH_COLUMN);
  42. };
  43. $this->assertSame(0, $getCacheItemCount());
  44. $cache = $this->createCachePool();
  45. $item = $cache->getItem('some_nice_key');
  46. $item->expiresAfter(1);
  47. $item->set(1);
  48. $cache->save($item);
  49. $this->assertSame(1, $getCacheItemCount());
  50. sleep(2);
  51. $newItem = $cache->getItem($item->getKey());
  52. $this->assertFalse($newItem->isHit());
  53. $this->assertSame(0, $getCacheItemCount(), 'PDOAdapter must clean up expired items');
  54. }
  55. }