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