Skip to content

Commit

Permalink
update show command and add tests for specifying languages
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Apr 9, 2016
1 parent e170475 commit 3cf8337
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/Commands/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Themsaid\Langman\Manager;

class ShowCommand extends Command
Expand Down Expand Up @@ -52,7 +53,7 @@ class ShowCommand extends Command
protected $files;

/**
* Array of selected languages.
* Array of displayable languages.
*
* @var array
*/
Expand Down Expand Up @@ -81,14 +82,12 @@ public function handle()

$this->files = $this->filesFromKey();

$this->languages = $this->manager->languages();
try {
$this->languages = $this->getLanguages();
} catch (InvalidArgumentException $e) {
$this->error($e->getMessage());

if ($this->option('lang') != null) {
$languages = explode(',', $this->option('lang'));
if (!empty($diffLangagues = array_diff($languages, $this->languages))) {
return $this->error('Unknown Langauges [ '.implode($diffLangagues,',').' ]');
}
$this->languages = explode(',', $this->option('lang'));
return;
}

$this->table(
Expand All @@ -110,11 +109,11 @@ private function tableRows()

foreach ($this->files as $languageKey => $file) {
foreach ($filesContent[$languageKey] = Arr::dot($this->manager->getFileContent($file)) as $key => $value) {
if (!$this->shouldShowKey($key)) {
if (! $this->shouldShowKey($key)) {
continue;
}

$output[$key]['key'] = $key;
$output[$key]['key'] = $key;
$output[$key][$languageKey] = $value;
}
}
Expand Down Expand Up @@ -194,15 +193,37 @@ private function shouldShowKey($key)
return true;
}

if (!$this->option('close') && $key != $this->key) {
if (! $this->option('close') && $key != $this->key) {
return false;
}

if ($this->option('close') && !Str::contains($key, $this->key)) {
if ($this->option('close') && ! Str::contains($key, $this->key)) {
return false;
}
}

return true;
}

/**
* Get the languages to be displayed in the command output.
*
* @return array
*/
private function getLanguages()
{
$allLanguages = $this->manager->languages();

if (! $this->option('lang')) {
return $allLanguages;
}

$userLanguages = explode(',', (string) $this->option('lang'));

if ($missingLanguages = array_diff($userLanguages, $allLanguages)) {
throw new InvalidArgumentException('Unknown Language(s) ['.implode(',', $missingLanguages).'].');
}

return $userLanguages;
}
}
16 changes: 16 additions & 0 deletions tests/ShowCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ public function testCommandOutputForFile()
$this->assertRegExp('/age(?:.*)Age(?:.*)|(?: *)|/', $this->consoleOutput());
}

public function testCommandOutputForFileAndSpecificLanguages()
{
$this->createTempFiles([
'en' => ['user' => "<?php\n return ['name' => 'Name', 'age' => 'Age'];"],
'nl' => ['user' => "<?php\n return ['name' => 'Naam'];"],
'it_lang' => ['user' => "<?php\n return ['name' => 'Nome'];"],
]);

$this->artisan('langman:show', ['key' => 'user', '--lang' => 'en,nl']);

$this->assertRegExp('/key(?:.*)en(?:.*)nl/', $this->consoleOutput());
$this->assertRegExp('/name(?:.*)Name(?:.*)Naam/', $this->consoleOutput());
$this->assertNotContains('Nome', $this->consoleOutput());
$this->assertNotContains('it_lang', $this->consoleOutput());
}

public function testCommandOutputForPackageFile()
{
$this->createTempFiles([
Expand Down

1 comment on commit 3cf8337

@ahmedash95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great modifications, i have to improve my code next.

Please sign in to comment.