Skip to content

Commit

Permalink
rename command to statuscommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe Damen committed Feb 2, 2024
1 parent e223c19 commit 32d7b46
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 127 deletions.
38 changes: 0 additions & 38 deletions src/Commands/CheckTranslationsCommand.php

This file was deleted.

47 changes: 47 additions & 0 deletions src/Commands/StatusCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace NextApps\PoeditorSync\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use NextApps\PoeditorSync\Poeditor\Poeditor;
use NextApps\PoeditorSync\Translations\TranslationManager;

class StatusCommand extends Command
{
protected $signature = 'poeditor:status';

protected $description = 'Check if local translations match the ones on POEditor';

public function handle() : int
{
$isOutdated = $this->getLocales()->map(function ($locale, $key) {
$poeditorTranslations = app(Poeditor::class)->download(is_string($key) ? $key : $locale);

return collect($locale)->map(function ($internalLocale) use ($poeditorTranslations) {
$localTranslations = app(TranslationManager::class)->getTranslations($internalLocale);

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

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

return false;
});
})->flatten()->contains(false);

if ($isOutdated) {
return Command::FAILURE;
}

$this->info('All translations match the ones on POEditor!');

return Command::SUCCESS;
}

protected function getLocales() : Collection
{
return collect(config('poeditor-sync.locales'));
}
}
4 changes: 2 additions & 2 deletions src/PoeditorSyncServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace NextApps\PoeditorSync;

use Illuminate\Support\ServiceProvider;
use NextApps\PoeditorSync\Commands\CheckTranslationsCommand;
use NextApps\PoeditorSync\Commands\DownloadCommand;
use NextApps\PoeditorSync\Commands\StatusCommand;
use NextApps\PoeditorSync\Commands\UploadCommand;
use NextApps\PoeditorSync\Poeditor\Poeditor;

Expand All @@ -20,7 +20,7 @@ public function boot() : void
$this->commands([
DownloadCommand::class,
UploadCommand::class,
CheckTranslationsCommand::class,
StatusCommand::class,
]);
}
}
Expand Down
102 changes: 30 additions & 72 deletions tests/DownloadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace NextApps\PoeditorSync\Tests;

use Illuminate\Filesystem\Filesystem;
use NextApps\PoeditorSync\Poeditor\Poeditor;
use Symfony\Component\VarExporter\VarExporter;

class DownloadCommandTest extends TestCase
{
Expand Down Expand Up @@ -41,10 +39,10 @@ public function it_saves_php_translations_of_every_locale()

$this->artisan('poeditor:download')->assertExitCode(0);

$this->assertPhpTranslationFile(lang_path('en/first-en-php-file.php'), ['foo' => 'bar']);
$this->assertPhpTranslationFile(lang_path('en/second-en-php-file.php'), ['foo_bar' => 'bar foo']);
$this->assertPhpTranslationFile(lang_path('nl/first-nl-php-file.php'), ['bar_foo' => 'foo bar']);
$this->assertPhpTranslationFile(lang_path('nl/second-nl-php-file.php'), ['bar' => 'foo']);
$this->assertPhpTranslationFile('en/first-en-php-file.php', ['foo' => 'bar']);
$this->assertPhpTranslationFile('en/second-en-php-file.php', ['foo_bar' => 'bar foo']);
$this->assertPhpTranslationFile('nl/first-nl-php-file.php', ['bar_foo' => 'foo bar']);
$this->assertPhpTranslationFile('nl/second-nl-php-file.php', ['bar' => 'foo']);
}

/** @test */
Expand All @@ -64,11 +62,11 @@ public function it_saves_json_translations_of_every_locale()

$this->artisan('poeditor:download')->assertExitCode(0);

