2016-07-30 17:17:21 +02:00
|
|
|
<?php
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-07-30 17:17:21 +02:00
|
|
|
namespace ShlinkioTest\Shlink\Common\Middleware;
|
|
|
|
|
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-30 17:17:21 +02:00
|
|
|
use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware;
|
2017-03-24 21:49:31 +01:00
|
|
|
use ShlinkioTest\Shlink\Common\Util\TestUtils;
|
2016-07-30 17:17:21 +02:00
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
|
use Zend\I18n\Translator\Translator;
|
|
|
|
|
|
|
|
|
|
class LocaleMiddlewareTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var LocaleMiddleware
|
|
|
|
|
*/
|
|
|
|
|
protected $middleware;
|
|
|
|
|
/**
|
|
|
|
|
* @var Translator
|
|
|
|
|
*/
|
|
|
|
|
protected $translator;
|
|
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
|
{
|
|
|
|
|
$this->translator = Translator::factory(['locale' => 'ru']);
|
|
|
|
|
$this->middleware = new LocaleMiddleware($this->translator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @test
|
|
|
|
|
*/
|
|
|
|
|
public function whenNoHeaderIsPresentLocaleIsNotChanged()
|
|
|
|
|
{
|
|
|
|
|
$this->assertEquals('ru', $this->translator->getLocale());
|
2018-03-26 19:02:41 +02:00
|
|
|
$this->middleware->process(ServerRequestFactory::fromGlobals(), TestUtils::createReqHandlerMock()->reveal());
|
2016-07-30 17:17:21 +02:00
|
|
|
$this->assertEquals('ru', $this->translator->getLocale());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @test
|
|
|
|
|
*/
|
|
|
|
|
public function whenTheHeaderIsPresentLocaleIsChanged()
|
|
|
|
|
{
|
|
|
|
|
$this->assertEquals('ru', $this->translator->getLocale());
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es');
|
2018-03-26 19:02:41 +02:00
|
|
|
$this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
|
2016-07-30 17:17:21 +02:00
|
|
|
$this->assertEquals('es', $this->translator->getLocale());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @test
|
2018-11-18 17:00:23 +01:00
|
|
|
* @dataProvider provideLanguages
|
2016-07-30 17:17:21 +02:00
|
|
|
*/
|
2018-11-18 17:00:23 +01:00
|
|
|
public function localeGetsNormalized(string $lang, string $expected)
|
2016-07-30 17:17:21 +02:00
|
|
|
{
|
2018-11-18 17:00:23 +01:00
|
|
|
$handler = TestUtils::createReqHandlerMock();
|
2017-03-24 21:49:31 +01:00
|
|
|
|
2016-07-30 17:17:21 +02:00
|
|
|
$this->assertEquals('ru', $this->translator->getLocale());
|
|
|
|
|
|
2018-11-18 17:00:23 +01:00
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', $lang);
|
|
|
|
|
$this->middleware->process($request, $handler->reveal());
|
|
|
|
|
$this->assertEquals($expected, $this->translator->getLocale());
|
|
|
|
|
}
|
2016-07-30 17:17:21 +02:00
|
|
|
|
2018-11-18 17:00:23 +01:00
|
|
|
public function provideLanguages(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
['ru', 'ru'],
|
|
|
|
|
['es_ES', 'es'],
|
|
|
|
|
['en-US', 'en'],
|
|
|
|
|
];
|
2016-07-30 17:17:21 +02:00
|
|
|
}
|
|
|
|
|
}
|