-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
SiteLocaleTest.php
222 lines (187 loc) · 7.33 KB
/
SiteLocaleTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
* UserFrosting (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/UserFrosting
* @copyright Copyright (c) 2019 Alexander Weissman
* @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\Sprinkle\Core\Tests\Integration\I18n;
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use UserFrosting\I18n\Locale;
use UserFrosting\Sprinkle\Core\I18n\SiteLocale;
use UserFrosting\Tests\TestCase;
/**
* SiteLocaleTest class.
* Tests SiteLocale
*/
class SiteLocaleTest extends TestCase
{
use MockeryPHPUnitIntegration;
protected $testLocale = [
'fr_FR' => 'french', // Legacy setting
'en_US' => true,
'es_ES' => false,
'it_IT' => null, // Legacy setting
];
// Apply fake config
public function setUp(): void
{
parent::setUp();
$this->ci->config['site.locales.available'] = $this->testLocale;
}
/**
* {@inheritdoc}
*/
public function tearDown(): void
{
parent::tearDown();
m::close();
}
public function testService(): void
{
$this->assertInstanceOf(SiteLocale::class, $this->ci->locale);
}
public function testFakeConfig(): void
{
$this->assertSame($this->testLocale, $this->ci->config['site.locales.available']);
}
/**
* @depends testFakeConfig
*/
public function testGetAvailableIdentifiers(): void
{
$this->assertSame([
'fr_FR',
'en_US',
], $this->ci->locale->getAvailableIdentifiers());
}
/**
* @depends testGetAvailableIdentifiers
*/
public function testgetAvailable(): void
{
$locales = $this->ci->locale->getAvailable();
$this->assertIsArray($locales);
$this->assertCount(2, $locales);
$this->assertInstanceOf(Locale::class, $locales[0]);
}
/**
* @depends testgetAvailable
*/
public function testgetAvailableOptions(): void
{
// Implement fake locale file location
/** @var \UserFrosting\UniformResourceLocator\ResourceLocator $locator */
$locator = $this->ci->locator;
$locator->removeStream('locale')->registerStream('locale', '', __DIR__ . '/data', true);
// Set expectations. Note the sort applied here
$expected = [
'en_US' => 'English',
'fr_FR' => 'Tomato', // Just to be sure the fake locale are loaded ;)
];
$options = $this->ci->locale->getAvailableOptions();
$this->assertIsArray($options);
$this->assertSame($expected, $options);
}
/**
* @depends testGetAvailableIdentifiers
*/
public function testIsAvailable(): void
{
$this->assertFalse($this->ci->locale->isAvailable('ZZ_zz'));
$this->assertFalse($this->ci->locale->isAvailable('es_ES'));
$this->assertTrue($this->ci->locale->isAvailable('en_US'));
}
/**
* Will return the default locale (fr_FR)
*/
public function testGetLocaleIndentifier(): void
{
$this->ci->config['site.locales.default'] = 'fr_FR';
$this->assertSame('fr_FR', $this->ci->locale->getLocaleIndentifier());
}
/**
* Will return en_US
*/
public function testGetLocaleIndentifierWithDefaultIndentifier(): void
{
$this->ci->config['site.locales.default'] = '';
$this->assertSame('en_US', $this->ci->locale->getLocaleIndentifier());
}
/**
* Will return en_US
*/
public function testGetLocaleIndentifierWithNonStringIndentifier(): void
{
$this->ci->config['site.locales.default'] = ['foo', 'bar'];
$this->assertSame('en_US', $this->ci->locale->getLocaleIndentifier());
}
/**
* Test old method of defining the default locale
*/
public function testGetLocaleIndentifierWithCommaSeparatedString(): void
{
$this->ci->config['site.locales.default'] = 'fr_FR, en_US';
$this->assertSame('fr_FR, en_US', $this->ci->locale->getLocaleIndentifier());
}
/**
* Test old method of defining the default locale
*/
public function testGetLocaleIndentifierWithCommaSeparatedStringReverseOrder(): void
{
$this->ci->config['site.locales.default'] = 'en_US,fr_FR';
$this->assertSame('en_US,fr_FR', $this->ci->locale->getLocaleIndentifier());
}
public function testGetLocaleIndentifierWithBrowserAndComplexLocale(): void
{
$request = m::mock(\Psr\Http\Message\ServerRequestInterface::class);
$request->shouldReceive('hasHeader')->with('Accept-Language')->once()->andReturn(true);
$request->shouldReceive('getHeaderLine')->with('Accept-Language')->once()->andReturn('en-US, en;q=0.9, fr;q=0.8, de;q=0.7, *;q=0.5');
$this->ci->config['site.locales.default'] = 'fr_FR';
$this->ci->request = $request;
$service = $this->ci->locale;
$this->assertSame('en_US', $service->getLocaleIndentifier());
}
public function testGetLocaleIndentifierWithBrowserAndMultipleLocale(): void
{
$request = m::mock(\Psr\Http\Message\ServerRequestInterface::class);
$request->shouldReceive('hasHeader')->with('Accept-Language')->once()->andReturn(true);
$request->shouldReceive('getHeaderLine')->with('Accept-Language')->once()->andReturn('es-ES, fr-FR;q=0.7, fr-CA;q=0.9, en-US;q=0.8, *;q=0.5');
$this->ci->config['site.locales.default'] = 'fr_FR';
$this->ci->request = $request;
$service = $this->ci->locale;
$this->assertSame('en_US', $service->getLocaleIndentifier());
}
public function testGetLocaleIndentifierWithBrowserAndLocaleInSecondPlace(): void
{
$request = m::mock(\Psr\Http\Message\ServerRequestInterface::class);
$request->shouldReceive('hasHeader')->with('Accept-Language')->once()->andReturn(true);
$request->shouldReceive('getHeaderLine')->with('Accept-Language')->once()->andReturn('zz-ZZ, en-US;q=0.9, fr;q=0.8, de;q=0.7, *;q=0.5');
$this->ci->config['site.locales.default'] = 'fr_FR';
$this->ci->request = $request;
$service = $this->ci->locale;
$this->assertSame('en_US', $service->getLocaleIndentifier());
}
public function testGetLocaleIndentifierWithBrowserAndInvalidLocale(): void
{
$request = m::mock(\Psr\Http\Message\ServerRequestInterface::class);
$request->shouldReceive('hasHeader')->with('Accept-Language')->once()->andReturn(true);
$request->shouldReceive('getHeaderLine')->with('Accept-Language')->once()->andReturn('fo,oba;;;r,');
$this->ci->config['site.locales.default'] = 'fr_FR';
$this->ci->request = $request;
$service = $this->ci->locale;
$this->assertSame('fr_FR', $service->getLocaleIndentifier());
}
public function testGetLocaleIndentifierWithBrowserAndNonExistingLocale(): void
{
$request = m::mock(\Psr\Http\Message\ServerRequestInterface::class);
$request->shouldReceive('hasHeader')->with('Accept-Language')->once()->andReturn(true);
$request->shouldReceive('getHeaderLine')->with('Accept-Language')->once()->andReturn('fr-ca');
$this->ci->config['site.locales.default'] = 'fr_FR';
$this->ci->request = $request;
$service = $this->ci->locale;
$this->assertSame('fr_FR', $service->getLocaleIndentifier());
}
}