Skip to content

Commit

Permalink
Merge pull request #26 from nextapps-be/feature/show_keys_that_do_not…
Browse files Browse the repository at this point in the history
…_match_in_status_command

fix status command + show which keys don't match
  • Loading branch information
yinx authored Apr 5, 2024
2 parents 15d1fbb + 837eef4 commit 0b8ffde
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/Commands/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,27 @@ public function handle() : int
return collect($locale)->map(function ($internalLocale) use ($poeditorTranslations) {
$localTranslations = app(TranslationManager::class)->getTranslations($internalLocale);

if ($poeditorTranslations === $localTranslations) {
$poeditorTranslations = collect($poeditorTranslations)->dot()->sortKeys();
$localTranslations = collect($localTranslations)->dot()->sortKeys();

if ($poeditorTranslations->toArray() === $localTranslations->toArray()) {
return true;
}

$this->error("The translations for '{$internalLocale}' do not match the ones on POEditor.");

$outdatedLocalTranslations = $poeditorTranslations->diff($localTranslations);
$outdatedPoeditorTranslations = $localTranslations->diff($poeditorTranslations);

$this->table(
['Translation Key'],
$outdatedLocalTranslations->merge($outdatedPoeditorTranslations)
->keys()
->unique()
->map(fn ($key) => [$key])
->all()
);

return false;
});
})->flatten()->contains(false);
Expand Down
63 changes: 61 additions & 2 deletions tests/StatusCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public function it_checks_if_local_translations_match_downloaded_translations()

$this->mockPoeditorDownload('en', [
'php-file' => [
'foo' => 'bar',
'nested' => [
'value' => 'nested value',
],
'baz' => 'baz',
'foo' => 'bar',
],
]);

Expand All @@ -40,14 +41,72 @@ public function it_checks_if_local_translations_match_downloaded_translations()
$this->artisan('poeditor:status')
->expectsOutput('The translations for \'en\' do not match the ones on POEditor.')
->doesntExpectOutput('The translations for \'nl\' do not match the ones on POEditor.')
->expectsTable(
['Translation Key'],
[
['php-file.baz'],
]
)
->assertExitCode(1);

$this->createPhpTranslationFile('en/php-file.php', ['foo' => 'bar', 'nested' => ['value' => 'nested value']]);
$this->createPhpTranslationFile('en/php-file.php', ['foo' => 'bar', 'nested' => ['value' => 'nested value'], 'baz' => 'baz', 'foo' => 'bar']);

$this->artisan('poeditor:status')
->expectsOutput('All translations match the ones on POEditor!')
->doesntExpectOutput('The translations for \'en\' do not match the ones on POEditor.')
->doesntExpectOutput('The translations for \'nl\' do not match the ones on POEditor.')
->assertExitCode(0);
}

/** @test */
public function it_does_not_fail_if_translations_in_different_order()
{
config()->set('poeditor-sync.locales', ['en']);

$this->createPhpTranslationFile('en/php-file.php', ['foo' => 'bar', 'baz' => 'bar']);

$this->mockPoeditorDownload('en', [
'php-file' => [
'baz' => 'bar',
'foo' => 'bar',
],
]);

$this->artisan('poeditor:status')
->expectsOutput('All translations match the ones on POEditor!')
->doesntExpectOutput('The translations for \'en\' do not match the ones on POEditor.')
->doesntExpectOutput('The translations for \'nl\' do not match the ones on POEditor.')
->assertExitCode(0);
}

/** @test */
public function it_fails_if_translations_do_not_match()
{
config()->set('poeditor-sync.locales', ['en']);

$this->createPhpTranslationFile('en/php-file.php', ['foo' => 'bar', 'nested' => ['value' => 'nested value']]);

$this->mockPoeditorDownload('en', [
'php-file' => [
'foo' => 'bar',
'nested' => [
'value' => 'nested values',
],
],
]);

$this->artisan('poeditor:status')
->expectsOutput('The translations for \'en\' do not match the ones on POEditor.')
->expectsTable(['Translation Key'], [
['php-file.nested.value'],
])
->assertExitCode(1);

$this->createPhpTranslationFile('en/php-file.php', ['foo' => 'bar', 'nested' => ['value' => 'nested values']]);

$this->artisan('poeditor:status')
->expectsOutput('All translations match the ones on POEditor!')
->doesntExpectOutput('The translations for \'en\' do not match the ones on POEditor.')
->assertExitCode(0);
}
}

0 comments on commit 0b8ffde

Please sign in to comment.