Passed
Pull Request — master (#42)
by
unknown
09:29
created

ApcuCacheTest::testConstructorLoadsCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
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
11
use function apcu_clear_cache;
12
use function apcu_delete;
13
use function apcu_enabled;
14
use function apcu_fetch;
15
use function function_exists;
16
use function sleep;
17
18
/**
19
 * @covers \PhpMyAdmin\MoTranslator\Cache\ApcuCache
20
 */
21
class ApcuCacheTest extends TestCase
22
{
23
    protected function setUp(): void
24
    {
25
        parent::setUp();
26
27
        if (function_exists('apcu_enabled') && apcu_enabled()) {
28
            return;
29
        }
30
31
        $this->markTestSkipped('ACPu extension is not installed and enabled for CLI');
32
    }
33
34
    protected function tearDown(): void
35
    {
36
        parent::tearDown();
37
38
        apcu_clear_cache();
39
    }
40
41
    public function testConstructorLoadsCache(): void
42
    {
43
        $expected = 'Pole';
44
        $locale = 'foo';
45
        $domain = 'bar';
46
        $msgid = 'Column';
47
48
        new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain);
49
50
        $actual = apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid);
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 ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain, $ttl);
62
        sleep($ttl * 2);
63
64
        apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success);
65
        $this->assertFalse($success);
66
    }
67
68
    public function testConstructorSetsReloadOnMiss(): void
69
    {
70
        $expected = 'Column';
71
        $locale = 'foo';
72
        $domain = 'bar';
73
        $msgid = 'Column';
74
        $prefix = 'baz_';
75
76
        $cache = new ApcuCache(
77
            new MoParser(__DIR__ . '/../data/little.mo'),
78
            $locale,
79
            $domain,
80
            0,
81
            false,
82
            $prefix
83
        );
84
85
        apcu_delete($prefix . $locale . '.' . $domain . '.' . $msgid);
86
        $actual = $cache->get($msgid);
87
        $this->assertEquals($expected, $actual);
88
    }
89
90
    public function testConstructorSetsPrefix(): void
91
    {
92
        $expected = 'Pole';
93
        $locale = 'foo';
94
        $domain = 'bar';
95
        $msgid = 'Column';
96
        $prefix = 'baz_';
97
98
        new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain, 0, true, $prefix);
99
100
        $actual = apcu_fetch($prefix . $locale . '.' . $domain . '.' . $msgid);
101
        $this->assertSame($expected, $actual);
102
    }
103
104
    public function testEnsureTranslationsLoadedSetsLoadedKey(): void
105
    {
106
        $expected = 1;
107
        $locale = 'foo';
108
        $domain = 'bar';
109
110
        new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain);
111
112
        $actual = apcu_fetch('mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY);
113
        $this->assertSame($expected, $actual);
114
    }
115
116
    public function testGetReturnsMsgstr(): void
117
    {
118
        $expected = 'Pole';
119
        $msgid = 'Column';
120
121
        $cache = new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), 'foo', 'bar');
122
123
        $actual = $cache->get($msgid);
124
        $this->assertSame($expected, $actual);
125
    }
126
127
    public function testGetReturnsMsgidForCacheMiss(): void
128
    {
129
        $expected = 'Column';
130
131
        $cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
132
133
        $actual = $cache->get($expected);
134
        $this->assertSame($expected, $actual);
135
    }
136
137
    public function testGetReloadsOnCacheMiss(): void
138
    {
139
        $expected = 'Pole';
140
        $locale = 'foo';
141
        $domain = 'bar';
142
        $msgid = 'Column';
143
144
        $cache = new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), 'foo', 'bar');
145
146
        apcu_delete('mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY);
147
        $actual = $cache->get($msgid);
148
        $this->assertSame($expected, $actual);
149
    }
150
151
    public function testSetSetsMsgstr(): void
152
    {
153
        $expected = 'Pole';
154
        $msgid = 'Column';
155
156
        $cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
157
        $cache->set($msgid, $expected);
158
159
        $actual = $cache->get($msgid);
160
        $this->assertSame($expected, $actual);
161
    }
162
163
    public function testHasReturnsFalse(): void
164
    {
165
        $cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
166
        $actual = $cache->has('Column');
167
        $this->assertFalse($actual);
168
    }
169
170
    public function testHasReturnsTrue(): void
171
    {
172
        $cache = new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), 'foo', 'bar');
173
        $actual = $cache->has('Column');
174
        $this->assertTrue($actual);
175
    }
176
177
    public function testSetAllSetsTranslations(): void
178
    {
179
        $translations = [
180
            'foo' => 'bar',
181
            'and' => 'another',
182
        ];
183
184
        $cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
185
        $cache->setAll($translations);
186
187
        foreach ($translations as $msgid => $expected) {
188
            $actual = $cache->get($msgid);
189
            $this->assertEquals($expected, $actual);
190
        }
191
    }
192
}
193