Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logic for nested values in arrays for language #2331

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 . '.');
}
}
}
Loading