/var/www/hkosl.com/nick/codeigniter/application/vendor/symfony/translation/Tests/TranslatorTest.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Translation\Tests;

use 
PHPUnit\Framework\TestCase;
use 
Symfony\Component\Translation\Loader\ArrayLoader;
use 
Symfony\Component\Translation\MessageCatalogue;
use 
Symfony\Component\Translation\Translator;

class 
TranslatorTest extends TestCase
{
    
/**
     * @dataProvider      getInvalidLocalesTests
     * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
     */
    
public function testConstructorInvalidLocale($locale)
    {
        new 
Translator($locale);
    }

    
/**
     * @dataProvider getValidLocalesTests
     */
    
public function testConstructorValidLocale($locale)
    {
        
$translator = new Translator($locale);

        
$this->assertEquals($locale$translator->getLocale());
    }

    public function 
testConstructorWithoutLocale()
    {
        
$translator = new Translator(null);

        
$this->assertNull($translator->getLocale());
    }

    public function 
testSetGetLocale()
    {
        
$translator = new Translator('en');

        
$this->assertEquals('en'$translator->getLocale());

        
$translator->setLocale('fr');
        
$this->assertEquals('fr'$translator->getLocale());
    }

    
/**
     * @dataProvider      getInvalidLocalesTests
     * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
     */
    
public function testSetInvalidLocale($locale)
    {
        
$translator = new Translator('fr');
        
$translator->setLocale($locale);
    }

    
/**
     * @dataProvider getValidLocalesTests
     */
    
public function testSetValidLocale($locale)
    {
        
$translator = new Translator($locale);
        
$translator->setLocale($locale);

        
$this->assertEquals($locale$translator->getLocale());
    }

    public function 
testGetCatalogue()
    {
        
$translator = new Translator('en');

        
$this->assertEquals(new MessageCatalogue('en'), $translator->getCatalogue());

        
$translator->setLocale('fr');
        
$this->assertEquals(new MessageCatalogue('fr'), $translator->getCatalogue('fr'));
    }

    public function 
testGetCatalogueReturnsConsolidatedCatalogue()
    {
        
/*
         * This will be useful once we refactor so that different domains will be loaded lazily (on-demand).
         * In that case, getCatalogue() will probably have to load all missing domains in order to return
         * one complete catalogue.
         */

        
$locale 'whatever';
        
$translator = new Translator($locale);
        
$translator->addLoader('loader-a', new ArrayLoader());
        
$translator->addLoader('loader-b', new ArrayLoader());
        
$translator->addResource('loader-a', ['foo' => 'foofoo'], $locale'domain-a');
        
$translator->addResource('loader-b', ['bar' => 'foobar'], $locale'domain-b');

        
/*
         * Test that we get a single catalogue comprising messages
         * from different loaders and different domains
         */
        
$catalogue $translator->getCatalogue($locale);
        
$this->assertTrue($catalogue->defines('foo''domain-a'));
        
$this->assertTrue($catalogue->defines('bar''domain-b'));
    }

    public function 
testSetFallbackLocales()
    {
        
$translator = new Translator('en');
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['foo' => 'foofoo'], 'en');
        
$translator->addResource('array', ['bar' => 'foobar'], 'fr');

        
// force catalogue loading
        
$translator->trans('bar');

        
$translator->setFallbackLocales(['fr']);
        
$this->assertEquals('foobar'$translator->trans('bar'));
    }

    public function 
testSetFallbackLocalesMultiple()
    {
        
$translator = new Translator('en');
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['foo' => 'foo (en)'], 'en');
        
$translator->addResource('array', ['bar' => 'bar (fr)'], 'fr');

        
// force catalogue loading
        
$translator->trans('bar');

        
$translator->setFallbackLocales(['fr_FR''fr']);
        
$this->assertEquals('bar (fr)'$translator->trans('bar'));
    }

    
/**
     * @dataProvider      getInvalidLocalesTests
     * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
     */
    
public function testSetFallbackInvalidLocales($locale)
    {
        
$translator = new Translator('fr');
        
$translator->setFallbackLocales(['fr'$locale]);
    }

    
/**
     * @dataProvider getValidLocalesTests
     */
    
public function testSetFallbackValidLocales($locale)
    {
        
$translator = new Translator($locale);
        
$translator->setFallbackLocales(['fr'$locale]);
        
// no assertion. this method just asserts that no exception is thrown
        
$this->addToAssertionCount(1);
    }

    public function 