$this->assertJsonTranslationFile(lang_path('en.json'), [
$this->assertJsonTranslationFile('en.json', [
'first-en-json-key' => 'bar',
'second-en-json-key' => 'bar foo',
]);
$this->assertJsonTranslationFile(lang_path('nl.json'), [
$this->assertJsonTranslationFile('nl.json', [
'first-nl-json-key' => 'foo bar',
'second-nl-json-key' => 'foo',
]);
Expand Down Expand Up @@ -123,35 +121,35 @@ public function it_saves_vendor_php_and_json_translations_of_every_locale()
$this->assertFalse(file_exists(lang_path('nl/vendor.php')));

$this->assertPhpTranslationFile(
lang_path('vendor/first-package/en/first-package-en-php-file.php'),
'vendor/first-package/en/first-package-en-php-file.php',
['foo' => 'bar']
);
$this->assertJsonTranslationFile(lang_path('vendor/first-package/en.json'), [
$this->assertJsonTranslationFile('vendor/first-package/en.json', [
'first-package-first-en-json-key' => 'bar foo',
'first-package-second-en-json-key' => 'foo bar',
]);
$this->assertPhpTranslationFile(
lang_path('vendor/second-package/en/second-package-en-php-file.php'),
'vendor/second-package/en/second-package-en-php-file.php',
['bar' => 'foo']
);
$this->assertJsonTranslationFile(lang_path('vendor/second-package/en.json'), [
$this->assertJsonTranslationFile('vendor/second-package/en.json', [
'second-package-first-en-json-key' => 'bar foo bar',
'second-package-second-en-json-key' => 'foo bar foo',
]);

$this->assertPhpTranslationFile(
lang_path('vendor/first-package/nl/first-package-nl-php-file.php'),
'vendor/first-package/nl/first-package-nl-php-file.php',
['bar' => 'foo']
);
$this->assertJsonTranslationFile(lang_path('vendor/first-package/nl.json'), [
$this->assertJsonTranslationFile('vendor/first-package/nl.json', [
'first-package-first-nl-json-key' => 'foo bar',
'first-package-second-nl-json-key' => 'bar foo',
]);
$this->assertPhpTranslationFile(
lang_path('vendor/second-package/nl/second-package-nl-php-file.php'),
'vendor/second-package/nl/second-package-nl-php-file.php',
['foo' => 'bar']
);
$this->assertJsonTranslationFile(lang_path('vendor/second-package/nl.json'), [
$this->assertJsonTranslationFile('vendor/second-package/nl.json', [
'second-package-first-nl-json-key' => 'foo bar foo',
'second-package-second-nl-json-key' => 'bar foo bar',
]);
Expand Down Expand Up @@ -179,10 +177,10 @@ public function it_saves_php_and_json_and_vendor_translations_of_locale()

$this->assertFalse(file_exists(lang_path('en/vendor.php')));

$this->assertPhpTranslationFile(lang_path('en/php-file.php'), ['foo' => 'bar']);
$this->assertJsonTranslationFile(lang_path('en.json'), ['json-key' => 'bar foo']);
$this->assertPhpTranslationFile(lang_path('vendor/package-name/en/package-php-file.php'), ['bar' => 'foo']);
$this->assertJsonTranslationFile(lang_path('vendor/package-name/en.json'), ['package-json-key' => 'foo bar foo']);
$this->assertPhpTranslationFile('en/php-file.php', ['foo' => 'bar']);
$this->assertJsonTranslationFile('en.json', ['json-key' => 'bar foo']);
$this->assertPhpTranslationFile('vendor/package-name/en/package-php-file.php', ['bar' => 'foo']);
$this->assertJsonTranslationFile('vendor/package-name/en.json', ['package-json-key' => 'foo bar foo']);
}

/** @test */
Expand Down Expand Up @@ -232,7 +230,7 @@ public function it_overrides_old_json_translation_of_locale()

$this->artisan('poeditor:download')->assertExitCode(0);

$this->assertJsonTranslationFile(lang_path('en.json'), ['bar' => 'foo']);
$this->assertJsonTranslationFile('en.json', ['bar' => 'foo']);
}

/** @test */
Expand Down Expand Up @@ -275,7 +273,7 @@ public function it_overrides_old_json_vendor_translations_of_locale()

$this->artisan('poeditor:download')->assertExitCode(0);

$this->assertJsonTranslationFile(lang_path('vendor/package-name/en.json'), ['bar' => 'foo']);
$this->assertJsonTranslationFile('vendor/package-name/en.json', ['bar' => 'foo']);
}

/** @test */
Expand Down Expand Up @@ -344,10 +342,10 @@ public function it_maps_poeditor_locales_on_internal_locales()

$this->artisan('poeditor:download')->assertExitCode(0);

$this->assertPhpTranslationFile(lang_path('en/en-php-file.php'), ['foo' => 'bar']);
$this->assertJsonTranslationFile(lang_path('en.json'), ['foo bar' => 'bar foo']);
$this->assertPhpTranslationFile(lang_path('nl/nl-php-file.php'), ['bar' => 'foo']);
$this->assertJsonTranslationFile(lang_path('nl.json'), ['bar foo' => 'foo bar']);
$this->assertPhpTranslationFile('en/en-php-file.php', ['foo' => 'bar']);
$this->assertJsonTranslationFile('en.json', ['foo bar' => 'bar foo']);
$this->assertPhpTranslationFile('nl/nl-php-file.php', ['bar' => 'foo']);
$this->assertJsonTranslationFile('nl.json', ['bar foo' => 'foo bar']);
}

/** @test */
Expand All @@ -371,51 +369,11 @@ public function it_maps_poeditor_locales_on_multiple_internal_locales()

$this->artisan('poeditor:download')->assertExitCode(0);

$this->assertPhpTranslationFile(lang_path('en_GB/en-php-file.php'), ['foo' => 'bar']);
$this->assertJsonTranslationFile(lang_path('en_GB.json'), ['foo bar' => 'bar foo']);
$this->assertPhpTranslationFile(lang_path('nl_BE/nl-php-file.php'), ['bar' => 'foo']);
$this->assertJsonTranslationFile(lang_path('nl_BE.json'), ['bar foo' => 'foo bar']);
$this->assertPhpTranslationFile(lang_path('nl_NL/nl-php-file.php'), ['bar' => 'foo']);
$this->assertJsonTranslationFile(lang_path('nl_NL.json'), ['bar foo' => 'foo bar']);
}

/**
* Mock the POEditor "download" method.
*
* @param string $language
* @param array $data
*
* @return void
*/
public function mockPoeditorDownload(string $language, array $data)
{
if (get_class(app(Poeditor::class)) !== Poeditor::class) {
app(Poeditor::class)->shouldReceive('download')
->with($language)
->andReturn($data);

return;
}

$this->mock(Poeditor::class, function ($mock) use ($language, $data) {
$mock->shouldReceive('download')
->with($language)
->andReturn($data);
});
}

public function assertPhpTranslationFile(string $filename, array $data) : void
{
$this->assertTrue(file_exists($filename));
$this->assertEquals(
'<?php' . PHP_EOL . PHP_EOL . 'return ' . VarExporter::export($data) . ';' . PHP_EOL,
file_get_contents($filename)
);
}

public function assertJsonTranslationFile(string $filename, array $data) : void
{
$this->assertTrue(file_exists($filename));
$this->assertEquals(json_encode($data, JSON_PRETTY_PRINT), file_get_contents($filename));
$this->assertPhpTranslationFile('en_GB/en-php-file.php', ['foo' => 'bar']);
$this->assertJsonTranslationFile('en_GB.json', ['foo bar' => 'bar foo']);
$this->assertPhpTranslationFile('nl_BE/nl-php-file.php', ['bar' => 'foo']);
$this->assertJsonTranslationFile('nl_BE.json', ['bar foo' => 'foo bar']);
$this->assertPhpTranslationFile('nl_NL/nl-php-file.php', ['bar' => 'foo']);
$this->assertJsonTranslationFile('nl_NL.json', ['bar foo' => 'foo bar']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace NextApps\PoeditorSync\Tests;

use Illuminate\Filesystem\Filesystem;
use NextApps\PoeditorSync\Poeditor\Poeditor;

class CheckTranslationsCommandTest extends TestCase
class StatusCommandTest extends TestCase
{
protected function setUp() : void
{
Expand All @@ -21,6 +20,7 @@ public function it_checks_if_local_translations_match_downloaded_translations()
config()->set('poeditor-sync.locales', ['en', 'nl']);

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

$this->mockPoeditorDownload('en', [
'php-file' => [
Expand All @@ -37,20 +37,17 @@ public function it_checks_if_local_translations_match_downloaded_translations()
],
]);

$this->artisan('poeditor:check')
->expectsOutput('Checking translations for en')
->expectsOutput('The translations for en do not match the ones on POEditor')
->expectsOutput('Checking translations for nl')
->expectsOutput('The translations for nl do not match the ones on POEditor')
->assertExitCode(0);
}
$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.')
->assertExitCode(1);

public function mockPoeditorDownload(string $language, array $data) : void
{
if (get_class(app(Poeditor::class)) === Poeditor::class) {
$this->mock(Poeditor::class);
}
$this->createPhpTranslationFile('en/php-file.php', ['foo' => 'bar', 'nested' => ['value' => 'nested value']]);

app(Poeditor::class)->shouldReceive('download')->with($language)->andReturn($data);
$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);
}
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Str;
use NextApps\PoeditorSync\Poeditor\Poeditor;
use NextApps\PoeditorSync\PoeditorSyncServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Symfony\Component\VarExporter\VarExporter;
Expand Down Expand Up @@ -74,4 +75,13 @@ public function assertJsonTranslationFile(string $filename, array $data) : void
$this->assertTrue(file_exists($filename));
$this->assertEquals(json_encode($data, JSON_PRETTY_PRINT), file_get_contents($filename));
}

public function mockPoeditorDownload(string $language, array $data) : void
{
if (get_class(app(Poeditor::class)) === Poeditor::class) {
$this->mock(Poeditor::class);
}

app(Poeditor::class)->shouldReceive('download')->with($language)->andReturn($data);
}
}

0 comments on commit 32d7b46

Please sign in to comment.