CacheTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. use Doctrine\Common\Cache\Cache;
  4. use ArrayObject;
  5. abstract class CacheTest extends \Doctrine\Tests\DoctrineTestCase
  6. {
  7. /**
  8. * @dataProvider provideCrudValues
  9. */
  10. public function testBasicCrudOperations($value)
  11. {
  12. $cache = $this->_getCacheDriver();
  13. // Test saving a value, checking if it exists, and fetching it back
  14. $this->assertTrue($cache->save('key', 'value'));
  15. $this->assertTrue($cache->contains('key'));
  16. $this->assertEquals('value', $cache->fetch('key'));
  17. // Test updating the value of a cache entry
  18. $this->assertTrue($cache->save('key', 'value-changed'));
  19. $this->assertTrue($cache->contains('key'));
  20. $this->assertEquals('value-changed', $cache->fetch('key'));
  21. // Test deleting a value
  22. $this->assertTrue($cache->delete('key'));
  23. $this->assertFalse($cache->contains('key'));
  24. }
  25. public function provideCrudValues()
  26. {
  27. return array(
  28. 'array' => array(array('one', 2, 3.0)),
  29. 'string' => array('value'),
  30. 'integer' => array(1),
  31. 'float' => array(1.5),
  32. 'object' => array(new ArrayObject()),
  33. );
  34. }
  35. public function testDeleteAll()
  36. {
  37. $cache = $this->_getCacheDriver();
  38. $this->assertTrue($cache->save('key1', 1));
  39. $this->assertTrue($cache->save('key2', 2));
  40. $this->assertTrue($cache->deleteAll());
  41. $this->assertFalse($cache->contains('key1'));
  42. $this->assertFalse($cache->contains('key2'));
  43. }
  44. public function testDeleteAllAndNamespaceVersioningBetweenCaches()
  45. {
  46. if ( ! $this->isSharedStorage()) {
  47. $this->markTestSkipped('The ' . __CLASS__ .' does not use shared storage');
  48. }
  49. $cache1 = $this->_getCacheDriver();
  50. $cache2 = $this->_getCacheDriver();
  51. $this->assertTrue($cache1->save('key1', 1));
  52. $this->assertTrue($cache2->save('key2', 2));
  53. /* Both providers are initialized with the same namespace version, so
  54. * they can see entries set by each other.
  55. */
  56. $this->assertTrue($cache1->contains('key1'));
  57. $this->assertTrue($cache1->contains('key2'));
  58. $this->assertTrue($cache2->contains('key1'));
  59. $this->assertTrue($cache2->contains('key2'));
  60. /* Deleting all entries through one provider will only increment the
  61. * namespace version on that object (and in the cache itself, which new
  62. * instances will use to initialize). The second provider will retain
  63. * its original version and still see stale data.
  64. */
  65. $this->assertTrue($cache1->deleteAll());
  66. $this->assertFalse($cache1->contains('key1'));
  67. $this->assertFalse($cache1->contains('key2'));
  68. $this->assertTrue($cache2->contains('key1'));
  69. $this->assertTrue($cache2->contains('key2'));
  70. /* A new cache provider should not see the deleted entries, since its
  71. * namespace version will be initialized.
  72. */
  73. $cache3 = $this->_getCacheDriver();
  74. $this->assertFalse($cache3->contains('key1'));
  75. $this->assertFalse($cache3->contains('key2'));
  76. }
  77. public function testFlushAll()
  78. {
  79. $cache = $this->_getCacheDriver();
  80. $this->assertTrue($cache->save('key1', 1));
  81. $this->assertTrue($cache->save('key2', 2));
  82. $this->assertTrue($cache->flushAll());
  83. $this->assertFalse($cache->contains('key1'));
  84. $this->assertFalse($cache->contains('key2'));
  85. }
  86. public function testFlushAllAndNamespaceVersioningBetweenCaches()
  87. {
  88. if ( ! $this->isSharedStorage()) {
  89. $this->markTestSkipped('The ' . __CLASS__ .' does not use shared storage');
  90. }
  91. $cache1 = $this->_getCacheDriver();
  92. $cache2 = $this->_getCacheDriver();
  93. /* Deleting all elements from the first provider should increment its
  94. * namespace version before saving the first entry.
  95. */
  96. $cache1->deleteAll();
  97. $this->assertTrue($cache1->save('key1', 1));
  98. /* The second provider will be initialized with the same namespace
  99. * version upon its first save operation.
  100. */
  101. $this->assertTrue($cache2->save('key2', 2));
  102. /* Both providers have the same namespace version and can see entires
  103. * set by each other.
  104. */
  105. $this->assertTrue($cache1->contains('key1'));
  106. $this->assertTrue($cache1->contains('key2'));
  107. $this->assertTrue($cache2->contains('key1'));
  108. $this->assertTrue($cache2->contains('key2'));
  109. /* Flushing all entries through one cache will remove all entries from
  110. * the cache but leave their namespace version as-is.
  111. */
  112. $this->assertTrue($cache1->flushAll());
  113. $this->assertFalse($cache1->contains('key1'));
  114. $this->assertFalse($cache1->contains('key2'));
  115. $this->assertFalse($cache2->contains('key1'));
  116. $this->assertFalse($cache2->contains('key2'));
  117. /* Inserting a new entry will use the same, incremented namespace
  118. * version, and it will be visible to both providers.
  119. */
  120. $this->assertTrue($cache1->save('key1', 1));
  121. $this->assertTrue($cache1->contains('key1'));
  122. $this->assertTrue($cache2->contains('key1'));
  123. /* A new cache provider will be initialized with the original namespace
  124. * version and not share any visibility with the first two providers.
  125. */
  126. $cache3 = $this->_getCacheDriver();
  127. $this->assertFalse($cache3->contains('key1'));
  128. $this->assertFalse($cache3->contains('key2'));
  129. $this->assertTrue($cache3->save('key3', 3));
  130. $this->assertTrue($cache3->contains('key3'));
  131. }
  132. public function testNamespace()
  133. {
  134. $cache = $this->_getCacheDriver();
  135. $cache->setNamespace('ns1_');
  136. $this->assertTrue($cache->save('key1', 1));
  137. $this->assertTrue($cache->contains('key1'));
  138. $cache->setNamespace('ns2_');
  139. $this->assertFalse($cache->contains('key1'));
  140. }
  141. public function testDeleteAllNamespace()
  142. {
  143. $cache = $this->_getCacheDriver();
  144. $cache->setNamespace('ns1');
  145. $this->assertFalse($cache->contains('key1'));
  146. $cache->save('key1', 'test');
  147. $this->assertTrue($cache->contains('key1'));
  148. $cache->setNamespace('ns2');
  149. $this->assertFalse($cache->contains('key1'));
  150. $cache->save('key1', 'test');
  151. $this->assertTrue($cache->contains('key1'));
  152. $cache->setNamespace('ns1');
  153. $this->assertTrue($cache->contains('key1'));
  154. $cache->deleteAll();
  155. $this->assertFalse($cache->contains('key1'));
  156. $cache->setNamespace('ns2');
  157. $this->assertTrue($cache->contains('key1'));
  158. $cache->deleteAll();
  159. $this->assertFalse($cache->contains('key1'));
  160. }
  161. /**
  162. * @group DCOM-43
  163. */
  164. public function testGetStats()
  165. {
  166. $cache = $this->_getCacheDriver();
  167. $stats = $cache->getStats();
  168. $this->assertArrayHasKey(Cache::STATS_HITS, $stats);
  169. $this->assertArrayHasKey(Cache::STATS_MISSES, $stats);
  170. $this->assertArrayHasKey(Cache::STATS_UPTIME, $stats);
  171. $this->assertArrayHasKey(Cache::STATS_MEMORY_USAGE, $stats);
  172. $this->assertArrayHasKey(Cache::STATS_MEMORY_AVAILABLE, $stats);
  173. }
  174. public function testFetchMissShouldReturnFalse()
  175. {
  176. $cache = $this->_getCacheDriver();
  177. /* Ensure that caches return boolean false instead of null on a fetch
  178. * miss to be compatible with ORM integration.
  179. */
  180. $result = $cache->fetch('nonexistent_key');
  181. $this->assertFalse($result);
  182. $this->assertNotNull($result);
  183. }
  184. /**
  185. * Return whether multiple cache providers share the same storage.
  186. *
  187. * This is used for skipping certain tests for shared storage behavior.
  188. *
  189. * @return boolean
  190. */
  191. protected function isSharedStorage()
  192. {
  193. return true;
  194. }
  195. /**
  196. * @return \Doctrine\Common\Cache\CacheProvider
  197. */
  198. abstract protected function _getCacheDriver();
  199. }