1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\MoTranslator\Tests\Cache; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\MoTranslator\Cache\ApcuCache; |
8
|
|
|
use PhpMyAdmin\MoTranslator\MoParser; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use ReflectionMethod; |
11
|
|
|
|
12
|
|
|
use function apcu_clear_cache; |
13
|
|
|
use function apcu_delete; |
14
|
|
|
use function apcu_enabled; |
15
|
|
|
use function apcu_entry; |
16
|
|
|
use function apcu_fetch; |
17
|
|
|
use function chr; |
18
|
|
|
use function explode; |
19
|
|
|
use function function_exists; |
20
|
|
|
use function implode; |
21
|
|
|
use function sleep; |
22
|
|
|
|
23
|
|
|
/** @covers \PhpMyAdmin\MoTranslator\Cache\ApcuCache */ |
24
|
|
|
class ApcuCacheTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
protected function setUp(): void |
27
|
|
|
{ |
28
|
|
|
parent::setUp(); |
29
|
|
|
|
30
|
|
|
if (function_exists('apcu_enabled') && apcu_enabled()) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->markTestSkipped('ACPu extension is not installed and enabled for CLI'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function tearDown(): void |
38
|
|
|
{ |
39
|
|
|
parent::tearDown(); |
40
|
|
|
|
41
|
|
|
apcu_clear_cache(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testConstructorLoadsCache(): void |
45
|
|
|
{ |
46
|
|
|
$expected = 'Pole'; |
47
|
|
|
$locale = 'foo'; |
48
|
|
|
$domain = 'bar'; |
49
|
|
|
$msgid = 'Column'; |
50
|
|
|
|
51
|
|
|
new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain); |
52
|
|
|
|
53
|
|
|
$actual = apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid); |
54
|
|
|
self::assertSame($expected, $actual); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testConstructorSetsTtl(): void |
58
|
|
|
{ |
59
|
|
|
$locale = 'foo'; |
60
|
|
|
$domain = 'bar'; |
61
|
|
|
$msgid = 'Column'; |
62
|
|
|
$ttl = 1; |
63
|
|
|
|
64
|
|
|
new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain, $ttl); |
65
|
|
|
sleep($ttl * 2); |
66
|
|
|
|
67
|
|
|
apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success); |
68
|
|
|
self::assertFalse($success); |
69
|
|
|
apcu_fetch('mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY, $success); |
70
|
|
|
self::assertFalse($success); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testConstructorSetsReloadOnMiss(): void |
74
|
|
|
{ |
75
|
|
|
$expected = 'Column'; |
76
|
|
|
$locale = 'foo'; |
77
|
|
|
$domain = 'bar'; |
78
|
|
|
$msgid = 'Column'; |
79
|
|
|
$prefix = 'baz_'; |
80
|
|
|
|
81
|
|
|
$cache = new ApcuCache( |
82
|
|
|
new MoParser(__DIR__ . '/../data/little.mo'), |
83
|
|
|
$locale, |
84
|
|
|
$domain, |
85
|
|
|
0, |
86
|
|
|
false, |
87
|
|
|
$prefix, |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
apcu_delete($prefix . $locale . '.' . $domain . '.' . $msgid); |
91
|
|
|
$actual = $cache->get($msgid); |
92
|
|
|
self::assertSame($expected, $actual); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testConstructorSetsPrefix(): void |
96
|
|
|
{ |
97
|
|
|
$expected = 'Pole'; |
98
|
|
|
$locale = 'foo'; |
99
|
|
|
$domain = 'bar'; |
100
|
|
|
$msgid = 'Column'; |
101
|
|
|
$prefix = 'baz_'; |
102
|
|
|
|
103
|
|
|
new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain, 0, true, $prefix); |
104
|
|
|
|
105
|
|
|
$actual = apcu_fetch($prefix . $locale . '.' . $domain . '.' . $msgid); |
106
|
|
|
self::assertSame($expected, $actual); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testEnsureTranslationsLoadedSetsLoadedKey(): void |
110
|
|
|
{ |
111
|
|
|
$expected = 1; |
112
|
|
|
$locale = 'foo'; |
113
|
|
|
$domain = 'bar'; |
114
|
|
|
|
115
|
|
|
new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain); |
116
|
|
|
|
117
|
|
|
$actual = apcu_fetch('mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY); |
118
|
|
|
self::assertSame($expected, $actual); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testEnsureTranslationsLoadedHonorsLock(): void |
122
|
|
|
{ |
123
|
|
|
$locale = 'foo'; |
124
|
|
|
$domain = 'bar'; |
125
|
|
|
$msgid = 'Column'; |
126
|
|
|
|
127
|
|
|
$lock = 'mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY; |
128
|
|
|
apcu_entry($lock, static function () { |
129
|
|
|
sleep(1); |
130
|
|
|
|
131
|
|
|
return 1; |
132
|
|
|
}); |
133
|
|
|
|
134
|
|
|
new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain); |
135
|
|
|
|
136
|
|
|
$actual = apcu_fetch($lock); |
137
|
|
|
self::assertSame(1, $actual); |
138
|
|
|
apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success); |
139
|
|
|
self::assertFalse($success); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testGetReturnsMsgstr(): void |
143
|
|
|
{ |
144
|
|
|
$expected = 'Pole'; |
145
|
|
|
$msgid = 'Column'; |
146
|
|
|
|
147
|
|
|
$cache = new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), 'foo', 'bar'); |
148
|
|
|
|
149
|
|
|
$actual = $cache->get($msgid); |
150
|
|
|
self::assertSame($expected, $actual); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testGetReturnsMsgidForCacheMiss(): void |
154
|
|
|
{ |
155
|
|
|
$expected = 'Column'; |
156
|
|
|
|
157
|
|
|
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar'); |
158
|
|
|
|
159
|
|
|
$actual = $cache->get($expected); |
160
|
|
|
self::assertSame($expected, $actual); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testStoresMsgidOnCacheMiss(): void |
164
|
|
|
{ |
165
|
|
|
$expected = 'Column'; |
166
|
|
|
$locale = 'foo'; |
167
|
|
|
$domain = 'bar'; |
168
|
|
|
|
169
|
|
|
$cache = new ApcuCache(new MoParser(null), $locale, $domain); |
170
|
|
|
$cache->get($expected); |
171
|
|
|
|
172
|
|
|
$actual = apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $expected); |
173
|
|
|
self::assertSame($expected, $actual); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testGetReloadsOnCacheMiss(): void |
177
|
|
|
{ |
178
|
|
|
$expected = 'Pole'; |
179
|
|
|
$locale = 'foo'; |
180
|
|
|
$domain = 'bar'; |
181
|
|
|
$msgid = 'Column'; |
182
|
|
|
|
183
|
|
|
$cache = new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain); |
184
|
|
|
|
185
|
|
|
apcu_delete('mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY); |
186
|
|
|
$actual = $cache->get($msgid); |
187
|
|
|
self::assertSame($expected, $actual); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testReloadOnMissHonorsLock(): void |
191
|
|
|
{ |
192
|
|
|
$expected = 'Pole'; |
193
|
|
|
$locale = 'foo'; |
194
|
|
|
$domain = 'bar'; |
195
|
|
|
$msgid = 'Column'; |
196
|
|
|
|
197
|
|
|
$cache = new ApcuCache(new MoParser(null), $locale, $domain); |
198
|
|
|
|
199
|
|
|
$method = new ReflectionMethod($cache, 'reloadOnMiss'); |
200
|
|
|
$method->setAccessible(true); |
201
|
|
|
|
202
|
|
|
$key = 'mo_' . $locale . '.' . $domain . '.' . $msgid; |
203
|
|
|
apcu_entry($key, static function () use ($expected): string { |
204
|
|
|
sleep(1); |
205
|
|
|
|
206
|
|
|
return $expected; |
207
|
|
|
}); |
208
|
|
|
$actual = $method->invoke($cache, $msgid); |
209
|
|
|
|
210
|
|
|
self::assertSame($expected, $actual); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testSetSetsMsgstr(): void |
214
|
|
|
{ |
215
|
|
|
$expected = 'Pole'; |
216
|
|
|
$msgid = 'Column'; |
217
|
|
|
|
218
|
|
|
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar'); |
219
|
|
|
$cache->set($msgid, $expected); |
220
|
|
|
|
221
|
|
|
$actual = $cache->get($msgid); |
222
|
|
|
self::assertSame($expected, $actual); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testHasReturnsFalse(): void |
226
|
|
|
{ |
227
|
|
|
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar'); |
228
|
|
|
$actual = $cache->has('Column'); |
229
|
|
|
self::assertFalse($actual); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testHasReturnsTrue(): void |
233
|
|
|
{ |
234
|
|
|
$cache = new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), 'foo', 'bar'); |
235
|
|
|
$actual = $cache->has('Column'); |
236
|
|
|
self::assertTrue($actual); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testSetAllSetsTranslations(): void |
240
|
|
|
{ |
241
|
|
|
$translations = [ |
242
|
|
|
'foo' => 'bar', |
243
|
|
|
'and' => 'another', |
244
|
|
|
]; |
245
|
|
|
|
246
|
|
|
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar'); |
247
|
|
|
$cache->setAll($translations); |
248
|
|
|
|
249
|
|
|
foreach ($translations as $msgid => $expected) { |
250
|
|
|
$actual = $cache->get($msgid); |
251
|
|
|
self::assertSame($expected, $actual); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function testCacheStoresPluralForms(): void |
256
|
|
|
{ |
257
|
|
|
$expected = ['first', 'second']; |
258
|
|
|
$plural = ["%d pig went to the market\n", "%d pigs went to the market\n"]; |
259
|
|
|
$msgid = implode(chr(0), $plural); |
260
|
|
|
|
261
|
|
|
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar'); |
262
|
|
|
$cache->set($msgid, implode(chr(0), $expected)); |
263
|
|
|
|
264
|
|
|
$msgstr = $cache->get($msgid); |
265
|
|
|
$actual = explode(chr(0), $msgstr); |
266
|
|
|
self::assertSame($expected, $actual); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|