From f90ab26feedb61615bde2f94bbf5acc7e4f28342 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 8 Jul 2019 01:31:59 +0200 Subject: Moved translation helpers to sub namespace --- .../Translation/TranslationServiceProviderTest.php | 84 ++++++++++++++++++++ tests/Unit/Helpers/Translation/TranslatorTest.php | 90 ++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php create mode 100644 tests/Unit/Helpers/Translation/TranslatorTest.php (limited to 'tests/Unit/Helpers/Translation') diff --git a/tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php b/tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php new file mode 100644 index 00000000..171b5967 --- /dev/null +++ b/tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php @@ -0,0 +1,84 @@ +getApp(['make', 'instance', 'get']); + /** @var Config|MockObject $config */ + $config = $this->createMock(Config::class); + /** @var Session|MockObject $session */ + $session = $this->createMock(Session::class); + /** @var Translator|MockObject $translator */ + $translator = $this->createMock(Translator::class); + + /** @var TranslationServiceProvider|MockObject $serviceProvider */ + $serviceProvider = $this->getMockBuilder(TranslationServiceProvider::class) + ->setConstructorArgs([$app]) + ->setMethods(['initGettext', 'setLocale']) + ->getMock(); + + $serviceProvider->expects($this->once()) + ->method('initGettext'); + + $app->expects($this->exactly(2)) + ->method('get') + ->withConsecutive(['config'], ['session']) + ->willReturnOnConsecutiveCalls($config, $session); + + $defaultLocale = 'fo_OO'; + $locale = 'te_ST.WTF-9'; + $locales = ['fo_OO' => 'Foo', 'fo_OO.BAR' => 'Foo (Bar)', 'te_ST.WTF-9' => 'WTF\'s Testing?']; + $config->expects($this->exactly(2)) + ->method('get') + ->withConsecutive( + ['locales'], + ['default_locale'] + ) + ->willReturnOnConsecutiveCalls( + $locales, + $defaultLocale + ); + + $session->expects($this->once()) + ->method('get') + ->with('locale', $defaultLocale) + ->willReturn($locale); + $session->expects($this->once()) + ->method('set') + ->with('locale', $locale); + + $app->expects($this->once()) + ->method('make') + ->with( + Translator::class, + [ + 'locale' => $locale, + 'locales' => $locales, + 'localeChangeCallback' => [$serviceProvider, 'setLocale'], + ] + ) + ->willReturn($translator); + + $app->expects($this->exactly(2)) + ->method('instance') + ->withConsecutive( + [Translator::class, $translator], + ['translator', $translator] + ); + + $serviceProvider->register(); + } +} 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 @@ + '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); + } +} -- cgit v1.2.3-54-g00ecf