Completed
Push — translatable-trait ( 71d736 )
by Kamil
40:10
created

it_instantiates_with_valid_locale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ThemeBundle\Tests\Translation;
13
14
use Sylius\Bundle\ThemeBundle\Translation\Provider\Loader\TranslatorLoaderProvider;
15
use Sylius\Bundle\ThemeBundle\Translation\Provider\Resource\TranslatorResourceProvider;
16
use Sylius\Bundle\ThemeBundle\Translation\Translator;
17
use Symfony\Component\Translation\Exception\InvalidArgumentException;
18
use Symfony\Component\Translation\Loader\ArrayLoader;
19
use Symfony\Component\Translation\MessageCatalogue;
20
use Symfony\Component\Translation\MessageSelector;
21
22
/**
23
 * @see \Symfony\Component\Translation\Tests\TranslatorTest
24
 *
25
 * @author Jan Góralski <[email protected]>
26
 */
27
final class TranslatorTest extends \PHPUnit_Framework_TestCase
28
{
29
    /**
30
     * @test
31
     * @dataProvider getInvalidOptionsTests
32
     * @expectedException \InvalidArgumentException
33
     */
34
    public function it_throws_exception_on_instantiating_with_invalid_options(array $options)
35
    {
36
        $this->createTranslator('en', $options);
37
    }
38
39
    /**
40
     * @test
41
     * @dataProvider getValidOptionsTests
42
     */
43
    public function it_instantiates_with_valid_options(array $options)
44
    {
45
        $this->createTranslator('en', $options);
46
    }
47
48
    /**
49
     * @test
50
     * @dataProvider getInvalidLocalesTests
51
     * @expectedException \InvalidArgumentException
52
     */
53
    public function it_throws_exception_on_instantiating_with_invalid_locale($locale)
54
    {
55
        $this->createTranslator($locale);
56
    }
57
58
    /**
59
     * @test
60
     * @dataProvider getAllValidLocalesTests
61
     */
62
    public function it_instantiates_with_valid_locale($locale)
63
    {
64
        $translator = $this->createTranslator($locale);
65
66
        $this->assertEquals($locale, $translator->getLocale());
67
    }
68
69
    /**
70
     * @test
71
     * @dataProvider getInvalidLocalesTests
72
     * @expectedException InvalidArgumentException
73
     */
74
    public function its_throws_exception_on_setting_invalid_fallback_locales($locale)
75
    {
76
        $translator = $this->createTranslator('fr');
77
        $translator->setFallbackLocales(['fr', $locale]);
78
    }
79
80
    /**
81
     * @test
82
     * @dataProvider getAllValidLocalesTests
83
     */
84
    public function its_fallback_locales_can_be_set_only_if_valid($locale)
85
    {
86
        $translator = $this->createTranslator('fr');
87
        $translator->setFallbackLocales(['fr', $locale]);
88
    }
89
90
    /**
91
     * @test
92
     * @dataProvider getAllValidLocalesTests
93
     */
94
    public function it_adds_resources_with_valid_locales($locale)
95
    {
96
        $translator = $this->createTranslator('fr');
97
        $translator->addResource('array', ['foo' => 'foofoo'], $locale);
98
    }
99
100
    /**
101
     * @test
102
     * @dataProvider getAllValidLocalesTests
103
     */
104
    public function it_translates_valid_locales($locale)
105
    {
106
        $translator = $this->createTranslator($locale);
107
        $translator->addLoader('array', new ArrayLoader());
108
        $translator->addResource('array', ['test' => 'OK'], $locale);
109
110
        $this->assertEquals('OK', $translator->trans('test'));
111
        $this->assertEquals('OK', $translator->trans('test', [], null, $locale));
112
    }
113
114
    /**
115
     * @test
116
     */
117
    public function it_translates_to_a_fallback_locale()
118
    {
119
        $translator = $this->createTranslator('en');
120
        $translator->setFallbackLocales(['fr']);
121
122
        $translator->addLoader('array', new ArrayLoader());
123
        $translator->addResource('array', ['foo' => 'foofoo'], 'en');
124
        $translator->addResource('array', ['bar' => 'foobar'], 'fr');
125
126
        $this->assertEquals('foobar', $translator->trans('bar'));
127
    }
128
129
    /**
130
     * @test
131
     */
132
    public function it_can_have_multiple_fallback_locales()
133
    {
134
        $translator = $this->createTranslator('en');
135
        $translator->setFallbackLocales(['de', 'fr']);
136
137
        $translator->addLoader('array', new ArrayLoader());
138
        $translator->addResource('array', ['foo' => 'foo (en)'], 'en');
139
        $translator->addResource('array', ['bar' => 'bar (fr)'], 'fr');
140
        $translator->addResource('array', ['foobar' => 'foobar (de)'], 'de');
141
142
        $this->assertEquals('bar (fr)', $translator->trans('bar'));
143
        $this->assertEquals('foobar (de)', $translator->trans('foobar'));
144
    }
145
146
    /**
147
     * @test
148
     * @dataProvider getThemelessLocalesTests
149
     */
150
    public function it_gets_catalogue_with_fallback_catalogues_of_a_simple_locale($locale)
151
    {
152
        $translator = $this->createTranslator($locale);
153
        $catalogue = new MessageCatalogue($locale);
154
155
        $this->assertEquals($catalogue, $translator->getCatalogue());
156
    }
157
158
    /**
159
     * @test
160
     * @dataProvider getThemedLocalesTests
161
     */
162
    public function it_gets_catalogue_with_fallback_catalogues_of_a_themed_locale($locale)
163
    {
164
        $translator = $this->createTranslator($locale);
165
166
        $catalogue = new MessageCatalogue($locale);
167
        $themeDelimiter = strrpos($locale, '@');
168
169
        $catalogue->addFallbackCatalogue(new MessageCatalogue(substr($locale, 0, $themeDelimiter)));
170
171
        $this->assertEquals($catalogue, $translator->getCatalogue());
172
    }
173
174
    /**
175
     * @test
176
     */
177
    public function it_creates_a_nested_catalogue_with_fallback_translations_of_a_territorial_locale()
178
    {
179
        $translator = $this->createTranslator('fr_FR');
180
181
        $catalogue = new MessageCatalogue('fr_FR');
182
183
        $fallback = new MessageCatalogue('fr');
184
        $catalogue->addFallbackCatalogue($fallback);
185
186
        $this->assertEquals($catalogue, $translator->getCatalogue());
187
    }
188
189
    /**
190
     * @test
191
     */
192
    public function it_creates_a_nested_catalogue_with_fallback_translations_of_a_themed_locale()
193
    {
194
        $translator = $this->createTranslator('fr_FR@heron');
195
196
        $catalogue = new MessageCatalogue('fr_FR@heron');
197
198
        $firstFallback = new MessageCatalogue('fr_FR');
199
        $catalogue->addFallbackCatalogue($firstFallback);
200
201
        $secondFallback = new MessageCatalogue('fr@heron');
202
        $firstFallback->addFallbackCatalogue($secondFallback);
203
204
        $thirdFallback = new MessageCatalogue('fr');
205
        $secondFallback->addFallbackCatalogue($thirdFallback);
206
207
        $this->assertEquals($catalogue, $translator->getCatalogue());
208
    }
209
210
    /**
211
     * @test
212
     */
213
    public function it_creates_a_nested_catalogue_with_fallback_translations_with_duplicated_additional_fallbacks()
214
    {
215
        $translator = $this->createTranslator('fr_FR@heron');
216
        $translator->setFallbackLocales(['fr_FR', 'fr']);
217
218
        $catalogue = new MessageCatalogue('fr_FR@heron');
219
220
        $firstFallback = new MessageCatalogue('fr_FR');
221
        $catalogue->addFallbackCatalogue($firstFallback);
222
223
        $secondFallback = new MessageCatalogue('fr@heron');
224
        $firstFallback->addFallbackCatalogue($secondFallback);
225
226
        $thirdFallback = new MessageCatalogue('fr');
227
        $secondFallback->addFallbackCatalogue($thirdFallback);
228
229
        $this->assertEquals($catalogue, $translator->getCatalogue());
230
    }
231
232
    /**
233
     * @test
234
     */
235
    public function it_creates_a_nested_catalogue_with_fallback_translations()
236
    {
237
        $translator = $this->createTranslator('fr_FR@heron');
238
        $translator->setFallbackLocales(['en_US', 'en']);
239
240
        $catalogue = new MessageCatalogue('fr_FR@heron');
241
242
        $firstFallback = new MessageCatalogue('fr_FR');
243
        $catalogue->addFallbackCatalogue($firstFallback);
244
245
        $secondFallback = new MessageCatalogue('fr@heron');
246
        $firstFallback->addFallbackCatalogue($secondFallback);
247
248
        $thirdFallback = new MessageCatalogue('fr');
249
        $secondFallback->addFallbackCatalogue($thirdFallback);
250
251
        $fourthFallback = new MessageCatalogue('en_US@heron');
252
        $thirdFallback->addFallbackCatalogue($fourthFallback);
253
254
        $fifthFallback = new MessageCatalogue('en_US');
255
        $fourthFallback->addFallbackCatalogue($fifthFallback);
256
257
        $sixthFallback = new MessageCatalogue('en@heron');
258
        $fifthFallback->addFallbackCatalogue($sixthFallback);
259
260
        $seventhFallback = new MessageCatalogue('en');
261
        $sixthFallback->addFallbackCatalogue($seventhFallback);
262
263
        $this->assertEquals($catalogue, $translator->getCatalogue());
264
    }
265
266
    /**
267
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[][].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
268
     */
269
    public function getInvalidLocalesTests()
270
    {
271
        return [
272
            ['fr FR'],
273
            ['français'],
274
            ['fr+en'],
275
            ['utf#8'],
276
            ['fr&en'],
277
            ['fr~FR'],
278
            [' fr'],
279
            ['fr '],
280
            ['fr*'],
281
            ['fr/FR'],
282
            ['fr\\FR'],
283
        ];
284
    }
285
286
    /**
287
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string|null>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
288
     */
289
    public function getAllValidLocalesTests()
290
    {
291
        return array_merge(
292
            $this->getThemedLocalesTests(),
293
            $this->getThemelessLocalesTests()
294
        );
295
    }
296
297
    /**
298
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[][].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
299
     */
300
    public function getThemedLocalesTests()
301
    {
302
        return [
303
            ['fr@heron'],
304
            ['francais@heron'],
305
            ['FR@heron'],
306
            ['frFR@heron'],
307
            ['fr-FR@heron'],
308
            ['fr.FR@heron'],
309
            ['fr-FR.UTF8@heron'],
310
        ];
311
    }
312
313
    /**
314
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string|null>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
315
     */
316
    public function getThemelessLocalesTests()
317
    {
318
        return [
319
            [''],
320
            [null],
321
            ['fr'],
322
            ['francais'],
323
            ['FR'],
324
            ['frFR'],
325
            ['fr-FR'],
326
            ['fr.FR'],
327
            ['fr-FR.UTF8'],
328
        ];
329
    }
330
331
    /**
332
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array[][].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
333
     */
334
    public function getValidOptionsTests(){
335
        return [
336
            [['cache_dir' => null, 'debug' => false]],
337
            [['cache_dir' => 'someDirectory', 'debug' => false]],
338
            [['debug' => false]],
339
            [['cache_dir' => 'yup']],
340
            [[]],
341
        ];
342
    }
343
344
    /**
345
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
346
     */
347
    public function getInvalidOptionsTests(){
348
        return [
349
            [['heron' => '']],
350
            [['cache_dir' => null, 'pugs' => 'yes']],
351
            [['cache_dir' => null, 'debug' => false, 'pug' => 'heron']],
352
        ];
353
    }
354
355
    /**
356
     * @param string $locale
357
     * @param string[] $options
358
     *
359
     * @return Translator
360
     */
361
    private function createTranslator($locale = 'en', $options = [])
362
    {
363
        $loaderProvider = new TranslatorLoaderProvider();
364
        $resourceProvider = new TranslatorResourceProvider();
365
        $messageSelector = $this->getMockBuilder(MessageSelector::class)->getMock();
366
367
        return new Translator($loaderProvider, $resourceProvider, $messageSelector, $locale, $options);
368
    }
369
}
370