From 37b35400436412bfcf8b449e31c068eb72248666 Mon Sep 17 00:00:00 2001 From: khalyomede Date: Mon, 24 Jan 2022 21:37:21 +0100 Subject: [PATCH 1/2] #28 add ci flag --- src/Commands/LocalizeCommand.php | 4 ++-- src/Services/Parser.php | 5 +++++ tests/LocalizatorTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Commands/LocalizeCommand.php b/src/Commands/LocalizeCommand.php index 2592cfb..ee7446d 100644 --- a/src/Commands/LocalizeCommand.php +++ b/src/Commands/LocalizeCommand.php @@ -16,7 +16,7 @@ class LocalizeCommand extends Command * * @var string */ - protected $signature = 'localize {lang?}'; + protected $signature = 'localize {lang?} {--c|ci : Returns a non zero code when some translations have been added.}'; /** * The console command description. @@ -65,7 +65,7 @@ public function handle(Localizator $localizator, Parser $parser): int "\nTranslatable strings have been generated for locale(s): ".implode(', ', $locales) ); - return 0; + return $this->option("ci") && $parser->foundSomeKeys() ? 1 : 0; } /** diff --git a/src/Services/Parser.php b/src/Services/Parser.php index 145e504..74b652a 100644 --- a/src/Services/Parser.php +++ b/src/Services/Parser.php @@ -69,6 +69,11 @@ public function parseKeys(): void }); } + public function foundSomeKeys(): bool + { + return ($this->defaultKeys->count() + $this->jsonKeys->count()) > 0; + } + /** * @param $key * @return bool diff --git a/tests/LocalizatorTest.php b/tests/LocalizatorTest.php index 863edb2..07a0b11 100644 --- a/tests/LocalizatorTest.php +++ b/tests/LocalizatorTest.php @@ -165,4 +165,28 @@ public function testLocalizeCommandWhereKeysAreEscapedWithSlashes(): void // Cleanup. self::flushDirectories('lang', 'views'); } + + public function testLocalizeCommandReturnsNonZeroCodeWhenCiFlagIsOnAndSomeMissingTranslationsHaveBeenAdded(): void + { + $this->createTestView("{{ __('Hi mom') }}"); + + // Run localize command. + $this->artisan('localize', ['--ci' => true]) + ->assertExitCode(1); + + // Cleanup. + self::flushDirectories('lang', 'views'); + } + + public function testLocalizeCommandReturnsZeroCodeWhenCiFlagIsOnAndNoMissingTranslationsHaveBeenFound(): void + { + $this->createTestView(""); + + // Run localize command. + $this->artisan('localize', ['--ci' => true]) + ->assertExitCode(0); + + // Cleanup. + self::flushDirectories('lang', 'views'); + } } From febac85fafe1490955d6f0b67647c4ef6b7f3738 Mon Sep 17 00:00:00 2001 From: khalyomede Date: Mon, 24 Jan 2022 21:46:43 +0100 Subject: [PATCH 2/2] #28 add test case --- tests/LocalizatorTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/LocalizatorTest.php b/tests/LocalizatorTest.php index 07a0b11..3917024 100644 --- a/tests/LocalizatorTest.php +++ b/tests/LocalizatorTest.php @@ -189,4 +189,20 @@ public function testLocalizeCommandReturnsZeroCodeWhenCiFlagIsOnAndNoMissingTran // Cleanup. self::flushDirectories('lang', 'views'); } + + public function testLocalizeCommandReturnsNonZeroCodeThenZeroCodeIfFileDidNotChangedBetweenTwoRun(): void + { + $this->createTestView("{{ __('Hi mom') }}"); + + // Run localize command. + $this->artisan('localize', ['--ci' => true]) + ->assertExitCode(1); + + // Run localize command. + $this->artisan('localize', ['--ci' => true]) + ->assertExitCode(0); + + // Cleanup. + self::flushDirectories('lang', 'views'); + } }