diff options
author | msquare <msquare@notrademark.de> | 2019-07-21 13:32:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-21 13:32:45 +0200 |
commit | d4d4b409b6fd96ca297af323936b8922d45b6eda (patch) | |
tree | 6c8efa1a1e429f3965820838796cb4b97cd2df11 /tests/Unit/Helpers/Translation | |
parent | d5bf7fd065a5ea93dea9fd55e6ac225ee062a3db (diff) | |
parent | 51a3c6eb44a5dbdf9d7a3cfac678f0d29b0d3eef (diff) |
Merge pull request #622 from MyIgel/controllers
AuthController (login, logout), use templating, replaced gettext, input validation
Diffstat (limited to 'tests/Unit/Helpers/Translation')
-rw-r--r-- | tests/Unit/Helpers/Translation/Assets/fo_OO/default.mo | bin | 0 -> 73 bytes | |||
-rw-r--r-- | tests/Unit/Helpers/Translation/Assets/fo_OO/default.po | 3 | ||||
-rw-r--r-- | tests/Unit/Helpers/Translation/GettextTranslatorTest.php | 67 | ||||
-rw-r--r-- | tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php | 90 | ||||
-rw-r--r-- | tests/Unit/Helpers/Translation/TranslatorTest.php | 134 |
5 files changed, 294 insertions, 0 deletions
diff --git a/tests/Unit/Helpers/Translation/Assets/fo_OO/default.mo b/tests/Unit/Helpers/Translation/Assets/fo_OO/default.mo Binary files differnew file mode 100644 index 00000000..96f1f3ca --- /dev/null +++ b/tests/Unit/Helpers/Translation/Assets/fo_OO/default.mo 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 @@ +<?php + +namespace Engelsystem\Test\Unit\Helpers\Translation; + +use Engelsystem\Helpers\Translation\GettextTranslator; +use Engelsystem\Helpers\Translation\TranslationNotFound; +use Engelsystem\Test\Unit\ServiceProviderTest; +use Gettext\Translation; +use Gettext\Translations; + +class GettextTranslatorTest extends ServiceProviderTest +{ + /** + * @covers \Engelsystem\Helpers\Translation\GettextTranslator::assertHasTranslation() + */ + public function testNoTranslation() + { + $translations = $this->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 new file mode 100644 index 00000000..91307bdd --- /dev/null +++ b/tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php @@ -0,0 +1,90 @@ +<?php + +namespace Engelsystem\Test\Unit\Helpers\Translation; + +use Engelsystem\Config\Config; +use Engelsystem\Helpers\Translation\TranslationServiceProvider; +use Engelsystem\Helpers\Translation\Translator; +use Engelsystem\Test\Unit\ServiceProviderTest; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\HttpFoundation\Session\Session; + +class TranslationServiceProviderTest extends ServiceProviderTest +{ + /** + * @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::register() + */ + 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 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(['setLocale']) + ->getMock(); + + $app->expects($this->exactly(2)) + ->method('get') + ->withConsecutive(['config'], ['session']) + ->willReturnOnConsecutiveCalls($config, $session); + + $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, + 'fallbackLocale' => 'en_US', + 'getTranslatorCallback' => [$serviceProvider, 'getTranslator'], + 'localeChangeCallback' => [$serviceProvider, 'setLocale'], + ] + ) + ->willReturn($translator); + + $app->expects($this->exactly(2)) + ->method('instance') + ->withConsecutive( + [Translator::class, $translator], + ['translator', $translator] + ); + + $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 new file mode 100644 index 00000000..c173209a --- /dev/null +++ b/tests/Unit/Helpers/Translation/TranslatorTest.php @@ -0,0 +1,134 @@ +<?php + +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; +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' => 'Tests', 'fo_OO' => 'SomeFOO']; + $locale = 'te_ST'; + + /** @var callable|MockObject $localeChange */ + $localeChange = $this->getMockBuilder(stdClass::class) + ->setMethods(['__invoke']) + ->getMock(); + $localeChange->expects($this->exactly(2)) + ->method('__invoke') + ->withConsecutive(['te_ST'], ['fo_OO']); + + $translator = new Translator($locale, 'fo_OO', function () { }, $locales, $localeChange); + + $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')); + } + + /** + * @covers \Engelsystem\Helpers\Translation\Translator::translate + */ + public function testTranslate() + { + /** @var Translator|MockObject $translator */ + $translator = $this->getMockBuilder(Translator::class) + ->setConstructorArgs(['de_DE', 'en_US', function () { }, ['de_DE' => 'Deutsch']]) + ->setMethods(['translateText']) + ->getMock(); + $translator->expects($this->exactly(2)) + ->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); + + $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', 'en_US', function () { }, ['de_DE' => 'Deutsch']]) + ->setMethods(['translateText']) + ->getMock(); + $translator->expects($this->once()) + ->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'])); + } +} |