2018-08-28 22:23:59 +02:00
|
|
|
<?php
|
|
|
|
|
2019-07-08 01:31:59 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Helpers\Translation;
|
2018-08-28 22:23:59 +02:00
|
|
|
|
2019-07-08 01:47:01 +02:00
|
|
|
use Engelsystem\Helpers\Translation\GettextTranslator;
|
|
|
|
use Engelsystem\Helpers\Translation\TranslationNotFound;
|
2019-07-08 01:31:59 +02:00
|
|
|
use Engelsystem\Helpers\Translation\Translator;
|
2018-08-28 22:23:59 +02:00
|
|
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use stdClass;
|
|
|
|
|
|
|
|
class TranslatorTest extends ServiceProviderTest
|
|
|
|
{
|
|
|
|
/**
|
2019-07-08 01:31:59 +02:00
|
|
|
* @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
|
2018-08-28 22:23:59 +02:00
|
|
|
*/
|
|
|
|
public function testInit()
|
|
|
|
{
|
2019-07-08 01:47:01 +02:00
|
|
|
$locales = ['te_ST' => 'Tests', 'fo_OO' => 'SomeFOO'];
|
|
|
|
$locale = 'te_ST';
|
2018-08-28 22:23:59 +02:00
|
|
|
|
2019-07-08 01:47:01 +02:00
|
|
|
/** @var callable|MockObject $localeChange */
|
|
|
|
$localeChange = $this->getMockBuilder(stdClass::class)
|
2019-11-06 12:29:58 +01:00
|
|
|
->addMethods(['__invoke'])
|
2018-08-28 22:23:59 +02:00
|
|
|
->getMock();
|
2019-07-08 01:47:01 +02:00
|
|
|
$localeChange->expects($this->exactly(2))
|
2018-08-28 22:23:59 +02:00
|
|
|
->method('__invoke')
|
2019-07-08 01:47:01 +02:00
|
|
|
->withConsecutive(['te_ST'], ['fo_OO']);
|
2018-08-28 22:23:59 +02:00
|
|
|
|
2019-11-10 23:26:23 +01:00
|
|
|
$translator = new Translator($locale, 'fo_OO', [$this, 'doNothing'], $locales, $localeChange);
|
2018-08-28 22:23:59 +02:00
|
|
|
|
|
|
|
$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'));
|
2019-07-08 01:47:01 +02:00
|
|
|
$this->assertFalse($translator->hasLocale('te_ST'));
|
2018-08-28 22:23:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-08 01:31:59 +02:00
|
|
|
* @covers \Engelsystem\Helpers\Translation\Translator::translate
|
2018-08-28 22:23:59 +02:00
|
|
|
*/
|
|
|
|
public function testTranslate()
|
|
|
|
{
|
|
|
|
/** @var Translator|MockObject $translator */
|
|
|
|
$translator = $this->getMockBuilder(Translator::class)
|
2019-11-10 23:26:23 +01:00
|
|
|
->setConstructorArgs(['de_DE', 'en_US', [$this, 'doNothing'], ['de_DE' => 'Deutsch']])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['translateText'])
|
2018-08-28 22:23:59 +02:00
|
|
|
->getMock();
|
2018-10-18 23:34:18 +02:00
|
|
|
$translator->expects($this->exactly(2))
|
2019-07-08 01:47:01 +02:00
|
|
|
->method('translateText')
|
|
|
|
->withConsecutive(['gettext', ['Hello!'], []], ['gettext', ['My favourite number is %u!'], [3]])
|
|
|
|
->willReturnOnConsecutiveCalls('Hallo!', 'Meine Lieblingszahl ist die 3!');
|
2018-10-18 23:34:18 +02:00
|
|
|
|
|
|
|
$return = $translator->translate('Hello!');
|
|
|
|
$this->assertEquals('Hallo!', $return);
|
2018-08-28 22:23:59 +02:00
|
|
|
|
|
|
|
$return = $translator->translate('My favourite number is %u!', [3]);
|
|
|
|
$this->assertEquals('Meine Lieblingszahl ist die 3!', $return);
|
|
|
|
}
|
2018-10-18 23:34:18 +02:00
|
|
|
|
|
|
|
/**
|
2019-07-08 01:31:59 +02:00
|
|
|
* @covers \Engelsystem\Helpers\Translation\Translator::translatePlural
|
2018-10-18 23:34:18 +02:00
|
|
|
*/
|
|
|
|
public function testTranslatePlural()
|
|
|
|
{
|
|
|
|
/** @var Translator|MockObject $translator */
|
|
|
|
$translator = $this->getMockBuilder(Translator::class)
|
2019-11-10 23:26:23 +01:00
|
|
|
->setConstructorArgs(['de_DE', 'en_US', [$this, 'doNothing'], ['de_DE' => 'Deutsch']])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['translateText'])
|
2018-10-18 23:34:18 +02:00
|
|
|
->getMock();
|
|
|
|
$translator->expects($this->once())
|
2019-07-08 01:47:01 +02:00
|
|
|
->method('translateText')
|
|
|
|
->with('ngettext', ['%s apple', '%s apples', 2], [2])
|
2018-10-18 23:34:18 +02:00
|
|
|
->willReturn('2 Äpfel');
|
|
|
|
|
|
|
|
$return = $translator->translatePlural('%s apple', '%s apples', 2, [2]);
|
|
|
|
$this->assertEquals('2 Äpfel', $return);
|
|
|
|
}
|
2019-07-08 01:47:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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)
|
2019-11-06 12:29:58 +01:00
|
|
|
->addMethods(['__invoke'])
|
2019-07-08 01:47:01 +02:00
|
|
|
->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']));
|
|
|
|
}
|
2019-11-10 23:26:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does nothing
|
|
|
|
*/
|
|
|
|
public function doNothing()
|
|
|
|
{
|
|
|
|
}
|
2018-08-28 22:23:59 +02:00
|
|
|
}
|