ApcuBackendTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Drupal\KernelTests\Core\Cache;
  3. use Drupal\Core\Cache\Apcu4Backend;
  4. use Drupal\Core\Cache\ApcuBackend;
  5. /**
  6. * Tests the APCu cache backend.
  7. *
  8. * @group Cache
  9. * @requires extension apcu
  10. */
  11. class ApcuBackendTest extends GenericCacheBackendUnitTestBase {
  12. /**
  13. * Get a list of failed requirements.
  14. *
  15. * This specifically bypasses checkRequirements because it fails tests. PHP 7
  16. * does not have APCu and simpletest does not have a explicit "skip"
  17. * functionality so to emulate it we override all test methods and explicitly
  18. * pass when requirements are not met.
  19. *
  20. * @return array
  21. */
  22. protected function getRequirements() {
  23. $requirements = [];
  24. if (!extension_loaded('apcu')) {
  25. $requirements[] = 'APCu extension not found.';
  26. }
  27. else {
  28. if (PHP_SAPI === 'cli' && !ini_get('apc.enable_cli')) {
  29. $requirements[] = 'apc.enable_cli must be enabled to run this test.';
  30. }
  31. }
  32. return $requirements;
  33. }
  34. /**
  35. * Check if requirements fail.
  36. *
  37. * If the requirements fail the test method should return immediately instead
  38. * of running any tests. Messages will be output to display why the test was
  39. * skipped.
  40. */
  41. protected function requirementsFail() {
  42. $requirements = $this->getRequirements();
  43. if (!empty($requirements)) {
  44. foreach ($requirements as $message) {
  45. $this->pass($message);
  46. }
  47. return TRUE;
  48. }
  49. return FALSE;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function createCacheBackend($bin) {
  55. if (version_compare(phpversion('apcu'), '5.0.0', '>=')) {
  56. return new ApcuBackend($bin, $this->databasePrefix, \Drupal::service('cache_tags.invalidator.checksum'));
  57. }
  58. else {
  59. return new Apcu4Backend($bin, $this->databasePrefix, \Drupal::service('cache_tags.invalidator.checksum'));
  60. }
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function tearDown() {
  66. foreach ($this->cachebackends as $bin => $cachebackend) {
  67. $this->cachebackends[$bin]->removeBin();
  68. }
  69. parent::tearDown();
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function testSetGet() {
  75. if ($this->requirementsFail()) {
  76. return;
  77. }
  78. parent::testSetGet();
  79. // Make sure entries are permanent (i.e. no TTL).
  80. $backend = $this->getCacheBackend($this->getTestBin());
  81. $key = $backend->getApcuKey('TEST8');
  82. if (class_exists('\APCUIterator')) {
  83. $iterator = new \APCUIterator('/^' . $key . '/');
  84. }
  85. else {
  86. $iterator = new \APCIterator('user', '/^' . $key . '/');
  87. }
  88. foreach ($iterator as $item) {
  89. $this->assertEqual(0, $item['ttl']);
  90. $found = TRUE;
  91. }
  92. $this->assertTrue($found);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function testDelete() {
  98. if ($this->requirementsFail()) {
  99. return;
  100. }
  101. parent::testDelete();
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function testValueTypeIsKept() {
  107. if ($this->requirementsFail()) {
  108. return;
  109. }
  110. parent::testValueTypeIsKept();
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function testGetMultiple() {
  116. if ($this->requirementsFail()) {
  117. return;
  118. }
  119. parent::testGetMultiple();
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function testSetMultiple() {
  125. if ($this->requirementsFail()) {
  126. return;
  127. }
  128. parent::testSetMultiple();
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function testDeleteMultiple() {
  134. if ($this->requirementsFail()) {
  135. return;
  136. }
  137. parent::testDeleteMultiple();
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function testDeleteAll() {
  143. if ($this->requirementsFail()) {
  144. return;
  145. }
  146. parent::testDeleteAll();
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function testInvalidate() {
  152. if ($this->requirementsFail()) {
  153. return;
  154. }
  155. parent::testInvalidate();
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function testInvalidateTags() {
  161. if ($this->requirementsFail()) {
  162. return;
  163. }
  164. parent::testInvalidateTags();
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function testInvalidateAll() {
  170. if ($this->requirementsFail()) {
  171. return;
  172. }
  173. parent::testInvalidateAll();
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function testRemoveBin() {
  179. if ($this->requirementsFail()) {
  180. return;
  181. }
  182. parent::testRemoveBin();
  183. }
  184. }