Skip to content

Commit

Permalink
UnitTest 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
abbadon1334 committed Jul 10, 2019
1 parent 100151f commit 3d9907c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 11 deletions.
6 changes: 1 addition & 5 deletions src/I18Next/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(string $code)

/**
* @param string $path
* @param bool $use_filename_as_namespace
* @param string|null ...$namespace_priority
*
* @throws Exception
Expand All @@ -34,11 +35,6 @@ public function load(string $path, bool $use_filename_as_namespace, ?string ...$
$this->translations->load($path.DIRECTORY_SEPARATOR.$this->code, $use_filename_as_namespace, ...$namespace_priority);
}

public function useNamespace(): bool
{
return $this->translations->useNamespaces();
}

public function process(string $key, ?array $parameters = null, ?string $context = null): ?string
{
return $this->processor->process($key, $parameters, $context);
Expand Down
4 changes: 1 addition & 3 deletions src/I18Next/Locale/Processor/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public function processKey(string $key, ?string $context = null, ?int $counter =
return $this->processWithCounter($key, $counter);
}

if ($this->translations->useNamespaces()) {
return $this->processWithNamespaceWithCounter($key, $counter);
}
return $this->processWithNamespaceWithCounter($key, $counter);
}

private function processWithCounter(string $key, ?int $counter = null)
Expand Down
7 changes: 4 additions & 3 deletions src/I18Next/Locale/Processor/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,24 @@ private function processValueInterpolate(string &$found_key, ?array $parameters
continue;
}

$found_key = $this->processValueReplaceParamenter($found_key, $index, $parameters);
$found_key = $this->processValueReplaceParameter($found_key, $index, $parameters);
$this->processValueNested($found_key, $parameters);
}
}

return $found_key;
}

private function processValueReplaceParamenter(string &$found_key, $index, $parameters)
private function processValueReplaceParameter(string &$found_key, $index, $parameters)
{
$value = $parameters[$index] ?? null;

$token = explode('.', $index);
if (isset($token[1])) {

$value = $parameters[$token[0]] ?? null;

if (is_a($value, '\atk4\data\Model')) {
if (is_subclass_of($value, '\atk4\data\Model', true)) {
$value = $value->get();
}

Expand Down
1 change: 1 addition & 0 deletions src/I18Next/Locale/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class Translations

/**
* @param string $path
* @param bool $use_filename_as_namespace
* @param string|null ...$namespace_priority
*
* @throws Exception
Expand Down
24 changes: 24 additions & 0 deletions tests/Translator_Atk4Model_Mock_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ public function test3()
]);
$this->assertEquals('utente : John Smith con email : sara.jones@agiletoolkit.com', $result);
}

public function testWithModel()
{
$this->model->load(1);

$this->setupTranslatorLanguages('it', 'en');

$result = $this->translator->_('atk4_model_object2', [
'user' => $this->model,
'address' => $this->model,
]);
$this->assertEquals('utente : John Smith con email : john.smith@agiletoolkit.com', $result);
}

public function testWithObject()
{
$this->model->load(2);

$this->setupTranslatorLanguages('it', 'en');

$result = $this->translator->_('atk4_model_object', ['user' => (object) ($this->model->data) ]);
$this->assertEquals('utente : Sarah Jones con email : sara.jones@agiletoolkit.com', $result);
}

}
// @codingStandardsIgnoreStart
class ATK4ModelMock extends Model
Expand Down
7 changes: 7 additions & 0 deletions tests/Translator_Atk4Trait_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public function testBase()
$result = $child->_('friend', null, null, 'ro');
$this->assertEquals('Un prieten', $result);
}

public function testException()
{
$this->expectException(Exception::class);
$app = new ATK4AppScopeMock();
$app->_('friend');
}
}

// @codingStandardsIgnoreStart
Expand Down
18 changes: 18 additions & 0 deletions tests/Translator_CaseNamespace_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,22 @@ public function testNoPriorityWithprefixedNamespaceNoFallbackLanguage()
$result = $this->translator->_('namespace3:nskey');
$this->assertEquals('namespace3 key', $result);
}

public function test2_int()
{
$this->translator->setNamespacePriority('key');
$this->setupTranslatorLanguages('en');

$result = $this->translator->_('key', ['count' => 2]);
$this->assertEquals('two', $result);
}

public function test6_int()
{
$this->translator->setNamespacePriority('key');
$this->setupTranslatorLanguages('it', 'en');

$result = $this->translator->_('key', ['count' => 6]);
$this->assertEquals('other', $result);
}
}

0 comments on commit 3d9907c

Please sign in to comment.