testTransWithFallbackLocale()
    {
        
$translator = new Translator('fr_FR');
        
$translator->setFallbackLocales(['en']);

        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['bar' => 'foobar'], 'en');

        
$this->assertEquals('foobar'$translator->trans('bar'));
    }

    
/**
     * @dataProvider      getInvalidLocalesTests
     * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
     */
    
public function testAddResourceInvalidLocales($locale)
    {
        
$translator = new Translator('fr');
        
$translator->addResource('array', ['foo' => 'foofoo'], $locale);
    }

    
/**
     * @dataProvider getValidLocalesTests
     */
    
public function testAddResourceValidLocales($locale)
    {
        
$translator = new Translator('fr');
        
$translator->addResource('array', ['foo' => 'foofoo'], $locale);
        
// no assertion. this method just asserts that no exception is thrown
        
$this->addToAssertionCount(1);
    }

    public function 
testAddResourceAfterTrans()
    {
        
$translator = new Translator('fr');
        
$translator->addLoader('array', new ArrayLoader());

        
$translator->setFallbackLocales(['en']);

        
$translator->addResource('array', ['foo' => 'foofoo'], 'en');
        
$this->assertEquals('foofoo'$translator->trans('foo'));

        
$translator->addResource('array', ['bar' => 'foobar'], 'en');
        
$this->assertEquals('foobar'$translator->trans('bar'));
    }

    
/**
     * @dataProvider      getTransFileTests
     * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
     */
    
public function testTransWithoutFallbackLocaleFile($format$loader)
    {
        
$loaderClass 'Symfony\\Component\\Translation\\Loader\\'.$loader;
        
$translator = new Translator('en');
        
$translator->addLoader($format, new $loaderClass());
        
$translator->addResource($format__DIR__.'/fixtures/non-existing''en');
        
$translator->addResource($format__DIR__.'/fixtures/resources.'.$format'en');

        
// force catalogue loading
        
$translator->trans('foo');
    }

    
/**
     * @dataProvider getTransFileTests
     */
    
public function testTransWithFallbackLocaleFile($format$loader)
    {
        
$loaderClass 'Symfony\\Component\\Translation\\Loader\\'.$loader;
        
$translator = new Translator('en_GB');
        
$translator->addLoader($format, new $loaderClass());
        
$translator->addResource($format__DIR__.'/fixtures/non-existing''en_GB');
        
$translator->addResource($format__DIR__.'/fixtures/resources.'.$format'en''resources');

        
$this->assertEquals('bar'$translator->trans('foo', [], 'resources'));
    }

    public function 
testTransWithFallbackLocaleBis()
    {
        
$translator = new Translator('en_US');
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['foo' => 'foofoo'], 'en_US');
        
$translator->addResource('array', ['bar' => 'foobar'], 'en');
        
$this->assertEquals('foobar'$translator->trans('bar'));
    }

    public function 
testTransWithFallbackLocaleTer()
    {
        
$translator = new Translator('fr_FR');
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['foo' => 'foo (en_US)'], 'en_US');
        
$translator->addResource('array', ['bar' => 'bar (en)'], 'en');

        
$translator->setFallbackLocales(['en_US''en']);

        
$this->assertEquals('foo (en_US)'$translator->trans('foo'));
        
$this->assertEquals('bar (en)'$translator->trans('bar'));
    }

    public function 
testTransNonExistentWithFallback()
    {
        
$translator = new Translator('fr');
        
$translator->setFallbackLocales(['en']);
        
$translator->addLoader('array', new ArrayLoader());
        
$this->assertEquals('non-existent'$translator->trans('non-existent'));
    }

    
/**
     * @expectedException \Symfony\Component\Translation\Exception\RuntimeException
     */
    
public function testWhenAResourceHasNoRegisteredLoader()
    {
        
$translator = new Translator('en');
        
$translator->addResource('array', ['foo' => 'foofoo'], 'en');

        
$translator->trans('foo');
    }

    public function 
testNestedFallbackCatalogueWhenUsingMultipleLocales()
    {
        
$translator = new Translator('fr');
        
$translator->setFallbackLocales(['ru''en']);

        
$translator->getCatalogue('fr');

        
$this->assertNotNull($translator->getCatalogue('ru')->getFallbackCatalogue());
    }

    public function 
