Skip to content

Commit

Permalink
Improve logic for nested values in arrays for language (LycheeOrg#2331)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Apr 6, 2024
1 parent 70b02d2 commit 1a5fbb1
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions tests/Feature/LangTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@

use function Safe\scandir;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Tests\AbstractTestCase;

class LangTest extends AbstractTestCase
{
private ConsoleSectionOutput $msgSection;
private bool $failed = false;

/**
* Test Languages are complete.
*
Expand All @@ -28,9 +32,7 @@ public function testLanguageConsistency(): void
static::assertEquals('en', app()->getLocale());
static::assertEquals('OK', __('lychee.SUCCESS'));

$msgSection = (new ConsoleOutput())->section();

$failed = false;
$this->msgSection = (new ConsoleOutput())->section();

/** @var array<int,string> $englishDictionaries */
$englishDictionaries = collect(array_diff(scandir(base_path('lang/en')), ['..', '.']))->filter(fn ($v) => str_ends_with($v, '.php'))->all();
Expand All @@ -40,21 +42,10 @@ public function testLanguageConsistency(): void

foreach ($availableDictionaries as $locale) {
$dictionary = include base_path('lang/' . $locale . '/' . $dictionaryFile);
$missingKeys = array_diff_key($englishDictionary, $dictionary);
foreach ($missingKeys as $key => $value) {
$msgSection->writeln(sprintf('<comment>Error:</comment> Locale %s %s misses the following key: %s', str_pad($locale, 8), $dictionaryFile, $key));
$failed = true;
}

$extraKeys = array_diff_key($dictionary, $englishDictionary);
foreach ($extraKeys as $key => $value) {
$msgSection->writeln(sprintf('<comment>Error:</comment> Locale %s %s has the following extra key: %s', str_pad($locale, 8), $dictionaryFile, $key));
$failed = true;
}
// $msgSection->writeln(sprintf('<comment>Error:</comment> Locale %s %s is done', str_pad($locale, 8), $dictionaryFile));
$this->recursiveCheck($englishDictionary, $dictionary, $locale, $dictionaryFile);
}
}
static::assertFalse($failed);
static::assertFalse($this->failed);
}

public function testEnglishAsFallbackIfLangConfigIsMissing(): void
Expand All @@ -63,4 +54,27 @@ public function testEnglishAsFallbackIfLangConfigIsMissing(): void
static::assertEquals('ZK', app()->getLocale());
static::assertEquals('OK', __('lychee.SUCCESS'));
}

private function recursiveCheck(array $expected, array $candidate, string $locale, string $file, string $prefix = ''): void
{
$missingKeys = array_diff_key($expected, $candidate);

foreach ($missingKeys as $key => $value) {
$this->msgSection->writeln(sprintf('<comment>Error:</comment> Locale %s %s misses the following key: %s', str_pad($locale, 8), $file, $prefix . $key));
$this->failed = true;
}

$extraKeys = array_diff_key($candidate, $expected);
foreach ($extraKeys as $key => $value) {
$this->msgSection->writeln(sprintf('<comment>Error:</comment> Locale %s %s has the following extra key: %s', str_pad($locale, 8), $file, $prefix . $key));
$this->failed = true;
}

$expected_arrays = array_filter($expected, fn ($v) => is_array($v));
$candidate_arrays = array_filter($candidate, fn ($v) => is_array($v));
foreach ($expected_arrays as $key => $sub_expected) {
$sub_candidate = $candidate_arrays[$key] ?? [];
$this->recursiveCheck($sub_expected, $sub_candidate, $locale, $file, $key . '.');
}
}
}

0 comments on commit 1a5fbb1

Please sign in to comment.