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