Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Cache / ApcuBackendTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Cache;
4
5 use Drupal\Core\Cache\Apcu4Backend;
6 use Drupal\Core\Cache\ApcuBackend;
7
8 /**
9  * Tests the APCu cache backend.
10  *
11  * @group Cache
12  * @requires extension apcu
13  */
14 class ApcuBackendTest extends GenericCacheBackendUnitTestBase {
15
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   /**
40    * Check if requirements fail.
41    *
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
44    * skipped.
45    */
46   protected function requirementsFail() {
47     $requirements = $this->getRequirements();
48     if (!empty($requirements)) {
49       foreach ($requirements as $message) {
50         $this->pass($message);
51       }
52       return TRUE;
53     }
54
55     return FALSE;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
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'));
64     }
65     else {
66       return new Apcu4Backend($bin, $this->databasePrefix, \Drupal::service('cache_tags.invalidator.checksum'));
67     }
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   protected function tearDown() {
74     foreach ($this->cachebackends as $bin => $cachebackend) {
75       $this->cachebackends[$bin]->removeBin();
76     }
77     parent::tearDown();
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function testSetGet() {
84     if ($this->requirementsFail()) {
85       return;
86     }
87     parent::testSetGet();
88
89     // Make sure entries are permanent (i.e. no TTL).
90     $backend = $this->getCacheBackend($this->getTestBin());
91     $key = $backend->getApcuKey('TEST8');
92
93     if (class_exists('\APCUIterator')) {
94       $iterator = new \APCUIterator('/^' . $key . '/');
95     }
96     else {
97       $iterator = new \APCIterator('user', '/^' . $key . '/');
98     }
99
100     foreach ($iterator as $item) {
101       $this->assertEqual(0, $item['ttl']);
102       $found = TRUE;
103     }
104     $this->assertTrue($found);
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function testDelete() {
111     if ($this->requirementsFail()) {
112       return;
113     }
114     parent::testDelete();
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   public function testValueTypeIsKept() {
121     if ($this->requirementsFail()) {
122       return;
123     }
124     parent::testValueTypeIsKept();
125   }
126
127   /**
128    * {@inheritdoc}
129    */
130   public function testGetMultiple() {
131     if ($this->requirementsFail()) {
132       return;
133     }
134     parent::testGetMultiple();
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function testSetMultiple() {
141     if ($this->requirementsFail()) {
142       return;
143     }
144     parent::testSetMultiple();
145   }
146
147   /**
148    * {@inheritdoc}
149    */
150   public function testDeleteMultiple() {
151     if ($this->requirementsFail()) {
152       return;
153     }
154     parent::testDeleteMultiple();
155   }
156
157   /**
158    * {@inheritdoc}
159    */
160   public function testDeleteAll() {
161     if ($this->requirementsFail()) {
162       return;
163     }
164     parent::testDeleteAll();
165   }
166
167   /**
168    * {@inheritdoc}
169    */
170   public function testInvalidate() {
171     if ($this->requirementsFail()) {
172       return;
173     }
174     parent::testInvalidate();
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   public function testInvalidateTags() {
181     if ($this->requirementsFail()) {
182       return;
183     }
184     parent::testInvalidateTags();
185   }
186
187   /**
188    * {@inheritdoc}
189    */
190   public function testInvalidateAll() {
191     if ($this->requirementsFail()) {
192       return;
193     }
194     parent::testInvalidateAll();
195   }
196
197   /**
198    * {@inheritdoc}
199    */
200   public function testRemoveBin() {
201     if ($this->requirementsFail()) {
202       return;
203     }
204     parent::testRemoveBin();
205   }
206
207 }