testFallbackCatalogueResources()
    {
        
$translator = new Translator('en_GB');
        
$translator->addLoader('yml', new \Symfony\Component\Translation\Loader\YamlFileLoader());
        
$translator->addResource('yml'__DIR__.'/fixtures/empty.yml''en_GB');
        
$translator->addResource('yml'__DIR__.'/fixtures/resources.yml''en');

        
// force catalogue loading
        
$this->assertEquals('bar'$translator->trans('foo', []));

        
$resources $translator->getCatalogue('en')->getResources();
        
$this->assertCount(1$resources);
        
$this->assertContains(__DIR__.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'resources.yml'$resources);

        
$resources $translator->getCatalogue('en_GB')->getResources();
        
$this->assertCount(2$resources);
        
$this->assertContains(__DIR__.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'empty.yml'$resources);
        
$this->assertContains(__DIR__.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'resources.yml'$resources);
    }

    
/**
     * @dataProvider getTransTests
     */
    
public function testTrans($expected$id$translation$parameters$locale$domain)
    {
        
$translator = new Translator('en');
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', [(string) $id => $translation], $locale$domain);

        
$this->assertEquals($expected$translator->trans($id$parameters$domain$locale));
    }

    
/**
     * @dataProvider      getInvalidLocalesTests
     * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
     */
    
public function testTransInvalidLocale($locale)
    {
        
$translator = new Translator('en');
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['foo' => 'foofoo'], 'en');

        
$translator->trans('foo', [], ''$locale);
    }

    
/**
     * @dataProvider      getValidLocalesTests
     */
    
public function testTransValidLocale($locale)
    {
        
$translator = new Translator($locale);
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['test' => 'OK'], $locale);

        
$this->assertEquals('OK'$translator->trans('test'));
        
$this->assertEquals('OK'$translator->trans('test', [], null$locale));
    }

    
/**
     * @dataProvider getFlattenedTransTests
     */
    
public function testFlattenedTrans($expected$messages$id)
    {
        
$translator = new Translator('en');
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array'$messages'fr''');

        
$this->assertEquals($expected$translator->trans($id, [], '''fr'));
    }

    
/**
     * @dataProvider getTransChoiceTests
     */
    
public function testTransChoice($expected$id$translation$number$parameters$locale$domain)
    {
        
$translator = new Translator('en');
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', [(string) $id => $translation], $locale$domain);

        
$this->assertEquals($expected$translator->transChoice($id$number$parameters$domain$locale));
    }

    
/**
     * @dataProvider      getInvalidLocalesTests
     * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
     */
    
public function testTransChoiceInvalidLocale($locale)
    {
        
$translator = new Translator('en');
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['foo' => 'foofoo'], 'en');

        
$translator->transChoice('foo'1, [], ''$locale);
    }

    
/**
     * @dataProvider      getValidLocalesTests
     */
    
public function testTransChoiceValidLocale($locale)
    {
        
$translator = new Translator('en');
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['foo' => 'foofoo'], 'en');

        
$translator->transChoice('foo'1, [], ''$locale);
        
// no assertion. this method just asserts that no exception is thrown
        
$this->addToAssertionCount(1);
    }

    public function 
getTransFileTests()
    {
        return [
            [
'csv''CsvFileLoader'],
            [
'ini''IniFileLoader'],
            [
'mo''MoFileLoader'],
            [
'po''PoFileLoader'],
            [
'php''PhpFileLoader'],
            [
'ts''QtFileLoader'],
            [
'xlf''XliffFileLoader'],
            [
'yml''YamlFileLoader'],
            [
'json''JsonFileLoader'],
        ];
    }

    public function 
getTransTests()
    {
        return [
            [
'Symfony est super !''Symfony is great!''Symfony est super !', [], 'fr'''],
            [
'Symfony est awesome !''Symfony is %what%!''Symfony est %what% !', ['%what%' => 'awesome'], 'fr'''],
            [
'Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', [], 'fr'''],
        ];
    }

    public function 
getFlattenedTransTests()
    {
        
$messages = [
            
'symfony' => [
                
'is' => [
                    
'great' => 'Symfony est super!',
                ],
            ],
            
'foo' => [
                
'bar' => [
                    
'baz' => 'Foo Bar Baz',
                ],
                
'baz' => 'Foo Baz',
            ],
        ];

        return [
            [
'Symfony est super!'$messages'symfony.is.great'],
            [
'Foo Bar Baz'$messages'foo.bar.baz'],
            [
'Foo Baz'$messages'foo.baz'],
        ];
    }

    public function 
