Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/translator-tests' of https://github.com/denixpo…
Browse files Browse the repository at this point in the history
…rt/zf2 into hotfix/translator-testing
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 104 deletions.
63 changes: 11 additions & 52 deletions test/Translator/Loader/GettextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,64 +62,23 @@ public function testLoaderLoadsBigEndianFile()
$this->assertInstanceOf('Zend\I18n\Translator\TextDomain', $domain);
}

public function testTranslatorAddsFile()
{
$translator = new Translator();
$translator->addTranslationFile('gettext', $this->testFilesDir . '/translation_en.mo');

$this->assertEquals('Message 1 (en)', $translator->translate('Message 1'));
$this->assertEquals('Message 6', $translator->translate('Message 6'));
}

public function testTranslatorAddsFileToTextDomain()
{
$translator = new Translator();
$translator->addTranslationFile('gettext', $this->testFilesDir . '/translation_en.mo', 'user');

$this->assertEquals('Message 2 (en)', $translator->translate('Message 2', 'user'));
}

public function testTranslatorAddsPattern()
{
$translator = new Translator();
$translator->addTranslationPattern(
'gettext',
$this->testFilesDir . '/testmo',
'translation-%s.mo'
);

$this->assertEquals('Message 1 (en)', $translator->translate('Message 1', 'default', 'en_US'));
$this->assertEquals('Nachricht 1', $translator->translate('Message 1', 'default', 'de_DE'));
}

public function testLoaderLoadsPluralRules()
public function testLoaderReturnsValidTextDomain()
{
$loader = new GettextLoader();
$domain = $loader->load($this->testFilesDir . '/translation_en.mo', 'en_EN');
$textDomain = $loader->load($this->testFilesDir . '/translation_en.mo', 'en_EN');

$this->assertEquals(2, $domain->getPluralRule()->evaluate(0));
$this->assertEquals(0, $domain->getPluralRule()->evaluate(1));
$this->assertEquals(1, $domain->getPluralRule()->evaluate(2));
$this->assertEquals(2, $domain->getPluralRule()->evaluate(10));
$this->assertEquals('Message 1 (en)', $textDomain['Message 1']);
$this->assertEquals('Message 4 (en)', $textDomain['Message 4']);
}

public function testTranslatorTranslatesPlurals()
public function testLoaderLoadsPluralRules()
{
$translator = new Translator();
$translator->setLocale('en_EN');
$translator->addTranslationFile(
'gettext',
$this->testFilesDir . '/translation_en.mo',
'default',
'en_EN'
);

$pl0 = $translator->translatePlural('Message 5', 'Message 5 Plural', 1);
$pl1 = $translator->translatePlural('Message 5', 'Message 5 Plural', 2);
$pl2 = $translator->translatePlural('Message 5', 'Message 5 Plural', 10);
$loader = new GettextLoader();
$textDomain = $loader->load($this->testFilesDir . '/translation_en.mo', 'en_EN');

$this->assertEquals('Message 5 (en) Plural 0', $pl0);
$this->assertEquals('Message 5 (en) Plural 1', $pl1);
$this->assertEquals('Message 5 (en) Plural 2', $pl2);
$this->assertEquals(2, $textDomain->getPluralRule()->evaluate(0));
$this->assertEquals(0, $textDomain->getPluralRule()->evaluate(1));
$this->assertEquals(1, $textDomain->getPluralRule()->evaluate(2));
$this->assertEquals(2, $textDomain->getPluralRule()->evaluate(10));
}
}
63 changes: 11 additions & 52 deletions test/Translator/Loader/PhpArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,64 +55,23 @@ public function testLoaderLoadsEmptyArray()
$this->assertInstanceOf('Zend\I18n\Translator\TextDomain', $textDomain);
}

public function testTranslatorAddsFile()
{
$translator = new Translator();
$translator->addTranslationFile('phparray', $this->testFilesDir . '/translation_en.php');

$this->assertEquals('Message 1 (en)', $translator->translate('Message 1'));
$this->assertEquals('Message 6', $translator->translate('Message 6'));
}

public function testTranslatorAddsFileToTextDomain()
{
$translator = new Translator();
$translator->addTranslationFile('phparray', $this->testFilesDir . '/translation_en.php', 'user');

$this->assertEquals('Message 2 (en)', $translator->translate('Message 2', 'user'));
}

public function testTranslatorAddsPattern()
{
$translator = new Translator();
$translator->addTranslationPattern(
'phparray',
$this->testFilesDir . '/testarray',
'translation-%s.php'
);

$this->assertEquals('Message 1 (en)', $translator->translate('Message 1', 'default', 'en_US'));
$this->assertEquals('Nachricht 1', $translator->translate('Message 1', 'default', 'de_DE'));
}

public function testLoaderLoadsPluralRules()
public function testLoaderReturnsValidTextDomain()
{
$loader = new PhpArrayLoader();
$domain = $loader->load($this->testFilesDir . '/translation_en.php', 'en_EN');
$textDomain = $loader->load($this->testFilesDir . '/translation_en.php', 'en_EN');

$this->assertEquals(2, $domain->getPluralRule()->evaluate(0));
$this->assertEquals(0, $domain->getPluralRule()->evaluate(1));
$this->assertEquals(1, $domain->getPluralRule()->evaluate(2));
$this->assertEquals(2, $domain->getPluralRule()->evaluate(10));
$this->assertEquals('Message 1 (en)', $textDomain['Message 1']);
$this->assertEquals('Message 4 (en)', $textDomain['Message 4']);
}

public function testTranslatorTranslatesPlurals()
public function testLoaderLoadsPluralRules()
{
$translator = new Translator();
$translator->setLocale('en_EN');
$translator->addTranslationFile(
'phparray',
$this->testFilesDir . '/translation_en.php',
'default',
'en_EN'
);

$pl0 = $translator->translatePlural('Message 5', 'Message 5 Plural', 1);
$pl1 = $translator->translatePlural('Message 5', 'Message 5 Plural', 2);
$pl2 = $translator->translatePlural('Message 5', 'Message 5 Plural', 10);
$loader = new PhpArrayLoader();
$textDomain = $loader->load($this->testFilesDir . '/translation_en.php', 'en_EN');

$this->assertEquals('Message 5 (en) Plural 0', $pl0);
$this->assertEquals('Message 5 (en) Plural 1', $pl1);
$this->assertEquals('Message 5 (en) Plural 2', $pl2);
$this->assertEquals(2, $textDomain->getPluralRule()->evaluate(0));
$this->assertEquals(0, $textDomain->getPluralRule()->evaluate(1));
$this->assertEquals(1, $textDomain->getPluralRule()->evaluate(2));
$this->assertEquals(2, $textDomain->getPluralRule()->evaluate(10));
}
}
19 changes: 19 additions & 0 deletions test/Translator/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,23 @@ public function testTranslate()

$this->assertEquals('bar', $this->translator->translate('foo'));
}

public function testTranslatePlurals()
{
$this->translator->setLocale('en_EN');
$this->translator->addTranslationFile(
'phparray',
$this->testFilesDir . '/translation_en.php',
'default',
'en_EN'
);

$pl0 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 1);
$pl1 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 2);
$pl2 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 10);

$this->assertEquals('Message 5 (en) Plural 0', $pl0);
$this->assertEquals('Message 5 (en) Plural 1', $pl1);
$this->assertEquals('Message 5 (en) Plural 2', $pl2);
}
}

0 comments on commit 95f0efa

Please sign in to comment.