PhpFileCacheTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. use Doctrine\Common\Cache\Cache;
  4. use Doctrine\Common\Cache\PhpFileCache;
  5. /**
  6. * @group DCOM-101
  7. */
  8. class PhpFileCacheTest extends CacheTest
  9. {
  10. /**
  11. * @var \Doctrine\Common\Cache\PhpFileCache
  12. */
  13. private $driver;
  14. protected function _getCacheDriver()
  15. {
  16. $dir = sys_get_temp_dir() . "/doctrine_cache_". uniqid();
  17. $this->assertFalse(is_dir($dir));
  18. $this->driver = new PhpFileCache($dir);
  19. $this->assertTrue(is_dir($dir));
  20. return $this->driver;
  21. }
  22. public function testObjects()
  23. {
  24. $this->markTestSkipped('PhpFileCache does not support saving objects that dont implement __set_state()');
  25. }
  26. public function testLifetime()
  27. {
  28. $cache = $this->_getCacheDriver();
  29. // Test save
  30. $cache->save('test_key', 'testing this out', 10);
  31. // Test contains to test that save() worked
  32. $this->assertTrue($cache->contains('test_key'));
  33. // Test fetch
  34. $this->assertEquals('testing this out', $cache->fetch('test_key'));
  35. // access private methods
  36. $getFilename = new \ReflectionMethod($cache, 'getFilename');
  37. $getNamespacedId = new \ReflectionMethod($cache, 'getNamespacedId');
  38. $getFilename->setAccessible(true);
  39. $getNamespacedId->setAccessible(true);
  40. $id = $getNamespacedId->invoke($cache, 'test_key');
  41. $path = $getFilename->invoke($cache, $id);
  42. $value = include $path;
  43. // update lifetime
  44. $value['lifetime'] = $value['lifetime'] - 20;
  45. file_put_contents($path, '<?php return unserialize(' . var_export(serialize($value), true) . ');');
  46. // test expired data
  47. $this->assertFalse($cache->contains('test_key'));
  48. $this->assertFalse($cache->fetch('test_key'));
  49. }
  50. public function testImplementsSetState()
  51. {
  52. $cache = $this->_getCacheDriver();
  53. // Test save
  54. $cache->save('test_set_state', new SetStateClass(array(1,2,3)));
  55. //Test __set_state call
  56. $this->assertCount(0, SetStateClass::$values);
  57. // Test fetch
  58. $value = $cache->fetch('test_set_state');
  59. $this->assertInstanceOf('Doctrine\Tests\Common\Cache\SetStateClass', $value);
  60. $this->assertEquals(array(1,2,3), $value->getValue());
  61. //Test __set_state call
  62. $this->assertCount(1, SetStateClass::$values);
  63. // Test contains
  64. $this->assertTrue($cache->contains('test_set_state'));
  65. }
  66. public function testNotImplementsSetState()
  67. {
  68. $cache = $this->_getCacheDriver();
  69. $this->setExpectedException('InvalidArgumentException');
  70. $cache->save('test_not_set_state', new NotSetStateClass(array(1,2,3)));
  71. }
  72. public function testGetStats()
  73. {
  74. $cache = $this->_getCacheDriver();
  75. $stats = $cache->getStats();
  76. $this->assertNull($stats[Cache::STATS_HITS]);
  77. $this->assertNull($stats[Cache::STATS_MISSES]);
  78. $this->assertNull($stats[Cache::STATS_UPTIME]);
  79. $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
  80. $this->assertGreaterThan(0, $stats[Cache::STATS_MEMORY_AVAILABLE]);
  81. }
  82. public function tearDown()
  83. {
  84. if (!$this->driver) {
  85. return;
  86. }
  87. $dir = $this->driver->getDirectory();
  88. $ext = $this->driver->getExtension();
  89. $iterator = new \RecursiveDirectoryIterator($dir);
  90. foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) {
  91. if ($file->isFile()) {
  92. @unlink($file->getRealPath());
  93. } else {
  94. @rmdir($file->getRealPath());
  95. }
  96. }
  97. }
  98. }
  99. class NotSetStateClass
  100. {
  101. private $value;
  102. public function __construct($value)
  103. {
  104. $this->value = $value;
  105. }
  106. public function getValue()
  107. {
  108. return $this->value;
  109. }
  110. }
  111. class SetStateClass extends NotSetStateClass
  112. {
  113. public static $values = array();
  114. public static function __set_state($data)
  115. {
  116. self::$values = $data;
  117. return new self($data['value']);
  118. }
  119. }