PhpFileCacheTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 BaseFileCacheTest
  9. {
  10. public function testLifetime()
  11. {
  12. $cache = $this->_getCacheDriver();
  13. // Test save
  14. $cache->save('test_key', 'testing this out', 10);
  15. // Test contains to test that save() worked
  16. $this->assertTrue($cache->contains('test_key'));
  17. // Test fetch
  18. $this->assertEquals('testing this out', $cache->fetch('test_key'));
  19. // access private methods
  20. $getFilename = new \ReflectionMethod($cache, 'getFilename');
  21. $getNamespacedId = new \ReflectionMethod($cache, 'getNamespacedId');
  22. $getFilename->setAccessible(true);
  23. $getNamespacedId->setAccessible(true);
  24. $id = $getNamespacedId->invoke($cache, 'test_key');
  25. $path = $getFilename->invoke($cache, $id);
  26. $value = include $path;
  27. // update lifetime
  28. $value['lifetime'] = $value['lifetime'] - 20;
  29. file_put_contents($path, '<?php return unserialize(' . var_export(serialize($value), true) . ');');
  30. // test expired data
  31. $this->assertFalse($cache->contains('test_key'));
  32. $this->assertFalse($cache->fetch('test_key'));
  33. }
  34. public function testImplementsSetState()
  35. {
  36. $cache = $this->_getCacheDriver();
  37. // Test save
  38. $cache->save('test_set_state', new SetStateClass(array(1,2,3)));
  39. //Test __set_state call
  40. $this->assertCount(0, SetStateClass::$values);
  41. // Test fetch
  42. $value = $cache->fetch('test_set_state');
  43. $this->assertInstanceOf('Doctrine\Tests\Common\Cache\SetStateClass', $value);
  44. $this->assertEquals(array(1,2,3), $value->getValue());
  45. //Test __set_state call
  46. $this->assertCount(1, SetStateClass::$values);
  47. // Test contains
  48. $this->assertTrue($cache->contains('test_set_state'));
  49. }
  50. public function testNotImplementsSetState()
  51. {
  52. $cache = $this->_getCacheDriver();
  53. $this->setExpectedException('InvalidArgumentException');
  54. $cache->save('test_not_set_state', new NotSetStateClass(array(1,2,3)));
  55. }
  56. public function testGetStats()
  57. {
  58. $cache = $this->_getCacheDriver();
  59. $stats = $cache->getStats();
  60. $this->assertNull($stats[Cache::STATS_HITS]);
  61. $this->assertNull($stats[Cache::STATS_MISSES]);
  62. $this->assertNull($stats[Cache::STATS_UPTIME]);
  63. $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
  64. $this->assertGreaterThan(0, $stats[Cache::STATS_MEMORY_AVAILABLE]);
  65. }
  66. protected function _getCacheDriver()
  67. {
  68. return new PhpFileCache($this->directory);
  69. }
  70. }
  71. class NotSetStateClass
  72. {
  73. private $value;
  74. public function __construct($value)
  75. {
  76. $this->value = $value;
  77. }
  78. public function getValue()
  79. {
  80. return $this->value;
  81. }
  82. }
  83. class SetStateClass extends NotSetStateClass
  84. {
  85. public static $values = array();
  86. public static function __set_state($data)
  87. {
  88. self::$values = $data;
  89. return new self($data['value']);
  90. }
  91. }