3 namespace Drupal\KernelTests\Core\Cache;
5 use Drupal\Core\Cache\Apcu4Backend;
6 use Drupal\Core\Cache\ApcuBackend;
9 * Tests the APCu cache backend.
12 * @requires extension apcu
14 class ApcuBackendTest extends GenericCacheBackendUnitTestBase {
17 * Get a list of failed requirements.
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.
26 protected function getRequirements() {
28 if (!extension_loaded('apcu')) {
29 $requirements[] = 'APCu extension not found.';
32 if (PHP_SAPI === 'cli' && !ini_get('apc.enable_cli')) {
33 $requirements[] = 'apc.enable_cli must be enabled to run this test.';
40 * Check if requirements fail.
42 * If the requirements fail the test method should return immediately instead
43 * of running any tests. Messages will be output to display why the test was
46 protected function requirementsFail() {
47 $requirements = $this->getRequirements();
48 if (!empty($requirements)) {
49 foreach ($requirements as $message) {
50 $this->pass($message);
61 protected function createCacheBackend($bin) {
62 if (version_compare(phpversion('apcu'), '5.0.0', '>=')) {
63 return new ApcuBackend($bin, $this->databasePrefix, \Drupal::service('cache_tags.invalidator.checksum'));
66 return new Apcu4Backend($bin, $this->databasePrefix, \Drupal::service('cache_tags.invalidator.checksum'));
73 protected function tearDown() {
74 foreach ($this->cachebackends as $bin => $cachebackend) {
75 $this->cachebackends[$bin]->removeBin();
83 public function testSetGet() {
84 if ($this->requirementsFail()) {
89 // Make sure entries are permanent (i.e. no TTL).
90 $backend = $this->getCacheBackend($this->getTestBin());
91 $key = $backend->getApcuKey('TEST8');
93 if (class_exists('\APCUIterator')) {
94 $iterator = new \APCUIterator('/^' . $key . '/');
97 $iterator = new \APCIterator('user', '/^' . $key . '/');
100 foreach ($iterator as $item) {
101 $this->assertEqual(0, $item['ttl']);
104 $this->assertTrue($found);
110 public function testDelete() {
111 if ($this->requirementsFail()) {
114 parent::testDelete();
120 public function testValueTypeIsKept() {
121 if ($this->requirementsFail()) {
124 parent::testValueTypeIsKept();
130 public function testGetMultiple() {
131 if ($this->requirementsFail()) {
134 parent::testGetMultiple();
140 public function testSetMultiple() {
141 if ($this->requirementsFail()) {
144 parent::testSetMultiple();
150 public function testDeleteMultiple() {
151 if ($this->requirementsFail()) {
154 parent::testDeleteMultiple();
160 public function testDeleteAll() {
161 if ($this->requirementsFail()) {
164 parent::testDeleteAll();
170 public function testInvalidate() {
171 if ($this->requirementsFail()) {
174 parent::testInvalidate();
180 public function testInvalidateTags() {
181 if ($this->requirementsFail()) {
184 parent::testInvalidateTags();
190 public function testInvalidateAll() {
191 if ($this->requirementsFail()) {
194 parent::testInvalidateAll();
200 public function testRemoveBin() {
201 if ($this->requirementsFail()) {
204 parent::testRemoveBin();