From 508695efb253d7bc0caea1fa017ed5608d774596 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 8 Jul 2019 01:47:01 +0200 Subject: Replaced gettext translation with package This allows to check if no translation is available --- .../Helpers/Translation/Assets/fo_OO/default.mo | Bin 0 -> 73 bytes .../Helpers/Translation/Assets/fo_OO/default.po | 3 + .../Helpers/Translation/GettextTranslatorTest.php | 67 +++++++++++++++++ .../Translation/TranslationServiceProviderTest.php | 54 +++++++------- tests/Unit/Helpers/Translation/TranslatorTest.php | 80 ++++++++++++++++----- 5 files changed, 162 insertions(+), 42 deletions(-) create mode 100644 tests/Unit/Helpers/Translation/Assets/fo_OO/default.mo create mode 100644 tests/Unit/Helpers/Translation/Assets/fo_OO/default.po create mode 100644 tests/Unit/Helpers/Translation/GettextTranslatorTest.php (limited to 'tests/Unit') diff --git a/tests/Unit/Helpers/Translation/Assets/fo_OO/default.mo b/tests/Unit/Helpers/Translation/Assets/fo_OO/default.mo new file mode 100644 index 00000000..96f1f3ca Binary files /dev/null and b/tests/Unit/Helpers/Translation/Assets/fo_OO/default.mo differ diff --git a/tests/Unit/Helpers/Translation/Assets/fo_OO/default.po b/tests/Unit/Helpers/Translation/Assets/fo_OO/default.po new file mode 100644 index 00000000..015bc36d --- /dev/null +++ b/tests/Unit/Helpers/Translation/Assets/fo_OO/default.po @@ -0,0 +1,3 @@ +# Testing content +msgid "foo.bar" +msgstr "Foo Bar!" diff --git a/tests/Unit/Helpers/Translation/GettextTranslatorTest.php b/tests/Unit/Helpers/Translation/GettextTranslatorTest.php new file mode 100644 index 00000000..825cf5b7 --- /dev/null +++ b/tests/Unit/Helpers/Translation/GettextTranslatorTest.php @@ -0,0 +1,67 @@ +getTranslations(); + + $translator = new GettextTranslator(); + $translator->loadTranslations($translations); + + $this->assertEquals('Translation!', $translator->gettext('test.value')); + + $this->expectException(TranslationNotFound::class); + $this->expectExceptionMessage('//foo.bar'); + + $translator->gettext('foo.bar'); + } + + /** + * @covers \Engelsystem\Helpers\Translation\GettextTranslator::dpgettext() + */ + public function testDpgettext() + { + $translations = $this->getTranslations(); + + $translator = new GettextTranslator(); + $translator->loadTranslations($translations); + + $this->assertEquals('Translation!', $translator->dpgettext(null, null, 'test.value')); + } + + /** + * @covers \Engelsystem\Helpers\Translation\GettextTranslator::dnpgettext() + */ + public function testDnpgettext() + { + $translations = $this->getTranslations(); + + $translator = new GettextTranslator(); + $translator->loadTranslations($translations); + + $this->assertEquals('Translations!', $translator->dnpgettext(null, null, 'test.value', 'test.values', 2)); + } + + protected function getTranslations(): Translations + { + $translations = new Translations(); + $translations[] = + (new Translation(null, 'test.value', 'test.values')) + ->setTranslation('Translation!') + ->setPluralTranslations(['Translations!']); + + return $translations; + } +} diff --git a/tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php b/tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php index 171b5967..91307bdd 100644 --- a/tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php +++ b/tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php @@ -14,11 +14,14 @@ class TranslationServiceProviderTest extends ServiceProviderTest /** * @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::register() */ - public function testRegister() + public function testRegister(): void { + $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 = new Config(['locales' => $locales, 'default_locale' => $defaultLocale]); + $app = $this->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 */ @@ -27,31 +30,14 @@ class TranslationServiceProviderTest extends ServiceProviderTest /** @var TranslationServiceProvider|MockObject $serviceProvider */ $serviceProvider = $this->getMockBuilder(TranslationServiceProvider::class) ->setConstructorArgs([$app]) - ->setMethods(['initGettext', 'setLocale']) + ->setMethods(['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) @@ -65,9 +51,11 @@ class TranslationServiceProviderTest extends ServiceProviderTest ->with( Translator::class, [ - 'locale' => $locale, - 'locales' => $locales, - 'localeChangeCallback' => [$serviceProvider, 'setLocale'], + 'locale' => $locale, + 'locales' => $locales, + 'fallbackLocale' => 'en_US', + 'getTranslatorCallback' => [$serviceProvider, 'getTranslator'], + 'localeChangeCallback' => [$serviceProvider, 'setLocale'], ] ) ->willReturn($translator); @@ -81,4 +69,22 @@ class TranslationServiceProviderTest extends ServiceProviderTest $serviceProvider->register(); } + + /** + * @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getTranslator() + */ + public function testGetTranslator(): void + { + $app = $this->getApp(['get']); + $serviceProvider = new TranslationServiceProvider($app); + + $this->setExpects($app, 'get', ['path.lang'], __DIR__ . '/Assets'); + + // Get translator + $translator = $serviceProvider->getTranslator('fo_OO'); + $this->assertEquals('Foo Bar!', $translator->gettext('foo.bar')); + + // Retry from cache + $serviceProvider->getTranslator('fo_OO'); + } } diff --git a/tests/Unit/Helpers/Translation/TranslatorTest.php b/tests/Unit/Helpers/Translation/TranslatorTest.php index 7e9c534c..c173209a 100644 --- a/tests/Unit/Helpers/Translation/TranslatorTest.php +++ b/tests/Unit/Helpers/Translation/TranslatorTest.php @@ -2,6 +2,8 @@ namespace Engelsystem\Test\Unit\Helpers\Translation; +use Engelsystem\Helpers\Translation\GettextTranslator; +use Engelsystem\Helpers\Translation\TranslationNotFound; use Engelsystem\Helpers\Translation\Translator; use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit\Framework\MockObject\MockObject; @@ -19,18 +21,18 @@ class TranslatorTest extends ServiceProviderTest */ public function testInit() { - $locales = ['te_ST.ER-01' => 'Tests', 'fo_OO' => 'SomeFOO']; - $locale = 'te_ST.ER-01'; + $locales = ['te_ST' => 'Tests', 'fo_OO' => 'SomeFOO']; + $locale = 'te_ST'; - /** @var callable|MockObject $callable */ - $callable = $this->getMockBuilder(stdClass::class) + /** @var callable|MockObject $localeChange */ + $localeChange = $this->getMockBuilder(stdClass::class) ->setMethods(['__invoke']) ->getMock(); - $callable->expects($this->exactly(2)) + $localeChange->expects($this->exactly(2)) ->method('__invoke') - ->withConsecutive(['te_ST.ER-01'], ['fo_OO']); + ->withConsecutive(['te_ST'], ['fo_OO']); - $translator = new Translator($locale, $locales, $callable); + $translator = new Translator($locale, 'fo_OO', function () { }, $locales, $localeChange); $this->assertEquals($locales, $translator->getLocales()); $this->assertEquals($locale, $translator->getLocale()); @@ -43,24 +45,23 @@ class TranslatorTest extends ServiceProviderTest $this->assertEquals($newLocales, $translator->getLocales()); $this->assertTrue($translator->hasLocale('ip_SU-M')); - $this->assertFalse($translator->hasLocale('te_ST.ER-01')); + $this->assertFalse($translator->hasLocale('te_ST')); } /** - * @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']) + ->setConstructorArgs(['de_DE', 'en_US', function () { }, ['de_DE' => 'Deutsch']]) + ->setMethods(['translateText']) ->getMock(); $translator->expects($this->exactly(2)) - ->method('translateGettext') - ->withConsecutive(['Hello!'], ['My favourite number is %u!']) - ->willReturnOnConsecutiveCalls('Hallo!', 'Meine Lieblingszahl ist die %u!'); + ->method('translateText') + ->withConsecutive(['gettext', ['Hello!'], []], ['gettext', ['My favourite number is %u!'], [3]]) + ->willReturnOnConsecutiveCalls('Hallo!', 'Meine Lieblingszahl ist die 3!'); $return = $translator->translate('Hello!'); $this->assertEquals('Hallo!', $return); @@ -76,15 +77,58 @@ class TranslatorTest extends ServiceProviderTest { /** @var Translator|MockObject $translator */ $translator = $this->getMockBuilder(Translator::class) - ->setConstructorArgs(['de_DE.UTF-8', ['de_DE.UTF-8' => 'Deutsch']]) - ->setMethods(['translateGettextPlural']) + ->setConstructorArgs(['de_DE', 'en_US', function () { }, ['de_DE' => 'Deutsch']]) + ->setMethods(['translateText']) ->getMock(); $translator->expects($this->once()) - ->method('translateGettextPlural') - ->with('%s apple', '%s apples', 2) + ->method('translateText') + ->with('ngettext', ['%s apple', '%s apples', 2], [2]) ->willReturn('2 Äpfel'); $return = $translator->translatePlural('%s apple', '%s apples', 2, [2]); $this->assertEquals('2 Äpfel', $return); } + + /** + * @covers \Engelsystem\Helpers\Translation\Translator::translatePlural + * @covers \Engelsystem\Helpers\Translation\Translator::translateText + * @covers \Engelsystem\Helpers\Translation\Translator::replaceText + */ + public function testReplaceText() + { + /** @var GettextTranslator|MockObject $gtt */ + $gtt = $this->createMock(GettextTranslator::class); + /** @var callable|MockObject $getTranslator */ + $getTranslator = $this->getMockBuilder(stdClass::class) + ->setMethods(['__invoke']) + ->getMock(); + $getTranslator->expects($this->exactly(5)) + ->method('__invoke') + ->withConsecutive(['te_ST'], ['fo_OO'], ['te_ST'], ['fo_OO'], ['te_ST']) + ->willReturn($gtt); + + $i = 0; + $gtt->expects($this->exactly(4)) + ->method('gettext') + ->willReturnCallback(function () use (&$i) { + $i++; + if ($i != 4) { + throw new TranslationNotFound(); + } + + return 'Lorem %s???'; + }); + $this->setExpects($gtt, 'ngettext', ['foo.barf'], 'Lorem %s!'); + + $translator = new Translator('te_ST', 'fo_OO', $getTranslator, ['te_ST' => 'Test', 'fo_OO' => 'Foo']); + + // No translation + $this->assertEquals('foo.bar', $translator->translate('foo.bar')); + + // Fallback translation + $this->assertEquals('Lorem test2???', $translator->translate('foo.batz', ['test2'])); + + // Successful translation + $this->assertEquals('Lorem test3!', $translator->translatePlural('foo.barf', 'foo.bar2', 3, ['test3'])); + } } -- cgit v1.2.3-70-g09d2