PhpFileCacheTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /**
  11. * {@inheritDoc}
  12. *
  13. * @dataProvider provideDataToCache
  14. */
  15. public function testSetContainsFetchDelete($value)
  16. {
  17. if (is_object($value) && ! method_exists($value, '__set_state')) {
  18. $this->markTestSkipped('PhpFileCache only allows objects that implement __set_state() and fully support var_export()');
  19. }
  20. if (0.0 === $value) {
  21. $cache = $this->_getCacheDriver();
  22. $this->assertTrue($cache->save('key', $value));
  23. $this->assertTrue($cache->contains('key'));
  24. $this->assertSame(0, $cache->fetch('key'), 'var_export exports float(0) as int(0) so we assert against 0 as integer');
  25. $this->assertTrue($cache->delete('key'));
  26. $this->assertFalse($cache->contains('key'));
  27. $this->assertFalse($cache->fetch('key'));
  28. } else {
  29. parent::testSetContainsFetchDelete($value);
  30. }
  31. }
  32. /**
  33. * {@inheritDoc}
  34. *
  35. * @dataProvider provideDataToCache
  36. */
  37. public function testUpdateExistingEntry($value)
  38. {
  39. if (is_object($value) && ! method_exists($value, '__set_state')) {
  40. $this->markTestSkipped('PhpFileCache only allows objects that implement __set_state() and fully support var_export()');
  41. }
  42. if (0.0 === $value) {
  43. $cache = $this->_getCacheDriver();
  44. $this->assertTrue($cache->save('key', 'old-value'));
  45. $this->assertTrue($cache->contains('key'));
  46. $this->assertTrue($cache->save('key', $value));
  47. $this->assertTrue($cache->contains('key'));
  48. $this->assertSame(0, $cache->fetch('key'), 'var_export exports float(0) as int(0) so we assert against 0 as integer');
  49. } else {
  50. parent::testUpdateExistingEntry($value);
  51. }
  52. }
  53. public function testLifetime()
  54. {
  55. $cache = $this->_getCacheDriver();
  56. // Test save
  57. $cache->save('test_key', 'testing this out', 10);
  58. // Test contains to test that save() worked
  59. $this->assertTrue($cache->contains('test_key'));
  60. // Test fetch
  61. $this->assertEquals('testing this out', $cache->fetch('test_key'));
  62. // access private methods
  63. $getFilename = new \ReflectionMethod($cache, 'getFilename');
  64. $getNamespacedId = new \ReflectionMethod($cache, 'getNamespacedId');
  65. $getFilename->setAccessible(true);
  66. $getNamespacedId->setAccessible(true);
  67. $id = $getNamespacedId->invoke($cache, 'test_key');
  68. $path = $getFilename->invoke($cache, $id);
  69. $value = include $path;
  70. // update lifetime
  71. $value['lifetime'] = $value['lifetime'] - 20;
  72. file_put_contents($path, '<?php return unserialize(' . var_export(serialize($value), true) . ');');
  73. // test expired data
  74. $this->assertFalse($cache->contains('test_key'));
  75. $this->assertFalse($cache->fetch('test_key'));
  76. }
  77. public function testImplementsSetState()
  78. {
  79. $cache = $this->_getCacheDriver();
  80. // Test save
  81. $cache->save('test_set_state', new SetStateClass(array(1,2,3)));
  82. //Test __set_state call
  83. $this->assertCount(0, SetStateClass::$values);
  84. // Test fetch
  85. $value = $cache->fetch('test_set_state');
  86. $this->assertInstanceOf('Doctrine\Tests\Common\Cache\SetStateClass', $value);
  87. $this->assertEquals(array(1,2,3), $value->getValue());
  88. //Test __set_state call
  89. $this->assertCount(1, SetStateClass::$values);
  90. // Test contains
  91. $this->assertTrue($cache->contains('test_set_state'));
  92. }
  93. public function testNotImplementsSetState()
  94. {
  95. $cache = $this->_getCacheDriver();
  96. $this->setExpectedException('InvalidArgumentException');
  97. $cache->save('test_not_set_state', new NotSetStateClass(array(1,2,3)));
  98. }
  99. public function testGetStats()
  100. {
  101. $cache = $this->_getCacheDriver();
  102. $stats = $cache->getStats();
  103. $this->assertNull($stats[Cache::STATS_HITS]);
  104. $this->assertNull($stats[Cache::STATS_MISSES]);
  105. $this->assertNull($stats[Cache::STATS_UPTIME]);
  106. $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
  107. $this->assertGreaterThan(0, $stats[Cache::STATS_MEMORY_AVAILABLE]);
  108. }
  109. public function testCachedObject()
  110. {
  111. $this->markTestSkipped("PhpFileCache cannot handle objects that don't implement __set_state.");
  112. }
  113. public function testFetchMultipleObjects()
  114. {
  115. $this->markTestSkipped("PhpFileCache cannot handle objects that don't implement __set_state.");
  116. }
  117. protected function _getCacheDriver()
  118. {
  119. return new PhpFileCache($this->directory);
  120. }
  121. }
  122. class NotSetStateClass
  123. {
  124. private $value;
  125. public function __construct($value)
  126. {
  127. $this->value = $value;
  128. }
  129. public function getValue()
  130. {
  131. return $this->value;
  132. }
  133. }
  134. class SetStateClass extends NotSetStateClass
  135. {
  136. public static $values = array();
  137. public static function __set_state($data)
  138. {
  139. self::$values = $data;
  140. return new self($data['value']);
  141. }
  142. }