1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Dimsav\Translatable\Locales; |
4
|
|
|
|
5
|
|
|
class LocalesTest extends TestsBase |
6
|
|
|
{ |
7
|
|
|
public function test_singleton() |
8
|
|
|
{ |
9
|
|
|
$this->assertSame(spl_object_id($this->app->make('translatable.locales')), spl_object_id($this->app->make('translatable.locales'))); |
10
|
|
|
$this->assertSame(spl_object_id($this->app->make(Locales::class)), spl_object_id($this->app->make(Locales::class))); |
11
|
|
|
$this->assertSame(spl_object_id($this->app->make('translatable.locales')), spl_object_id($this->app->make(Locales::class))); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public function test_load() |
15
|
|
|
{ |
16
|
|
|
$this->app['config']->set('translatable.locales', [ |
17
|
|
|
'de', |
18
|
|
|
]); |
19
|
|
|
$this->app->make('translatable.locales')->load(); |
20
|
|
|
$this->assertEquals(['de'], $this->app->make('translatable.locales')->all()); |
21
|
|
|
|
22
|
|
|
$this->app['config']->set('translatable.locales', [ |
23
|
|
|
'de', |
24
|
|
|
'en', |
25
|
|
|
]); |
26
|
|
|
$this->assertEquals(['de'], $this->app->make('translatable.locales')->all()); |
27
|
|
|
$this->app->make('translatable.locales')->load(); |
28
|
|
|
$this->assertEquals(['de', 'en'], $this->app->make('translatable.locales')->all()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function test_all_language_locales() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$this->app['config']->set('translatable.locales', [ |
34
|
|
|
'el', |
35
|
|
|
'en', |
36
|
|
|
'fr', |
37
|
|
|
'de', |
38
|
|
|
'id', |
39
|
|
|
]); |
40
|
|
|
$this->app->make('translatable.locales')->load(); |
41
|
|
|
|
42
|
|
|
$this->assertEquals(['el', 'en', 'fr', 'de', 'id'], $this->app->make('translatable.locales')->all()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function test_all_country_locales() |
46
|
|
|
{ |
47
|
|
|
$this->app['config']->set('translatable.locales', [ |
48
|
|
|
'en' => [ |
49
|
|
|
'GB', |
50
|
|
|
'US', |
51
|
|
|
], |
52
|
|
|
'de' => [ |
53
|
|
|
'DE', |
54
|
|
|
'CH', |
55
|
|
|
], |
56
|
|
|
]); |
57
|
|
|
$this->app->make('translatable.locales')->load(); |
58
|
|
|
|
59
|
|
|
$this->assertEquals(['en', 'en-GB', 'en-US', 'de', 'de-DE', 'de-CH'], $this->app->make('translatable.locales')->all()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function test_to_array() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->app['config']->set('translatable.locales', [ |
65
|
|
|
'el', |
66
|
|
|
'en', |
67
|
|
|
'fr', |
68
|
|
|
'de', |
69
|
|
|
'id', |
70
|
|
|
]); |
71
|
|
|
$this->app->make('translatable.locales')->load(); |
72
|
|
|
|
73
|
|
|
$this->assertEquals(['el', 'en', 'fr', 'de', 'id'], $this->app->make('translatable.locales')->toArray()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function test_current_config() |
77
|
|
|
{ |
78
|
|
|
$this->app['config']->set('translatable.locale', 'de'); |
79
|
|
|
|
80
|
|
|
$this->assertEquals('de', $this->app->make('translatable.locales')->current()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function test_current_translator() |
84
|
|
|
{ |
85
|
|
|
$this->app['config']->set('translatable.locale', null); |
86
|
|
|
$this->app['translator']->setLocale('en'); |
87
|
|
|
|
88
|
|
|
$this->assertEquals('en', $this->app->make('translatable.locales')->current()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function test_has() |
92
|
|
|
{ |
93
|
|
|
$this->app['config']->set('translatable.locales', [ |
94
|
|
|
'el', |
95
|
|
|
'en', |
96
|
|
|
'fr', |
97
|
|
|
'de', |
98
|
|
|
'id', |
99
|
|
|
]); |
100
|
|
|
$this->app->make('translatable.locales')->load(); |
101
|
|
|
|
102
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('de')); |
103
|
|
|
$this->assertFalse($this->app->make('translatable.locales')->has('jp')); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
public function test_offset_exists() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$this->app['config']->set('translatable.locales', [ |
109
|
|
|
'el', |
110
|
|
|
'en', |
111
|
|
|
'fr', |
112
|
|
|
'de', |
113
|
|
|
'id', |
114
|
|
|
]); |
115
|
|
|
$this->app->make('translatable.locales')->load(); |
116
|
|
|
|
117
|
|
|
$this->assertTrue(isset($this->app->make('translatable.locales')['de'])); |
118
|
|
|
$this->assertFalse(isset($this->app->make('translatable.locales')['jp'])); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function test_get() |
122
|
|
|
{ |
123
|
|
|
$this->app['config']->set('translatable.locales', [ |
124
|
|
|
'el', |
125
|
|
|
'en', |
126
|
|
|
'fr', |
127
|
|
|
'de', |
128
|
|
|
'id', |
129
|
|
|
]); |
130
|
|
|
$this->app->make('translatable.locales')->load(); |
131
|
|
|
|
132
|
|
|
$this->assertEquals('de', $this->app->make('translatable.locales')->get('de')); |
133
|
|
|
$this->assertNull($this->app->make('translatable.locales')->get('jp')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
public function test_offset_get() |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$this->app['config']->set('translatable.locales', [ |
139
|
|
|
'el', |
140
|
|
|
'en', |
141
|
|
|
'fr', |
142
|
|
|
'de', |
143
|
|
|
'id', |
144
|
|
|
]); |
145
|
|
|
$this->app->make('translatable.locales')->load(); |
146
|
|
|
|
147
|
|
|
$this->assertEquals('de', $this->app->make('translatable.locales')['de']); |
148
|
|
|
$this->assertNull($this->app->make('translatable.locales')['jp']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
View Code Duplication |
public function test_add_language_locale() |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$this->app['config']->set('translatable.locales', [ |
154
|
|
|
'de', |
155
|
|
|
]); |
156
|
|
|
$this->app->make('translatable.locales')->load(); |
157
|
|
|
|
158
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('de')); |
159
|
|
|
$this->assertFalse($this->app->make('translatable.locales')->has('en')); |
160
|
|
|
$this->app->make('translatable.locales')->add('en'); |
161
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('en')); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
View Code Duplication |
public function test_offset_set_language_locale() |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
$this->app['config']->set('translatable.locales', [ |
167
|
|
|
'de', |
168
|
|
|
]); |
169
|
|
|
$this->app->make('translatable.locales')->load(); |
170
|
|
|
|
171
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('de')); |
172
|
|
|
$this->assertFalse($this->app->make('translatable.locales')->has('en')); |
173
|
|
|
$this->app->make('translatable.locales')[] = 'en'; |
174
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('en')); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
View Code Duplication |
public function test_offset_set_country_locale() |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
$this->app['config']->set('translatable.locales', [ |
180
|
|
|
'de', |
181
|
|
|
]); |
182
|
|
|
$this->app->make('translatable.locales')->load(); |
183
|
|
|
|
184
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('de')); |
185
|
|
|
$this->assertFalse($this->app->make('translatable.locales')->has('de-AT')); |
186
|
|
|
$this->app->make('translatable.locales')['de'] = 'AT'; |
187
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('de-AT')); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
View Code Duplication |
public function test_forget() |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
$this->app['config']->set('translatable.locales', [ |
193
|
|
|
'de', |
194
|
|
|
'en', |
195
|
|
|
]); |
196
|
|
|
$this->app->make('translatable.locales')->load(); |
197
|
|
|
|
198
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('de')); |
199
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('en')); |
200
|
|
|
$this->app->make('translatable.locales')->forget('en'); |
201
|
|
|
$this->assertFalse($this->app->make('translatable.locales')->has('en')); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
View Code Duplication |
public function test_offset_unset() |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
$this->app['config']->set('translatable.locales', [ |
207
|
|
|
'de', |
208
|
|
|
'en', |
209
|
|
|
]); |
210
|
|
|
$this->app->make('translatable.locales')->load(); |
211
|
|
|
|
212
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('de')); |
213
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->has('en')); |
214
|
|
|
unset($this->app->make('translatable.locales')['en']); |
215
|
|
|
$this->assertFalse($this->app->make('translatable.locales')->has('en')); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function test_get_locale_separator_config() |
219
|
|
|
{ |
220
|
|
|
$this->app['config']->set('translatable.locale_separator', '_'); |
221
|
|
|
|
222
|
|
|
$this->assertEquals('_', $this->app->make('translatable.locales')->getLocaleSeparator()); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function test_get_locale_separator_default() |
226
|
|
|
{ |
227
|
|
|
$this->app['config']->set('translatable.locale_separator', null); |
228
|
|
|
|
229
|
|
|
$this->assertEquals('-', $this->app->make('translatable.locales')->getLocaleSeparator()); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function test_get_country_locale() |
233
|
|
|
{ |
234
|
|
|
$this->assertEquals('de-AT', $this->app->make('translatable.locales')->getCountryLocale('de', 'AT')); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function test_is_locale_country_based() |
238
|
|
|
{ |
239
|
|
|
$this->assertTrue($this->app->make('translatable.locales')->isLocaleCountryBased('de-AT')); |
240
|
|
|
$this->assertFalse($this->app->make('translatable.locales')->isLocaleCountryBased('de')); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function test_get_language_from_country_based_locale() |
244
|
|
|
{ |
245
|
|
|
$this->assertEquals('de', $this->app->make('translatable.locales')->getLanguageFromCountryBasedLocale('de-AT')); |
246
|
|
|
$this->assertEquals('de', $this->app->make('translatable.locales')->getLanguageFromCountryBasedLocale('de')); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.