|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware; |
|
8
|
|
|
use ShlinkioTest\Shlink\Common\Util\TestUtils; |
|
9
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
10
|
|
|
use Zend\I18n\Translator\Translator; |
|
11
|
|
|
|
|
12
|
|
|
class LocaleMiddlewareTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var LocaleMiddleware |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $middleware; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Translator |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $translator; |
|
22
|
|
|
|
|
23
|
|
|
public function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->translator = Translator::factory(['locale' => 'ru']); |
|
26
|
|
|
$this->middleware = new LocaleMiddleware($this->translator); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @test |
|
31
|
|
|
*/ |
|
32
|
|
|
public function whenNoHeaderIsPresentLocaleIsNotChanged() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->assertEquals('ru', $this->translator->getLocale()); |
|
35
|
|
|
$this->middleware->process(ServerRequestFactory::fromGlobals(), TestUtils::createReqHandlerMock()->reveal()); |
|
36
|
|
|
$this->assertEquals('ru', $this->translator->getLocale()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @test |
|
41
|
|
|
*/ |
|
42
|
|
|
public function whenTheHeaderIsPresentLocaleIsChanged() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->assertEquals('ru', $this->translator->getLocale()); |
|
45
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es'); |
|
46
|
|
|
$this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal()); |
|
47
|
|
|
$this->assertEquals('es', $this->translator->getLocale()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @test |
|
52
|
|
|
* @dataProvider provideLanguages |
|
53
|
|
|
*/ |
|
54
|
|
|
public function localeGetsNormalized(string $lang, string $expected) |
|
55
|
|
|
{ |
|
56
|
|
|
$handler = TestUtils::createReqHandlerMock(); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertEquals('ru', $this->translator->getLocale()); |
|
59
|
|
|
|
|
60
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', $lang); |
|
61
|
|
|
$this->middleware->process($request, $handler->reveal()); |
|
62
|
|
|
$this->assertEquals($expected, $this->translator->getLocale()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function provideLanguages(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
|
|
['ru', 'ru'], |
|
69
|
|
|
['es_ES', 'es'], |
|
70
|
|
|
['en-US', 'en'], |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|