2016-07-30 18:17:21 +03:00
|
|
|
<?php
|
|
|
|
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;
|
2016-07-30 18:17:21 +03: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());
|
2017-03-24 23:49:31 +03:00
|
|
|
$this->middleware->process(ServerRequestFactory::fromGlobals(), TestUtils::createDelegateMock()->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());
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es');
|
2017-03-24 23:49:31 +03:00
|
|
|
$this->middleware->process($request, TestUtils::createDelegateMock()->reveal());
|
2016-07-30 18:17:21 +03:00
|
|
|
$this->assertEquals('es', $this->translator->getLocale());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function localeGetsNormalized()
|
|
|
|
{
|
2017-03-24 23:49:31 +03:00
|
|
|
$delegate = TestUtils::createDelegateMock();
|
|
|
|
|
2016-07-30 18:17:21 +03:00
|
|
|
$this->assertEquals('ru', $this->translator->getLocale());
|
|
|
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es_ES');
|
2017-03-24 23:49:31 +03:00
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
2016-07-30 18:17:21 +03:00
|
|
|
$this->assertEquals('es', $this->translator->getLocale());
|
|
|
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'en-US');
|
2017-03-24 23:49:31 +03:00
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
2016-07-30 18:17:21 +03:00
|
|
|
$this->assertEquals('en', $this->translator->getLocale());
|
|
|
|
}
|
|
|
|
}
|