engelsystem/tests/Unit/Helpers/Translation/GettextTranslatorTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.9 KiB
PHP
Raw Normal View History

<?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(): void
{
$translations = $this->getTranslations();
2020-04-20 02:02:26 +02:00
$translator = GettextTranslator::createFromTranslations($translations);
$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()
*/
2020-04-20 02:02:26 +02:00
public function testTranslate(): void
{
$translations = $this->getTranslations();
2020-04-20 02:02:26 +02:00
$translator = GettextTranslator::createFromTranslations($translations);
2020-04-20 02:02:26 +02:00
$this->assertEquals('Translation!', $translator->gettext('test.value'));
}
/**
2020-04-20 02:02:26 +02:00
* @covers \Engelsystem\Helpers\Translation\GettextTranslator::translatePlural
*/
2020-04-20 02:02:26 +02:00
public function testTranslatePlural(): void
{
$translations = $this->getTranslations();
2020-04-20 02:02:26 +02:00
$translator = GettextTranslator::createFromTranslations($translations);
2020-04-20 02:02:26 +02:00
$this->assertEquals('Translations!', $translator->ngettext('test.value', 'test.value', 2));
}
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);
return $translations;
}
}