CacheTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 provideDataToCache
  9. */
  10. public function testSetContainsFetchDelete($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. if (is_object($value)) {
  17. $this->assertEquals($value, $cache->fetch('key'), 'Objects retrieved from the cache must be equal but not necessarily the same reference');
  18. } else {
  19. $this->assertSame($value, $cache->fetch('key'), 'Scalar and array data retrieved from the cache must be the same as the original, e.g. same type');
  20. }
  21. // Test deleting a value
  22. $this->assertTrue($cache->delete('key'));
  23. $this->assertFalse($cache->contains('key'));
  24. $this->assertFalse($cache->fetch('key'));
  25. }
  26. /**
  27. * @dataProvider provideDataToCache
  28. */
  29. public function testUpdateExistingEntry($value)
  30. {
  31. $cache = $this->_getCacheDriver();
  32. $this->assertTrue($cache->save('key', 'old-value'));
  33. $this->assertTrue($cache->contains('key'));
  34. $this->assertTrue($cache->save('key', $value));
  35. $this->assertTrue($cache->contains('key'));
  36. if (is_object($value)) {
  37. $this->assertEquals($value, $cache->fetch('key'), 'Objects retrieved from the cache must be equal but not necessarily the same reference');
  38. } else {
  39. $this->assertSame($value, $cache->fetch('key'), 'Scalar and array data retrieved from the cache must be the same as the original, e.g. same type');
  40. }
  41. }
  42. public function testFetchMulti()
  43. {
  44. $cache = $this->_getCacheDriver();
  45. $cache->deleteAll();
  46. // Test saving some values, checking if it exists, and fetching it back with multiGet
  47. $this->assertTrue($cache->save('key1', 'value1'));
  48. $this->assertTrue($cache->save('key2', 'value2'));
  49. $this->assertEquals(
  50. array('key1' => 'value1', 'key2' => 'value2'),
  51. $cache->fetchMultiple(array('key1', 'key2'))
  52. );
  53. $this->assertEquals(
  54. array('key1' => 'value1', 'key2' => 'value2'),
  55. $cache->fetchMultiple(array('key1', 'key3', 'key2'))
  56. );
  57. $this->assertEquals(
  58. array('key1' => 'value1', 'key2' => 'value2'),
  59. $cache->fetchMultiple(array('key1', 'key2', 'key3'))
  60. );
  61. }
  62. public function testFetchMultiWithEmptyKeysArray()
  63. {
  64. $cache = $this->_getCacheDriver();
  65. $this->assertEmpty(
  66. $cache->fetchMultiple(array())
  67. );
  68. }
  69. public function testFetchMultiWithFalsey()
  70. {
  71. $cache = $this->_getCacheDriver();
  72. $cache->deleteAll();
  73. $values = array(
  74. 'string' => 'str',
  75. 'integer' => 1,
  76. 'boolean' => true,
  77. 'null' => null,
  78. 'array_empty' => array(),
  79. 'integer_zero' => 0,
  80. 'string_empty' => ''
  81. );
  82. foreach ($values AS $key => $value) {
  83. $cache->save($key, $value);
  84. }
  85. $this->assertEquals(
  86. $values,
  87. $cache->fetchMultiple(array_keys($values))
  88. );
  89. }
  90. public function provideDataToCache()
  91. {
  92. return array(
  93. 'array' => array(array('one', 2, 3.01)),
  94. 'string' => array('value'),
  95. 'integer' => array(1),
  96. 'float' => array(1.5),
  97. 'object' => array(new ArrayObject()),
  98. 'true' => array(true),
  99. // the following are considered FALSE in boolean context, but caches should still recognize their existence
  100. 'null' => array(null),
  101. 'false' => array(false),
  102. 'array_empty' => array(array()),
  103. 'string_zero' => array('0'),
  104. 'integer_zero' => array(0),
  105. 'float_zero' => array(0.0),
  106. 'string_empty' => array('')
  107. );
  108. }
  109. public function testDeleteIsSuccessfulWhenKeyDoesNotExist()
  110. {
  111. $cache = $this->_getCacheDriver();
  112. $this->assertFalse($cache->contains('key'));
  113. $this->assertTrue($cache->delete('key'));
  114. }
  115. public function testDeleteAll()
  116. {
  117. $cache = $this->_getCacheDriver();
  118. $this->assertTrue($cache->save('key1', 1));
  119. $this->assertTrue($cache->save('key2', 2));
  120. $this->assertTrue($cache->deleteAll());
  121. $this->assertFalse($cache->contains('key1'));
  122. $this->assertFalse($cache->contains('key2'));
  123. }
  124. public function testDeleteAllAndNamespaceVersioningBetweenCaches()
  125. {
  126. if ( ! $this->isSharedStorage()) {
  127. $this->markTestSkipped('The ' . __CLASS__ .' does not use shared storage');
  128. }
  129. $cache1 = $this->_getCacheDriver();
  130. $cache2 = $this->_getCacheDriver();
  131. $this->assertTrue($cache1->save('key1', 1));
  132. $this->assertTrue($cache2->save('key2', 2));
  133. /* Both providers are initialized with the same namespace version, so
  134. * they can see entries set by each other.
  135. */
  136. $this->assertTrue($cache1->contains('key1'));
  137. $this->assertTrue($cache1->contains('key2'));
  138. $this->assertTrue($cache2->contains('key1'));
  139. $this->assertTrue($cache2->contains('key2'));
  140. /* Deleting all entries through one provider will only increment the
  141. * namespace version on that object (and in the cache itself, which new
  142. * instances will use to initialize). The second provider will retain
  143. * its original version and still see stale data.
  144. */
  145. $this->assertTrue($cache1->deleteAll());
  146. $this->assertFalse($cache1->contains('key1'));
  147. $this->assertFalse($cache1->contains('key2'));
  148. $this->assertTrue($cache2->contains('key1'));
  149. $this->assertTrue($cache2->contains('key2'));
  150. /* A new cache provider should not see the deleted entries, since its
  151. * namespace version will be initialized.
  152. */
  153. $cache3 = $this->_getCacheDriver();
  154. $this->assertFalse($cache3->contains('key1'));
  155. $this->assertFalse($cache3->contains('key2'));
  156. }
  157. public function testFlushAll()
  158. {
  159. $cache = $this->_getCacheDriver();
  160. $this->assertTrue($cache->save('key1', 1));
  161. $this->assertTrue($cache->save('key2', 2));
  162. $this->assertTrue($cache->flushAll());
  163. $this->assertFalse($cache->contains('key1'));
  164. $this->assertFalse($cache->contains('key2'));
  165. }
  166. public function testFlushAllAndNamespaceVersioningBetweenCaches()
  167. {
  168. if ( ! $this->isSharedStorage()) {
  169. $this->markTestSkipped('The ' . __CLASS__ .' does not use shared storage');
  170. }
  171. $cache1 = $this->_getCacheDriver();
  172. $cache2 = $this->_getCacheDriver();
  173. /* Deleting all elements from the first provider should increment its
  174. * namespace version before saving the first entry.
  175. */
  176. $cache1->deleteAll();
  177. $this->assertTrue($cache1->save('key1', 1));
  178. /* The second provider will be initialized with the same namespace
  179. * version upon its first save operation.
  180. */
  181. $this->assertTrue($cache2->save('key2', 2));
  182. /* Both providers have the same namespace version and can see entries
  183. * set by each other.
  184. */
  185. $this->assertTrue($cache1->contains('key1'));
  186. $this->assertTrue($cache1->contains('key2'));
  187. $this->assertTrue($cache2->contains('key1'));
  188. $this->assertTrue($cache2->contains('key2'));
  189. /* Flushing all entries through one cache will remove all entries from
  190. * the cache but leave their namespace version as-is.
  191. */
  192. $this->assertTrue($cache1->flushAll());
  193. $this->assertFalse($cache1->contains('key1'));
  194. $this->assertFalse($cache1->contains('key2'));
  195. $this->assertFalse($cache2->contains('key1'));
  196. $this->assertFalse($cache2->contains('key2'));
  197. /* Inserting a new entry will use the same, incremented namespace
  198. * version, and it will be visible to both providers.
  199. */
  200. $this->assertTrue($cache1->save('key1', 1));
  201. $this->assertTrue($cache1->contains('key1'));
  202. $this->assertTrue($cache2->contains('key1'));
  203. /* A new cache provider will be initialized with the original namespace
  204. * version and not share any visibility with the first two providers.
  205. */
  206. $cache3 = $this->_getCacheDriver();
  207. $this->assertFalse($cache3->contains('key1'));
  208. $this->assertFalse($cache3->contains('key2'));
  209. $this->assertTrue($cache3->save('key3', 3));
  210. $this->assertTrue($cache3->contains('key3'));
  211. }
  212. public function testNamespace()
  213. {
  214. $cache = $this->_getCacheDriver();
  215. $cache->setNamespace('ns1_');
  216. $this->assertTrue($cache->save('key1', 1));
  217. $this->assertTrue($cache->contains('key1'));
  218. $cache->setNamespace('ns2_');
  219. $this->assertFalse($cache->contains('key1'));
  220. }
  221. public function testDeleteAllNamespace()
  222. {
  223. $cache = $this->_getCacheDriver();
  224. $cache->setNamespace('ns1');
  225. $this->assertFalse($cache->contains('key1'));
  226. $cache->save('key1', 'test');
  227. $this->assertTrue($cache->contains('key1'));
  228. $cache->setNamespace('ns2');
  229. $this->assertFalse($cache->contains('key1'));
  230. $cache->save('key1', 'test');
  231. $this->assertTrue($cache->contains('key1'));
  232. $cache->setNamespace('ns1');
  233. $this->assertTrue($cache->contains('key1'));
  234. $cache->deleteAll();
  235. $this->assertFalse($cache->contains('key1'));
  236. $cache->setNamespace('ns2');
  237. $this->assertTrue($cache->contains('key1'));
  238. $cache->deleteAll();
  239. $this->assertFalse($cache->contains('key1'));
  240. }
  241. /**
  242. * @group DCOM-43
  243. */
  244. public function testGetStats()
  245. {
  246. $cache = $this->_getCacheDriver();
  247. $stats = $cache->getStats();
  248. $this->assertArrayHasKey(Cache::STATS_HITS, $stats);
  249. $this->assertArrayHasKey(Cache::STATS_MISSES, $stats);
  250. $this->assertArrayHasKey(Cache::STATS_UPTIME, $stats);
  251. $this->assertArrayHasKey(Cache::STATS_MEMORY_USAGE, $stats);
  252. $this->assertArrayHasKey(Cache::STATS_MEMORY_AVAILABLE, $stats);
  253. }
  254. public function testFetchMissShouldReturnFalse()
  255. {
  256. $cache = $this->_getCacheDriver();
  257. /* Ensure that caches return boolean false instead of null on a fetch
  258. * miss to be compatible with ORM integration.
  259. */
  260. $result = $cache->fetch('nonexistent_key');
  261. $this->assertFalse($result);
  262. $this->assertNotNull($result);
  263. }
  264. /**
  265. * Check to see that objects are correctly serialized and unserialized by the cache
  266. * provider.
  267. */
  268. public function testCachedObject()
  269. {
  270. $cache = $this->_getCacheDriver();
  271. $cache->deleteAll();
  272. $obj = new \stdClass();
  273. $obj->foo = "bar";
  274. $obj2 = new \stdClass();
  275. $obj2->bar = "foo";
  276. $obj2->obj = $obj;
  277. $obj->obj2 = $obj2;
  278. $cache->save("obj", $obj);
  279. $fetched = $cache->fetch("obj");
  280. $this->assertInstanceOf("stdClass", $obj);
  281. $this->assertInstanceOf("stdClass", $obj->obj2);
  282. $this->assertInstanceOf("stdClass", $obj->obj2->obj);
  283. $this->assertEquals("bar", $fetched->foo);
  284. $this->assertEquals("foo", $fetched->obj2->bar);
  285. }
  286. /**
  287. * Check to see that objects fetched via fetchMultiple are properly unserialized
  288. */
  289. public function testFetchMultipleObjects()
  290. {
  291. $cache = $this->_getCacheDriver();
  292. $cache->deleteAll();
  293. $obj1 = new \stdClass();
  294. $obj1->foo = "bar";
  295. $cache->save("obj1", $obj1);
  296. $obj2 = new \stdClass();
  297. $obj2->bar = "baz";
  298. $cache->save("obj2", $obj2);
  299. $fetched = $cache->fetchMultiple(array("obj1", "obj2"));
  300. $this->assertInstanceOf("stdClass", $fetched["obj1"]);
  301. $this->assertInstanceOf("stdClass", $fetched["obj2"]);
  302. $this->assertEquals("bar", $fetched["obj1"]->foo);
  303. $this->assertEquals("baz", $fetched["obj2"]->bar);
  304. }
  305. /**
  306. * Return whether multiple cache providers share the same storage.
  307. *
  308. * This is used for skipping certain tests for shared storage behavior.
  309. *
  310. * @return boolean
  311. */
  312. protected function isSharedStorage()
  313. {
  314. return true;
  315. }
  316. /**
  317. * @return \Doctrine\Common\Cache\CacheProvider
  318. */
  319. abstract protected function _getCacheDriver();
  320. }