getTransChoiceTests()
    {
        return [
            [
'Il y a 0 pomme''{0} There are no appless|{1} There is one apple|]1,Inf] There is %count% apples''[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes'0, [], 'fr'''],
            [
'Il y a 1 pomme''{0} There are no appless|{1} There is one apple|]1,Inf] There is %count% apples''[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes'1, [], 'fr'''],
            [
'Il y a 10 pommes''{0} There are no appless|{1} There is one apple|]1,Inf] There is %count% apples''[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes'10, [], 'fr'''],

            [
'Il y a 0 pomme''There is one apple|There is %count% apples''Il y a %count% pomme|Il y a %count% pommes'0, [], 'fr'''],
            [
'Il y a 1 pomme''There is one apple|There is %count% apples''Il y a %count% pomme|Il y a %count% pommes'1, [], 'fr'''],
            [
'Il y a 10 pommes''There is one apple|There is %count% apples''Il y a %count% pomme|Il y a %count% pommes'10, [], 'fr'''],

            [
'Il y a 0 pomme''one: There is one apple|more: There is %count% apples''one: Il y a %count% pomme|more: Il y a %count% pommes'0, [], 'fr'''],
            [
'Il y a 1 pomme''one: There is one apple|more: There is %count% apples''one: Il y a %count% pomme|more: Il y a %count% pommes'1, [], 'fr'''],
            [
'Il y a 10 pommes''one: There is one apple|more: There is %count% apples''one: Il y a %count% pomme|more: Il y a %count% pommes'10, [], 'fr'''],

            [
'Il n\'y a aucune pomme''{0} There are no apples|one: There is one apple|more: There is %count% apples''{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes'0, [], 'fr'''],
            [
'Il y a 1 pomme''{0} There are no apples|one: There is one apple|more: There is %count% apples''{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes'1, [], 'fr'''],
            [
'Il y a 10 pommes''{0} There are no apples|one: There is one apple|more: There is %count% apples''{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes'10, [], 'fr'''],

            [
'Il y a 0 pomme', new StringClass('{0} There are no appless|{1} There is one apple|]1,Inf] There is %count% apples'), '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes'0, [], 'fr'''],

            
// Override %count% with a custom value
            
['Il y a quelques pommes''one: There is one apple|more: There are %count% apples''one: Il y a %count% pomme|more: Il y a %count% pommes'2, ['%count%' => 'quelques'], 'fr'''],
        ];
    }

    public function 
getInvalidLocalesTests()
    {
        return [
            [
'fr FR'],
            [
'français'],
            [
'fr+en'],
            [
'utf#8'],
            [
'fr&en'],
            [
'fr~FR'],
            [
' fr'],
            [
'fr '],
            [
'fr*'],
            [
'fr/FR'],
            [
'fr\\FR'],
        ];
    }

    public function 
getValidLocalesTests()
    {
        return [
            [
''],
            [
null],
            [
'fr'],
            [
'francais'],
            [
'FR'],
            [
'frFR'],
            [
'fr-FR'],
            [
'fr_FR'],
            [
'fr.FR'],
            [
'fr-FR.UTF8'],
            [
'sr@latin'],
        ];
    }

    public function 
testTransChoiceFallback()
    {
        
$translator = new Translator('ru');
        
$translator->setFallbackLocales(['en']);
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['some_message2' => 'one thing|%count% things'], 'en');

        
$this->assertEquals('10 things'$translator->transChoice('some_message2'10, ['%count%' => 10]));
    }

    public function 
testTransChoiceFallbackBis()
    {
        
$translator = new Translator('ru');
        
$translator->setFallbackLocales(['en_US''en']);
        
$translator->addLoader('array', new ArrayLoader());
        
$translator->addResource('array', ['some_message2' => 'one thing|%count% things'], 'en_US');

        
$this->assertEquals('10 things'$translator->transChoice('some_message2'10, ['%count%' => 10]));
    }

    public function 
testTransChoiceFallbackWithNoTranslation()
    {
        
$translator = new Translator('ru');
        
$translator->setFallbackLocales(['en']);
        
$translator->addLoader('array', new ArrayLoader());

        
// consistent behavior with Translator::trans(), which returns the string
        // unchanged if it can't be found
        
$this->assertEquals('some_message2'$translator->transChoice('some_message2'10, ['%count%' => 10]));
    }
}

class 
StringClass
{
    protected 
$str;

    public function 
__construct($str)
    {
        
$this->str $str;
    }

    public function 
__toString()
    {
        return 
$this->str;
    }
}