/core/modules/system/src/Tests/Cache/ApcuBackendUnitTest.php

https://gitlab.com/geeta7/drupal · PHP · 212 lines · 121 code · 24 blank · 67 comment · 20 complexity · 571af2c05aef6a3425baa7e09e9107e0 MD5 · raw file

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