FilesystemAdapterTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 Psr\Cache\CacheItemPoolInterface;
  12. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  13. /**
  14. * @group time-sensitive
  15. */
  16. class FilesystemAdapterTest extends AdapterTestCase
  17. {
  18. public function createCachePool($defaultLifetime = 0)
  19. {
  20. return new FilesystemAdapter('', $defaultLifetime);
  21. }
  22. public static function tearDownAfterClass()
  23. {
  24. self::rmdir(sys_get_temp_dir().'/symfony-cache');
  25. }
  26. public static function rmdir($dir)
  27. {
  28. if (!file_exists($dir)) {
  29. return;
  30. }
  31. if (!$dir || 0 !== strpos(\dirname($dir), sys_get_temp_dir())) {
  32. throw new \Exception(__METHOD__."() operates only on subdirs of system's temp dir");
  33. }
  34. $children = new \RecursiveIteratorIterator(
  35. new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
  36. \RecursiveIteratorIterator::CHILD_FIRST
  37. );
  38. foreach ($children as $child) {
  39. if ($child->isDir()) {
  40. rmdir($child);
  41. } else {
  42. unlink($child);
  43. }
  44. }
  45. rmdir($dir);
  46. }
  47. protected function isPruned(CacheItemPoolInterface $cache, $name)
  48. {
  49. $getFileMethod = (new \ReflectionObject($cache))->getMethod('getFile');
  50. $getFileMethod->setAccessible(true);
  51. return !file_exists($getFileMethod->invoke($cache, $name));
  52. }
  53. }