summaryrefslogtreecommitdiff
path: root/tests/Unit
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-08-28 22:23:59 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-08-29 23:46:31 +0200
commit427315195bdd379a0207fc9b2aaf69a5b5b86c79 (patch)
tree678245351333a16c5a5bca129aada4bfd311c23c /tests/Unit
parentdf6360044b5c2396b2bee0dfa9e8d744bfa424d5 (diff)
Moved translation/internationalization to Helpers\Translator class
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/Helpers/TranslationServiceProviderTest.php86
-rw-r--r--tests/Unit/Helpers/TranslatorTest.php69
-rw-r--r--tests/Unit/HelpersTest.php26
-rw-r--r--tests/Unit/Middleware/SetLocaleTest.php71
4 files changed, 251 insertions, 1 deletions
diff --git a/tests/Unit/Helpers/TranslationServiceProviderTest.php b/tests/Unit/Helpers/TranslationServiceProviderTest.php
new file mode 100644
index 00000000..c5ba7386
--- /dev/null
+++ b/tests/Unit/Helpers/TranslationServiceProviderTest.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Helpers;
+
+use Engelsystem\Config\Config;
+use Engelsystem\Helpers\TranslationServiceProvider;
+use Engelsystem\Helpers\Translator;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use PHPUnit\Framework\MockObject\MockObject;
+use Symfony\Component\HttpFoundation\Session\Session;
+
+class TranslationServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Helpers\TranslationServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ $app = $this->getApp(['make', 'instance', 'get']);
+ /** @var Config|MockObject $config */
+ $config = $this->createMock(Config::class);
+ /** @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(['initGettext', 'setLocale'])
+ ->getMock();
+
+ $serviceProvider->expects($this->once())
+ ->method('initGettext');
+
+ $app->expects($this->exactly(2))
+ ->method('get')
+ ->withConsecutive(['config'], ['session'])
+ ->willReturnOnConsecutiveCalls($config, $session);
+
+ $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->expects($this->exactly(2))
+ ->method('get')
+ ->withConsecutive(
+ ['locales'],
+ ['default_locale']
+ )
+ ->willReturnOnConsecutiveCalls(
+ $locales,
+ $defaultLocale
+ );
+
+ $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,
+ 'localeChangeCallback' => [$serviceProvider, 'setLocale']
+ ]
+ )
+ ->willReturn($translator);
+
+ $app->expects($this->exactly(2))
+ ->method('instance')
+ ->withConsecutive(
+ [Translator::class, $translator],
+ ['translator', $translator]
+ );
+
+ $serviceProvider->register();
+ }
+}
+
+
diff --git a/tests/Unit/Helpers/TranslatorTest.php b/tests/Unit/Helpers/TranslatorTest.php
new file mode 100644
index 00000000..396d2b65
--- /dev/null
+++ b/tests/Unit/Helpers/TranslatorTest.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Helpers;
+
+use Engelsystem\Helpers\Translator;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use PHPUnit\Framework\MockObject\MockObject;
+use stdClass;
+
+class TranslatorTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Helpers\Translator::__construct()
+ * @covers \Engelsystem\Helpers\Translator::setLocale()
+ * @covers \Engelsystem\Helpers\Translator::setLocales()
+ * @covers \Engelsystem\Helpers\Translator::getLocale()
+ * @covers \Engelsystem\Helpers\Translator::getLocales()
+ * @covers \Engelsystem\Helpers\Translator::hasLocale()
+ */
+ public function testInit()
+ {
+ $locales = ['te_ST.ER-01' => 'Tests', 'fo_OO' => 'SomeFOO'];
+ $locale = 'te_ST.ER-01';
+
+ /** @var callable|MockObject $callable */
+ $callable = $this->getMockBuilder(stdClass::class)
+ ->setMethods(['__invoke'])
+ ->getMock();
+ $callable->expects($this->exactly(2))
+ ->method('__invoke')
+ ->withConsecutive(['te_ST.ER-01'], ['fo_OO']);
+
+ $translator = new Translator($locale, $locales, $callable);
+
+ $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.ER-01'));
+ }
+
+ /**
+ * @covers \Engelsystem\Helpers\Translator::translate()
+ */
+ public function testTranslate()
+ {
+ /** @var Translator|MockObject $translator */
+ $translator = $this->getMockBuilder(Translator::class)
+ ->setConstructorArgs(['de_DE.UTF-8', ['de_DE.UTF-8' => 'Deutsch']])
+ ->setMethods(['translateGettext'])
+ ->getMock();
+ $translator->expects($this->once())
+ ->method('translateGettext')
+ ->with('My favourite number is %u!')
+ ->willReturn('Meine Lieblingszahl ist die %u!');
+
+ $return = $translator->translate('My favourite number is %u!', [3]);
+ $this->assertEquals('Meine Lieblingszahl ist die 3!', $return);
+ }
+}
+
+
diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php
index b59b11ee..20d3c2de 100644
--- a/tests/Unit/HelpersTest.php
+++ b/tests/Unit/HelpersTest.php
@@ -5,10 +5,11 @@ namespace Engelsystem\Test\Unit;
use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Container\Container;
+use Engelsystem\Helpers\Translator;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
-use Engelsystem\Renderer\Renderer;
use Engelsystem\Http\UrlGenerator;
+use Engelsystem\Renderer\Renderer;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Symfony\Component\HttpFoundation\Session\Session;
@@ -195,6 +196,29 @@ class HelpersTest extends TestCase
}
/**
+ * @covers \__
+ * @covers \trans
+ */
+ public function testTrans()
+ {
+ /** @var Translator|MockObject $translator */
+ $translator = $this->getMockBuilder(Translator::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->getAppMock('translator', $translator);
+
+ $translator->expects($this->exactly(2))
+ ->method('translate')
+ ->with('Lorem %s Ipsum', ['foo'])
+ ->willReturn('Lorem foo Ipsum');
+
+ $this->assertEquals($translator, trans());
+ $this->assertEquals('Lorem foo Ipsum', trans('Lorem %s Ipsum', ['foo']));
+ $this->assertEquals('Lorem foo Ipsum', __('Lorem %s Ipsum', ['foo']));
+ }
+
+ /**
* @covers \url
*/
public function testUrl()
diff --git a/tests/Unit/Middleware/SetLocaleTest.php b/tests/Unit/Middleware/SetLocaleTest.php
new file mode 100644
index 00000000..c4e9d2a4
--- /dev/null
+++ b/tests/Unit/Middleware/SetLocaleTest.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Helpers\Translator;
+use Engelsystem\Middleware\SetLocale;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use Symfony\Component\HttpFoundation\Session\Session;
+
+class SetLocaleTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\SetLocale::__construct
+ * @covers \Engelsystem\Middleware\SetLocale::process
+ */
+ public function testRegister()
+ {
+ /** @var Translator|MockObject $translator */
+ $translator = $this->createMock(Translator::class);
+ /** @var Session|MockObject $session */
+ $session = $this->createMock(Session::class);
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
+ /** @var RequestHandlerInterface|MockObject $handler */
+ $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
+ /** @var ResponseInterface|MockObject $response */
+ $response = $this->getMockForAbstractClass(ResponseInterface::class);
+
+ $locale = 'te_ST.UTF8';
+
+ $request->expects($this->exactly(3))
+ ->method('getQueryParams')
+ ->willReturnOnConsecutiveCalls(
+ [],
+ ['set-locale' => 'en_US.UTF8'],
+ ['set-locale' => $locale]
+ );
+
+ $translator->expects($this->exactly(2))
+ ->method('hasLocale')
+ ->withConsecutive(
+ ['en_US.UTF8'],
+ [$locale]
+ )
+ ->willReturnOnConsecutiveCalls(
+ false,
+ true
+ );
+ $translator->expects($this->once())
+ ->method('setLocale')
+ ->with($locale);
+
+ $session->expects($this->once())
+ ->method('set')
+ ->with('locale', $locale);
+
+ $handler->expects($this->exactly(3))
+ ->method('handle')
+ ->with($request)
+ ->willReturn($response);
+
+ $middleware = new SetLocale($translator, $session);
+ $middleware->process($request, $handler);
+ $middleware->process($request, $handler);
+ $middleware->process($request, $handler);
+ }
+}