2019-07-08 01:47:01 +02:00
|
|
|
<?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()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testNoTranslation(): void
|
2019-07-08 01:47:01 +02:00
|
|
|
{
|
|
|
|
$translations = $this->getTranslations();
|
2020-04-20 02:02:26 +02:00
|
|
|
$translator = GettextTranslator::createFromTranslations($translations);
|
2019-07-08 01:47:01 +02:00
|
|
|
|
|
|
|
$this->assertEquals('Translation!', $translator->gettext('test.value'));
|
|
|
|
|
|
|
|
$this->expectException(TranslationNotFound::class);
|
|
|
|
$this->expectExceptionMessage('//foo.bar');
|
|
|
|
|
|
|
|
$translator->gettext('foo.bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 02:02:26 +02:00
|
|
|
* @covers \Engelsystem\Helpers\Translation\GettextTranslator::translate()
|
2019-07-08 01:47:01 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testTranslate(): void
|
2019-07-08 01:47:01 +02:00
|
|
|
{
|
|
|
|
$translations = $this->getTranslations();
|
2020-04-20 02:02:26 +02:00
|
|
|
$translator = GettextTranslator::createFromTranslations($translations);
|
2019-07-08 01:47:01 +02:00
|
|
|
|
2020-04-20 02:02:26 +02:00
|
|
|
$this->assertEquals('Translation!', $translator->gettext('test.value'));
|
2019-07-08 01:47:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 02:02:26 +02:00
|
|
|
* @covers \Engelsystem\Helpers\Translation\GettextTranslator::translatePlural
|
2019-07-08 01:47:01 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testTranslatePlural(): void
|
2019-07-08 01:47:01 +02:00
|
|
|
{
|
|
|
|
$translations = $this->getTranslations();
|
2020-04-20 02:02:26 +02:00
|
|
|
$translator = GettextTranslator::createFromTranslations($translations);
|
2019-07-08 01:47:01 +02:00
|
|
|
|
2020-04-20 02:02:26 +02:00
|
|
|
$this->assertEquals('Translations!', $translator->ngettext('test.value', 'test.value', 2));
|
2019-07-08 01:47:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTranslations(): Translations
|
|
|
|
{
|
2020-04-20 02:02:26 +02:00
|
|
|
$translation = Translation::create(null, 'test.value')
|
|
|
|
->translate('Translation!')
|
|
|
|
->translatePlural('Translations!');
|
|
|
|
|
|
|
|
$translations = Translations::create();
|
|
|
|
$translations->add($translation);
|
2019-07-08 01:47:01 +02:00
|
|
|
|
|
|
|
return $translations;
|
|
|
|
}
|
|
|
|
}
|