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
|
|
|
|
|
|
|
use Engelsystem\Config\Config;
|
2019-07-08 01:31:59 +02:00
|
|
|
use Engelsystem\Helpers\Translation\TranslationServiceProvider;
|
|
|
|
use Engelsystem\Helpers\Translation\Translator;
|
2021-01-02 17:05:18 +01:00
|
|
|
use Engelsystem\Http\Request;
|
2018-08-28 22:23:59 +02:00
|
|
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-10-25 09:33:09 +02:00
|
|
|
use PHPUnit\Framework\MockObject\Rule\InvokedCount;
|
2018-08-28 22:23:59 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
|
|
|
|
|
class TranslationServiceProviderTest extends ServiceProviderTest
|
|
|
|
{
|
|
|
|
/**
|
2019-10-09 13:54:04 +02:00
|
|
|
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::register
|
2018-08-28 22:23:59 +02:00
|
|
|
*/
|
2019-07-08 01:47:01 +02:00
|
|
|
public function testRegister(): void
|
2018-08-28 22:23:59 +02:00
|
|
|
{
|
2021-01-02 17:05:18 +01:00
|
|
|
$locale = 'te_ST';
|
|
|
|
$locales = ['fo_OO' => 'Foo', 'fo_OO.BAR' => 'Foo (Bar)', 'te_ST' => 'WTF\'s Testing?'];
|
|
|
|
$config = new Config(['locales' => $locales, 'default_locale' => 'fo_OO']);
|
2019-07-08 01:47:01 +02:00
|
|
|
|
2019-10-06 21:03:20 +02:00
|
|
|
$app = $this->getApp(['make', 'singleton', 'alias', 'get']);
|
2018-08-28 22:23:59 +02:00
|
|
|
/** @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])
|
2019-10-09 13:54:04 +02:00
|
|
|
->onlyMethods(['setLocale'])
|
2018-08-28 22:23:59 +02:00
|
|
|
->getMock();
|
|
|
|
|
2021-01-02 17:05:18 +01:00
|
|
|
$request = (new Request())->withAddedHeader('Accept-Language', 'te_ST');
|
|
|
|
|
|
|
|
$app->expects($this->exactly(3))
|
2018-08-28 22:23:59 +02:00
|
|
|
->method('get')
|
2021-01-02 17:05:18 +01:00
|
|
|
->withConsecutive(['config'], ['session'], ['request'])
|
|
|
|
->willReturnOnConsecutiveCalls($config, $session, $request);
|
2018-08-28 22:23:59 +02:00
|
|
|
|
|
|
|
$session->expects($this->once())
|
|
|
|
->method('get')
|
2021-01-02 17:05:18 +01:00
|
|
|
->with('locale', 'te_ST')
|
2018-08-28 22:23:59 +02:00
|
|
|
->willReturn($locale);
|
|
|
|
$session->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with('locale', $locale);
|
|
|
|
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('make')
|
|
|
|
->with(
|
|
|
|
Translator::class,
|
|
|
|
[
|
2019-07-08 01:47:01 +02:00
|
|
|
'locale' => $locale,
|
|
|
|
'locales' => $locales,
|
|
|
|
'fallbackLocale' => 'en_US',
|
|
|
|
'getTranslatorCallback' => [$serviceProvider, 'getTranslator'],
|
|
|
|
'localeChangeCallback' => [$serviceProvider, 'setLocale'],
|
2018-08-28 22:23:59 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
->willReturn($translator);
|
|
|
|
|
2019-10-06 21:03:20 +02:00
|
|
|
$app->expects($this->once())
|
|
|
|
->method('singleton')
|
2022-12-14 19:15:20 +01:00
|
|
|
->willReturnCallback(function (string $abstract, callable $callback) use ($translator): void {
|
2019-10-06 21:03:20 +02:00
|
|
|
$this->assertEquals(Translator::class, $abstract);
|
|
|
|
$this->assertEquals($translator, $callback());
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('alias')
|
|
|
|
->with(Translator::class, 'translator');
|
2018-08-28 22:23:59 +02:00
|
|
|
|
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
2019-07-08 01:47:01 +02:00
|
|
|
|
|
|
|
/**
|
2019-10-09 13:54:04 +02:00
|
|
|
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getTranslator
|
2019-07-08 01:47:01 +02:00
|
|
|
*/
|
|
|
|
public function testGetTranslator(): void
|
|
|
|
{
|
|
|
|
$app = $this->getApp(['get']);
|
|
|
|
$serviceProvider = new TranslationServiceProvider($app);
|
|
|
|
|
2019-10-25 09:33:09 +02:00
|
|
|
$this->setExpects($app, 'get', ['path.lang'], __DIR__ . '/Assets', new InvokedCount(2));
|
2019-07-08 01:47:01 +02:00
|
|
|
|
|
|
|
// Get translator
|
|
|
|
$translator = $serviceProvider->getTranslator('fo_OO');
|
|
|
|
$this->assertEquals('Foo Bar!', $translator->gettext('foo.bar'));
|
2019-10-25 09:33:09 +02:00
|
|
|
$this->assertEquals('Foo Bar required!', $translator->gettext('validation.foo.bar'));
|
2019-07-08 01:47:01 +02:00
|
|
|
|
|
|
|
// Retry from cache
|
|
|
|
$serviceProvider->getTranslator('fo_OO');
|
|
|
|
}
|
2019-10-09 13:54:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getTranslator
|
|
|
|
* @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getFile
|
|
|
|
*/
|
|
|
|
public function testGetTranslatorFromPo(): void
|
|
|
|
{
|
|
|
|
$app = $this->getApp(['get']);
|
2019-10-25 09:33:09 +02:00
|
|
|
$this->setExpects($app, 'get', ['path.lang'], __DIR__ . '/Assets', new InvokedCount(2));
|
2019-10-09 13:54:04 +02:00
|
|
|
|
|
|
|
$serviceProvider = new TranslationServiceProvider($app);
|
|
|
|
|
|
|
|
// Get translator using a .po file
|
|
|
|
$translator = $serviceProvider->getTranslator('ba_RR');
|
|
|
|
$this->assertEquals('B Arr!', $translator->gettext('foo.bar'));
|
2019-10-25 09:33:09 +02:00
|
|
|
$this->assertEquals('B Arr required!', $translator->gettext('validation.foo.bar'));
|
2019-10-09 13:54:04 +02:00
|
|
|
}
|
2018-08-28 22:23:59 +02:00
|
|
|
}
|