AbstractCacheTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. namespace Sabre\Cache;
  3. use Psr\SimpleCache\CacheInterface;
  4. /**
  5. * Abstract PSR-16 tester.
  6. *
  7. * Because all cache implementations should mostly behave the same way, they
  8. * can all extend this test.
  9. */
  10. abstract class AbstractCacheTest extends \PHPUnit\Framework\TestCase {
  11. abstract function getCache() : CacheInterface;
  12. function testSetGet() {
  13. $cache = $this->getCache();
  14. $cache->set('foo', 'bar');
  15. $this->assertEquals('bar', $cache->get('foo'));
  16. }
  17. /**
  18. * @depends testSetGet
  19. */
  20. function testDelete() {
  21. $cache = $this->getCache();
  22. $cache->set('foo', 'bar');
  23. $this->assertEquals('bar', $cache->get('foo'));
  24. $cache->delete('foo');
  25. $this->assertNull($cache->get('foo'));
  26. }
  27. /**
  28. * @expectedException \Psr\SimpleCache\InvalidArgumentException
  29. */
  30. function testGetInvalidArg() {
  31. $cache = $this->getCache();
  32. $cache->get(null);
  33. }
  34. /**
  35. * @depends testDelete
  36. */
  37. function testGetNotFound() {
  38. $cache = $this->getCache();
  39. $this->assertNull($cache->get('notfound'));
  40. }
  41. /**
  42. * @depends testDelete
  43. */
  44. function testGetNotFoundDefault() {
  45. $cache = $this->getCache();
  46. $default = 'chickpeas';
  47. $this->assertEquals(
  48. $default,
  49. $cache->get('notfound', $default)
  50. );
  51. }
  52. /**
  53. * @depends testSetGet
  54. * @slow
  55. */
  56. function testSetExpire() {
  57. $cache = $this->getCache();
  58. $cache->set('foo', 'bar', 1);
  59. $this->assertEquals('bar', $cache->get('foo'));
  60. // Wait 2 seconds so the cache expires
  61. usleep(2000000);
  62. $this->assertNull($cache->get('foo'));
  63. }
  64. /**
  65. * @depends testSetGet
  66. * @slow
  67. */
  68. function testSetExpireDateInterval() {
  69. $cache = $this->getCache();
  70. $cache->set('foo', 'bar', new \DateInterval('PT1S'));
  71. $this->assertEquals('bar', $cache->get('foo'));
  72. // Wait 2 seconds so the cache expires
  73. usleep(2000000);
  74. $this->assertNull($cache->get('foo'));
  75. }
  76. /**
  77. * @expectedException \Psr\SimpleCache\InvalidArgumentException
  78. */
  79. function testSetInvalidArg() {
  80. $cache = $this->getCache();
  81. $cache->set(null, 'bar');
  82. }
  83. /**
  84. * @expectedException \Psr\SimpleCache\InvalidArgumentException
  85. */
  86. function testDeleteInvalidArg() {
  87. $cache = $this->getCache();
  88. $cache->delete(null);
  89. }
  90. /**
  91. * @depends testSetGet
  92. */
  93. function testClearCache() {
  94. $cache = $this->getCache();
  95. $cache->set('foo', 'bar');
  96. $cache->clear();
  97. $this->assertNull($cache->get('foo'));
  98. }
  99. /**
  100. * @depends testSetGet
  101. */
  102. function testHas() {
  103. $cache = $this->getCache();
  104. $cache->set('foo', 'bar');
  105. $this->assertTrue($cache->has('foo'));
  106. }
  107. /**
  108. * @depends testHas
  109. */
  110. function testHasNot() {
  111. $cache = $this->getCache();
  112. $this->assertFalse($cache->has('not-found'));
  113. }
  114. /**
  115. * @expectedException \Psr\SimpleCache\InvalidArgumentException
  116. */
  117. function testHasInvalidArg() {
  118. $cache = $this->getCache();
  119. $cache->has(null);
  120. }
  121. /**
  122. * @depends testSetGet
  123. */
  124. function testSetGetMultiple() {
  125. $values = [
  126. 'key1' => 'value1',
  127. 'key2' => 'value2',
  128. 'key3' => 'value3',
  129. ];
  130. $cache = $this->getCache();
  131. $cache->setMultiple($values);
  132. $result = $cache->getMultiple(array_keys($values));
  133. foreach ($result as $key => $value) {
  134. $this->assertTrue(isset($values[$key]));
  135. $this->assertEquals($values[$key], $value);
  136. unset($values[$key]);
  137. }
  138. // The list of values should now be empty
  139. $this->assertEquals([], $values);
  140. }
  141. /**
  142. * @depends testSetGet
  143. */
  144. function testSetGetMultipleGenerator() {
  145. $values = [
  146. 'key1' => 'value1',
  147. 'key2' => 'value2',
  148. 'key3' => 'value3',
  149. ];
  150. $gen = function() use ($values) {
  151. foreach ($values as $key => $value) {
  152. yield $key => $value;
  153. }
  154. };
  155. $cache = $this->getCache();
  156. $cache->setMultiple($gen());
  157. $result = $cache->getMultiple(array_keys($values));
  158. foreach ($result as $key => $value) {
  159. $this->assertTrue(isset($values[$key]));
  160. $this->assertEquals($values[$key], $value);
  161. unset($values[$key]);
  162. }
  163. // The list of values should now be empty
  164. $this->assertEquals([], $values);
  165. }
  166. /**
  167. * @depends testSetGet
  168. */
  169. function testSetGetMultipleGenerator2() {
  170. $values = [
  171. 'key1' => 'value1',
  172. 'key2' => 'value2',
  173. 'key3' => 'value3',
  174. ];
  175. $gen = function() use ($values) {
  176. foreach ($values as $key => $value) {
  177. yield $key;
  178. }
  179. };
  180. $cache = $this->getCache();
  181. $cache->setMultiple($values);
  182. $result = $cache->getMultiple($gen());
  183. foreach ($result as $key => $value) {
  184. $this->assertTrue(isset($values[$key]));
  185. $this->assertEquals($values[$key], $value);
  186. unset($values[$key]);
  187. }
  188. // The list of values should now be empty
  189. $this->assertEquals([], $values);
  190. }
  191. /**
  192. * @depends testSetGetMultiple
  193. * @depends testSetExpire
  194. * @slow
  195. */
  196. function testSetMultipleExpireDateIntervalNotExpired() {
  197. $values = [
  198. 'key1' => 'value1',
  199. 'key2' => 'value2',
  200. 'key3' => 'value3',
  201. ];
  202. $cache = $this->getCache();
  203. $cache->setMultiple($values, new \DateInterval('PT5S'));
  204. $result = $cache->getMultiple(array_keys($values));
  205. $count = 0;
  206. foreach ($result as $key => $value) {
  207. $count++;
  208. $this->assertTrue(isset($values[$key]));
  209. $this->assertEquals($values[$key], $value);
  210. unset($values[$key]);
  211. }
  212. $this->assertEquals(3, $count);
  213. // The list of values should now be empty
  214. $this->assertEquals([], $values);
  215. }
  216. /**
  217. * @slow
  218. */
  219. function testSetMultipleExpireDateIntervalExpired() {
  220. $values = [
  221. 'key1' => 'value1',
  222. 'key2' => 'value2',
  223. 'key3' => 'value3',
  224. ];
  225. $cache = $this->getCache();
  226. $cache->setMultiple($values, new \DateInterval('PT1S'));
  227. // Wait 2 seconds so the cache expires
  228. sleep(2);
  229. $result = $cache->getMultiple(array_keys($values), 'not-found');
  230. $count = 0;
  231. $expected = [
  232. 'key1' => 'not-found',
  233. 'key2' => 'not-found',
  234. 'key3' => 'not-found',
  235. ];
  236. foreach ($result as $key => $value) {
  237. $count++;
  238. $this->assertTrue(isset($expected[$key]));
  239. $this->assertEquals($expected[$key], $value);
  240. unset($expected[$key]);
  241. }
  242. $this->assertEquals(3, $count);
  243. // The list of values should now be empty
  244. $this->assertEquals([], $expected);
  245. }
  246. /**
  247. * @slow
  248. */
  249. function testSetMultipleExpireDateIntervalInt() {
  250. $values = [
  251. 'key1' => 'value1',
  252. 'key2' => 'value2',
  253. 'key3' => 'value3',
  254. ];
  255. $cache = $this->getCache();
  256. $cache->setMultiple($values, 1);
  257. // Wait 2 seconds so the cache expires
  258. sleep(2);
  259. $result = $cache->getMultiple(array_keys($values), 'not-found');
  260. $count = 0;
  261. $expected = [
  262. 'key1' => 'not-found',
  263. 'key2' => 'not-found',
  264. 'key3' => 'not-found',
  265. ];
  266. foreach ($result as $key => $value) {
  267. $count++;
  268. $this->assertTrue(isset($expected[$key]));
  269. $this->assertEquals($expected[$key], $value);
  270. unset($expected[$key]);
  271. }
  272. $this->assertEquals(3, $count);
  273. // The list of values should now be empty
  274. $this->assertEquals([], $expected);
  275. }
  276. /**
  277. * @expectedException \Psr\SimpleCache\InvalidArgumentException
  278. */
  279. function testSetMultipleInvalidArg() {
  280. $cache = $this->getCache();
  281. $cache->setMultiple(null);
  282. }
  283. /**
  284. * @expectedException \Psr\SimpleCache\InvalidArgumentException
  285. */
  286. function testGetMultipleInvalidArg() {
  287. $cache = $this->getCache();
  288. $result = $cache->getMultiple(null);
  289. // If $result was a generator, the generator will only error once the
  290. // first value is requested.
  291. //
  292. // This extra line is just a precaution for that
  293. if ($result instanceof \Traversable) $result->current();
  294. }
  295. /**
  296. * @depends testSetGetMultiple
  297. */
  298. function testDeleteMultipleDefaultGet() {
  299. $values = [
  300. 'key1' => 'value1',
  301. 'key2' => 'value2',
  302. 'key3' => 'value3',
  303. ];
  304. $cache = $this->getCache();
  305. $cache->setMultiple($values);
  306. $cache->deleteMultiple(['key1', 'key3']);
  307. $result = $cache->getMultiple(array_keys($values), 'tea');
  308. $expected = [
  309. 'key1' => 'tea',
  310. 'key2' => 'value2',
  311. 'key3' => 'tea',
  312. ];
  313. foreach ($result as $key => $value) {
  314. $this->assertTrue(isset($expected[$key]));
  315. $this->assertEquals($expected[$key], $value);
  316. unset($expected[$key]);
  317. }
  318. // The list of values should now be empty
  319. $this->assertEquals([], $expected);
  320. }
  321. /**
  322. * @depends testSetGetMultiple
  323. */
  324. function testDeleteMultipleGenerator() {
  325. $values = [
  326. 'key1' => 'value1',
  327. 'key2' => 'value2',
  328. 'key3' => 'value3',
  329. ];
  330. $cache = $this->getCache();
  331. $cache->setMultiple($values);
  332. $gen = function() {
  333. yield 'key1';
  334. yield 'key3';
  335. };
  336. $cache->deleteMultiple($gen());
  337. $result = $cache->getMultiple(array_keys($values), 'tea');
  338. $expected = [
  339. 'key1' => 'tea',
  340. 'key2' => 'value2',
  341. 'key3' => 'tea',
  342. ];
  343. foreach ($result as $key => $value) {
  344. $this->assertTrue(isset($expected[$key]));
  345. $this->assertEquals($expected[$key], $value);
  346. unset($expected[$key]);
  347. }
  348. // The list of values should now be empty
  349. $this->assertEquals([], $expected);
  350. }
  351. /**
  352. * @expectedException \Psr\SimpleCache\InvalidArgumentException
  353. */
  354. function testDeleteMultipleInvalidArg() {
  355. $cache = $this->getCache();
  356. $cache->deleteMultiple(null);
  357. }
  358. }