1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\I18n; |
6
|
|
|
|
7
|
|
|
use Ecodev\Felix\ConfigProvider; |
8
|
|
|
use Ecodev\Felix\I18n\Translator; |
9
|
|
|
use EcodevTests\Felix\Traits\TestWithContainer; |
10
|
|
|
use Laminas\ConfigAggregator\ArrayProvider; |
11
|
|
|
use Laminas\ConfigAggregator\ConfigAggregator; |
12
|
|
|
use Laminas\I18n\Translator\Loader\PhpArray; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class TranslatorTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
use TestWithContainer; |
18
|
|
|
|
19
|
|
|
public function testTrWithLaminasI18n(): void |
20
|
|
|
{ |
21
|
|
|
$aggregator = new ConfigAggregator([ |
22
|
|
|
ConfigProvider::class, |
23
|
|
|
\Laminas\I18n\ConfigProvider::class, |
24
|
|
|
new ArrayProvider([ |
25
|
|
|
'translator' => [ |
26
|
|
|
'translation_files' => [ |
27
|
|
|
[ |
28
|
|
|
'type' => PhpArray::class, |
29
|
|
|
'filename' => 'tests/I18n/fr.php', |
30
|
|
|
], |
31
|
|
|
], |
32
|
|
|
], |
33
|
|
|
'dependencies' => [ |
34
|
|
|
'factories' => [ |
35
|
|
|
Translator::class => \Laminas\I18n\Translator\TranslatorServiceFactory::class, |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
]), |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$this->createContainer($aggregator); |
42
|
|
|
|
43
|
|
|
self::assertSame('translated value translated value', _tr('foo %param% param %param%', ['param' => 'value'])); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testTrWithCustomTranslator(): void |
47
|
|
|
{ |
48
|
|
|
$aggregator = new ConfigAggregator([ |
49
|
|
|
new ArrayProvider([ |
50
|
|
|
'dependencies' => [ |
51
|
|
|
'factories' => [ |
52
|
|
|
Translator::class => fn () => new class() implements Translator { |
53
|
|
|
public function translate(string $message): string |
54
|
|
|
{ |
55
|
|
|
return 'translated %param% translated %param%'; |
56
|
|
|
} |
57
|
|
|
}, |
58
|
|
|
], |
59
|
|
|
], |
60
|
|
|
]), |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
$this->createContainer($aggregator); |
64
|
|
|
|
65
|
|
|
self::assertSame('translated value translated value', _tr('foo %param% param %param%', ['param' => 'value'])); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @dataProvider providerTr |
70
|
|
|
*/ |
71
|
|
|
public function testTr(string $message, array $replacements, string $expected): void |
72
|
|
|
{ |
73
|
|
|
$this->createDefaultFelixContainer(); |
74
|
|
|
|
75
|
|
|
self::assertSame($expected, _tr($message, $replacements)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function providerTr(): array |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
[ |
82
|
|
|
'foo', |
83
|
|
|
[], |
84
|
|
|
'foo', |
85
|
|
|
], |
86
|
|
|
[ |
87
|
|
|
'foo %param% param', |
88
|
|
|
[], |
89
|
|
|
'foo %param% param', |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
'foo %param% param %param%', |
93
|
|
|
['param' => 'value'], |
94
|
|
|
'foo value param value', |
95
|
|
|
], |
96
|
|
|
[ |
97
|
|
|
'%not-recursive% %string% %int% %float% %null%', |
98
|
|
|
[ |
99
|
|
|
'not-recursive' => '%string%', |
100
|
|
|
'string' => 'value', |
101
|
|
|
'int' => 123, |
102
|
|
|
'float' => 0.75, |
103
|
|
|
'null' => null, |
104
|
|
|
], |
105
|
|
|
'%string% value 123 0.75 ', |
106
|
|
|
], |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|