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..3917024 100644 --- a/tests/LocalizatorTest.php +++ b/tests/LocalizatorTest.php @@ -165,4 +165,44 @@ 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'); + } + + 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'); + } }