91 lines
2.8 KiB
PHP
91 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Core;
|
|
|
|
use App\Core\I18n;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Guards against the most common i18n regression: a developer adds a key
|
|
* to one Locale file and forgets the other three. CI must fail loudly.
|
|
*/
|
|
final class LocaleConsistencyTest extends TestCase
|
|
{
|
|
/** @return array<string, array{string}> */
|
|
public static function localeProvider(): array
|
|
{
|
|
return [
|
|
'de' => ['de'],
|
|
'en' => ['en'],
|
|
'uk' => ['uk'],
|
|
'ru' => ['ru'],
|
|
];
|
|
}
|
|
|
|
#[Test]
|
|
public function allFourLocaleFilesLoadAndAreArrays(): void
|
|
{
|
|
foreach (self::localeProvider() as [$locale]) {
|
|
I18n::flushCache();
|
|
$data = require __DIR__ . '/../../app/Locales/' . $locale . '.php';
|
|
self::assertIsArray($data, "Locale file {$locale}.php must return an array");
|
|
self::assertNotEmpty($data, "Locale file {$locale}.php must not be empty");
|
|
}
|
|
}
|
|
|
|
#[Test]
|
|
public function everyLocaleHasExactlyTheSameKeySet(): void
|
|
{
|
|
$keysByLocale = [];
|
|
foreach (self::localeProvider() as [$locale]) {
|
|
$keysByLocale[$locale] = array_keys(require __DIR__ . '/../../app/Locales/' . $locale . '.php');
|
|
sort($keysByLocale[$locale]);
|
|
}
|
|
|
|
$reference = $keysByLocale['de'];
|
|
foreach (['en', 'uk', 'ru'] as $locale) {
|
|
$missing = array_diff($reference, $keysByLocale[$locale]);
|
|
$extra = array_diff($keysByLocale[$locale], $reference);
|
|
self::assertSame(
|
|
[],
|
|
$missing,
|
|
"Locale '{$locale}' is missing keys: " . implode(', ', $missing)
|
|
);
|
|
self::assertSame(
|
|
[],
|
|
$extra,
|
|
"Locale '{$locale}' has extra keys not in DE: " . implode(', ', $extra)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[Test]
|
|
public function noTranslationValueIsEmpty(): void
|
|
{
|
|
foreach (self::localeProvider() as [$locale]) {
|
|
$data = require __DIR__ . '/../../app/Locales/' . $locale . '.php';
|
|
foreach ($data as $key => $value) {
|
|
self::assertIsString($value, "{$locale}.{$key} must be a string");
|
|
self::assertNotSame('', trim($value), "{$locale}.{$key} must not be empty");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[Test]
|
|
#[DataProvider('localeProvider')]
|
|
public function everyTranslationIsValidUtf8(string $locale): void
|
|
{
|
|
$data = require __DIR__ . '/../../app/Locales/' . $locale . '.php';
|
|
foreach ($data as $key => $value) {
|
|
self::assertTrue(
|
|
mb_check_encoding($value, 'UTF-8'),
|
|
"{$locale}.{$key} contains invalid UTF-8"
|
|
);
|
|
}
|
|
}
|
|
}
|