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