Passed
Push — master ( 122983...91227d )
by William
02:34 queued 26s
created

ApcuCacheTest::testGetReloadsOnCacheMiss()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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