ApcuTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php declare (strict_types=1);
  2. namespace Sabre\Cache;
  3. use Psr\SimpleCache\CacheInterface;
  4. use Traversable;
  5. class ApcuTest extends AbstractCacheTest {
  6. function getCache() : CacheInterface {
  7. if (!function_exists('apcu_store')) {
  8. $this->markTestSkipped('Apcu extension is not loaded');
  9. }
  10. if (!ini_get('apc.enabled')) {
  11. $this->markTestSkipped('apc.enabled is set to 0. Enable it via php.ini');
  12. }
  13. if (php_sapi_name() === 'cli' && !ini_get('apc.enable_cli')) {
  14. $this->markTestSkipped('apc.enable_cli is set to 0. Enable it via php.ini');
  15. }
  16. return new Apcu();
  17. }
  18. /**
  19. * APC will only remove expired items from the cache during the next test,
  20. * so we can't fully test these.
  21. *
  22. * Instead, we test if the parameter is set but then don't check for a
  23. * result.
  24. *
  25. * So this test is not complete, but that's the best we can do.
  26. */
  27. function testSetExpire() {
  28. $cache = $this->getCache();
  29. $cache->set('foo', 'bar', 1);
  30. $this->assertEquals('bar', $cache->get('foo'));
  31. // Wait 2 seconds so the cache expires
  32. // usleep(2000000);
  33. // $this->assertNull($cache->get('foo'));
  34. }
  35. /**
  36. * APC will only remove expired items from the cache during the next test,
  37. * so we can't fully test these.
  38. *
  39. * Instead, we test if the parameter is set but then don't check for a
  40. * result.
  41. *
  42. * So this test is not complete, but that's the best we can do.
  43. */
  44. function testSetExpireDateInterval() {
  45. $cache = $this->getCache();
  46. $cache->set('foo', 'bar', new \DateInterval('PT1S'));
  47. $this->assertEquals('bar', $cache->get('foo'));
  48. // Wait 2 seconds so the cache expires
  49. // usleep(2000000);
  50. // $this->assertNull($cache->get('foo'));
  51. }
  52. /**
  53. * APC will only remove expired items from the cache during the next test,
  54. * so we can't fully test these.
  55. *
  56. * Instead, we test if the parameter is set but then don't check for a
  57. * result.
  58. *
  59. * So this test is not complete, but that's the best we can do.
  60. */
  61. function testSetMultipleExpireDateIntervalExpired() {
  62. $values = [
  63. 'key1' => 'value1',
  64. 'key2' => 'value2',
  65. 'key3' => 'value3',
  66. ];
  67. $cache = $this->getCache();
  68. $cache->setMultiple($values, new \DateInterval('PT1S'));
  69. //// Wait 2 seconds so the cache expires
  70. //sleep(2);
  71. $result = $cache->getMultiple(array_keys($values), 'not-found');
  72. $this->assertTrue($result instanceof Traversable || is_array($result));
  73. //$count = 0;
  74. //$expected = [
  75. // 'key1' => 'not-found',
  76. // 'key2' => 'not-found',
  77. // 'key3' => 'not-found',
  78. //];
  79. //foreach ($result as $key => $value) {
  80. // $count++;
  81. // $this->assertTrue(isset($expected[$key]));
  82. // $this->assertEquals($expected[$key], $value);
  83. // unset($expected[$key]);
  84. //}
  85. //$this->assertEquals(3, $count);
  86. //// The list of values should now be empty
  87. //$this->assertEquals([], $expected);
  88. }
  89. /**
  90. * APC will only remove expired items from the cache during the next test,
  91. * so we can't fully test these.
  92. *
  93. * Instead, we test if the parameter is set but then don't check for a
  94. * result.
  95. *
  96. * So this test is not complete, but that's the best we can do.
  97. */
  98. function testSetMultipleExpireDateIntervalInt() {
  99. $values = [
  100. 'key1' => 'value1',
  101. 'key2' => 'value2',
  102. 'key3' => 'value3',
  103. ];
  104. $cache = $this->getCache();
  105. $cache->setMultiple($values, 1);
  106. // Wait 2 seconds so the cache expires
  107. //sleep(2);
  108. $result = $cache->getMultiple(array_keys($values), 'not-found');
  109. $this->assertTrue($result instanceof Traversable || is_array($result));
  110. //$count = 0;
  111. //$expected = [
  112. // 'key1' => 'not-found',
  113. // 'key2' => 'not-found',
  114. // 'key3' => 'not-found',
  115. //];
  116. //foreach ($result as $key => $value) {
  117. // $count++;
  118. // $this->assertTrue(isset($expected[$key]));
  119. // $this->assertEquals($expected[$key], $value);
  120. // unset($expected[$key]);
  121. //}
  122. //$this->assertEquals(3, $count);
  123. //// The list of values should now be empty
  124. //$this->assertEquals([], $expected);
  125. }
  126. }