installation de motranslator

via Composer
This commit is contained in:
2024-12-13 06:44:07 +01:00
parent 3ee1a9eaf6
commit db9f05f6f6
249 changed files with 29788 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\MoTranslator\Tests\Cache;
use PhpMyAdmin\MoTranslator\Cache\ApcuCache;
use PhpMyAdmin\MoTranslator\Cache\ApcuCacheFactory;
use PhpMyAdmin\MoTranslator\MoParser;
use PHPUnit\Framework\TestCase;
use function apcu_clear_cache;
use function apcu_delete;
use function apcu_enabled;
use function apcu_fetch;
use function function_exists;
use function sleep;
/**
* @covers \PhpMyAdmin\MoTranslator\Cache\ApcuCacheFactory
*/
class ApcuCacheFactoryTest 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 testGetInstanceReturnApcuCache(): void
{
$factory = new ApcuCacheFactory();
$instance = $factory->getInstance(new MoParser(null), 'foo', 'bar');
$this->assertInstanceOf(ApcuCache::class, $instance);
}
public function testConstructorSetsTtl(): void
{
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$ttl = 1;
$factory = new ApcuCacheFactory($ttl);
$parser = new MoParser(__DIR__ . '/../data/little.mo');
$factory->getInstance($parser, $locale, $domain);
sleep($ttl * 2);
apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success);
$this->assertFalse($success);
}
public function testConstructorSetsReloadOnMiss(): void
{
$expected = 'Column';
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$factory = new ApcuCacheFactory(0, false);
$parser = new MoParser(__DIR__ . '/../data/little.mo');
$instance = $factory->getInstance($parser, $locale, $domain);
apcu_delete('mo_' . $locale . '.' . $domain . '.' . $msgid);
$actual = $instance->get($msgid);
$this->assertSame($expected, $actual);
}
public function testConstructorSetsPrefix(): void
{
$expected = 'Pole';
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$prefix = 'baz_';
$factory = new ApcuCacheFactory(0, true, $prefix);
$parser = new MoParser(__DIR__ . '/../data/little.mo');
$factory->getInstance($parser, $locale, $domain);
$actual = apcu_fetch($prefix . $locale . '.' . $domain . '.' . $msgid);
$this->assertSame($expected, $actual);
}
}

View File

@@ -0,0 +1,270 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\MoTranslator\Tests\Cache;
use PhpMyAdmin\MoTranslator\Cache\ApcuCache;
use PhpMyAdmin\MoTranslator\MoParser;
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;
/**
* @covers \PhpMyAdmin\MoTranslator\Cache\ApcuCache
*/
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);
$this->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);
$this->assertFalse($success);
apcu_fetch('mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY, $success);
$this->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);
$this->assertEquals($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);
$this->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);
$this->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);
$this->assertSame(1, $actual);
apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success);
$this->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);
$this->assertSame($expected, $actual);
}
public function testGetReturnsMsgidForCacheMiss(): void
{
$expected = 'Column';
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
$actual = $cache->get($expected);
$this->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);
$this->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);
$this->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);
$this->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);
$this->assertSame($expected, $actual);
}
public function testHasReturnsFalse(): void
{
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
$actual = $cache->has('Column');
$this->assertFalse($actual);
}
public function testHasReturnsTrue(): void
{
$cache = new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), 'foo', 'bar');
$actual = $cache->has('Column');
$this->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);
$this->assertEquals($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);
$this->assertSame($expected, $actual);
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\MoTranslator\Tests\Cache;
use PhpMyAdmin\MoTranslator\Cache\ApcuCache;
use PhpMyAdmin\MoTranslator\CacheException;
use PhpMyAdmin\MoTranslator\MoParser;
use PHPUnit\Framework\TestCase;
use function apcu_enabled;
use function function_exists;
final class ApcuDisabledTest extends TestCase
{
public function testConstructorApcuNotEnabledThrowsException(): void
{
if (function_exists('apcu_enabled') && apcu_enabled()) {
$this->markTestSkipped('ext-apcu is enabled');
}
$this->expectException(CacheException::class);
$this->expectExceptionMessage('ACPu extension must be installed and enabled');
new ApcuCache(new MoParser(null), 'foo', 'bar');
}
}

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\MoTranslator\Tests\Cache;
use PhpMyAdmin\MoTranslator\Cache\InMemoryCache;
use PhpMyAdmin\MoTranslator\MoParser;
use PHPUnit\Framework\TestCase;
/**
* @covers \PhpMyAdmin\MoTranslator\Cache\InMemoryCache
*/
class InMemoryCacheTest extends TestCase
{
public function testConstructorParsesCache(): void
{
$expected = 'Pole';
$parser = new MoParser(__DIR__ . '/../data/little.mo');
$cache = new InMemoryCache($parser);
$actual = $cache->get('Column');
$this->assertSame($expected, $actual);
}
public function testGetReturnsMsgidForCacheMiss(): void
{
$expected = 'Column';
$cache = new InMemoryCache(new MoParser(null));
$actual = $cache->get($expected);
$this->assertSame($expected, $actual);
}
public function testSetSetsMsgstr(): void
{
$expected = 'Pole';
$msgid = 'Column';
$cache = new InMemoryCache(new MoParser(null));
$cache->set($msgid, $expected);
$actual = $cache->get($msgid);
$this->assertSame($expected, $actual);
}
public function testHasReturnsFalse(): void
{
$cache = new InMemoryCache(new MoParser(null));
$actual = $cache->has('Column');
$this->assertFalse($actual);
}
public function testHasReturnsTrue(): void
{
$cache = new InMemoryCache(new MoParser(__DIR__ . '/../data/little.mo'));
$actual = $cache->has('Column');
$this->assertTrue($actual);
}
public function testSetAllSetsTranslations(): void
{
$translations = [
'foo' => 'bar',
'and' => 'another',
];
$cache = new InMemoryCache(new MoParser(null));
$cache->setAll($translations);
foreach ($translations as $msgid => $expected) {
$actual = $cache->get($msgid);
$this->assertEquals($expected, $actual);
}
}
public function testGetAllReturnsTranslations(): void
{
$expected = [
'foo' => 'bar',
'and' => 'another',
];
$cache = new InMemoryCache(new MoParser(null));
$cache->setAll($expected);
$actual = $cache->getAll();
$this->assertSame($expected, $actual);
}
}