diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2019-07-08 01:31:59 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2019-07-08 01:58:06 +0200 |
commit | f90ab26feedb61615bde2f94bbf5acc7e4f28342 (patch) | |
tree | bef3b8c72e27089dbef317512c4ab2d11ab34405 /tests/Unit/Helpers/Translation/TranslatorTest.php | |
parent | e9f157ec5ccdfae73b4c9e82c9ae7c37bcfa1513 (diff) |
Moved translation helpers to sub namespace
Diffstat (limited to 'tests/Unit/Helpers/Translation/TranslatorTest.php')
-rw-r--r-- | tests/Unit/Helpers/Translation/TranslatorTest.php | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/tests/Unit/Helpers/Translation/TranslatorTest.php b/tests/Unit/Helpers/Translation/TranslatorTest.php new file mode 100644 index 00000000..7e9c534c --- /dev/null +++ b/tests/Unit/Helpers/Translation/TranslatorTest.php @@ -0,0 +1,90 @@ +<?php + +namespace Engelsystem\Test\Unit\Helpers\Translation; + +use Engelsystem\Helpers\Translation\Translator; +use Engelsystem\Test\Unit\ServiceProviderTest; +use PHPUnit\Framework\MockObject\MockObject; +use stdClass; + +class TranslatorTest extends ServiceProviderTest +{ + /** + * @covers \Engelsystem\Helpers\Translation\Translator::__construct + * @covers \Engelsystem\Helpers\Translation\Translator::getLocale + * @covers \Engelsystem\Helpers\Translation\Translator::getLocales + * @covers \Engelsystem\Helpers\Translation\Translator::hasLocale + * @covers \Engelsystem\Helpers\Translation\Translator::setLocale + * @covers \Engelsystem\Helpers\Translation\Translator::setLocales + */ + public function testInit() + { + $locales = ['te_ST.ER-01' => 'Tests', 'fo_OO' => 'SomeFOO']; + $locale = 'te_ST.ER-01'; + + /** @var callable|MockObject $callable */ + $callable = $this->getMockBuilder(stdClass::class) + ->setMethods(['__invoke']) + ->getMock(); + $callable->expects($this->exactly(2)) + ->method('__invoke') + ->withConsecutive(['te_ST.ER-01'], ['fo_OO']); + + $translator = new Translator($locale, $locales, $callable); + + $this->assertEquals($locales, $translator->getLocales()); + $this->assertEquals($locale, $translator->getLocale()); + + $translator->setLocale('fo_OO'); + $this->assertEquals('fo_OO', $translator->getLocale()); + + $newLocales = ['lo_RM' => 'Lorem', 'ip_SU-M' => 'Ipsum']; + $translator->setLocales($newLocales); + $this->assertEquals($newLocales, $translator->getLocales()); + + $this->assertTrue($translator->hasLocale('ip_SU-M')); + $this->assertFalse($translator->hasLocale('te_ST.ER-01')); + } + + /** + * @covers \Engelsystem\Helpers\Translation\Translator::replaceText + * @covers \Engelsystem\Helpers\Translation\Translator::translate + */ + public function testTranslate() + { + /** @var Translator|MockObject $translator */ + $translator = $this->getMockBuilder(Translator::class) + ->setConstructorArgs(['de_DE.UTF-8', ['de_DE.UTF-8' => 'Deutsch']]) + ->setMethods(['translateGettext']) + ->getMock(); + $translator->expects($this->exactly(2)) + ->method('translateGettext') + ->withConsecutive(['Hello!'], ['My favourite number is %u!']) + ->willReturnOnConsecutiveCalls('Hallo!', 'Meine Lieblingszahl ist die %u!'); + + $return = $translator->translate('Hello!'); + $this->assertEquals('Hallo!', $return); + + $return = $translator->translate('My favourite number is %u!', [3]); + $this->assertEquals('Meine Lieblingszahl ist die 3!', $return); + } + + /** + * @covers \Engelsystem\Helpers\Translation\Translator::translatePlural + */ + public function testTranslatePlural() + { + /** @var Translator|MockObject $translator */ + $translator = $this->getMockBuilder(Translator::class) + ->setConstructorArgs(['de_DE.UTF-8', ['de_DE.UTF-8' => 'Deutsch']]) + ->setMethods(['translateGettextPlural']) + ->getMock(); + $translator->expects($this->once()) + ->method('translateGettextPlural') + ->with('%s apple', '%s apples', 2) + ->willReturn('2 Äpfel'); + + $return = $translator->translatePlural('%s apple', '%s apples', 2, [2]); + $this->assertEquals('2 Äpfel', $return); + } +} |