1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yii\I18n\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
7
|
|
|
use Yiisoft\I18n\Event\MissingTranslationEvent; |
8
|
|
|
use Yiisoft\I18n\MessageFormatterInterface; |
9
|
|
|
use Yiisoft\I18n\MessageReaderInterface; |
10
|
|
|
use Yiisoft\I18n\Translator\Translator; |
11
|
|
|
|
12
|
|
|
final class TranslatorTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @dataProvider getTranslations |
16
|
|
|
* @param string|null $id |
17
|
|
|
* @param string|null $translation |
18
|
|
|
* @param string|null $expected |
19
|
|
|
* @param array $parameters |
20
|
|
|
* @param string|null $category |
21
|
|
|
*/ |
22
|
|
|
public function testTranslation( |
23
|
|
|
?string $id, |
24
|
|
|
?string $translation, |
25
|
|
|
?string $expected, |
26
|
|
|
array $parameters, |
27
|
|
|
?string $category |
28
|
|
|
): void { |
29
|
|
|
$messageReader = $this->createMessageReader([$id => $translation]); |
30
|
|
|
|
31
|
|
|
$messageFormatter = null; |
32
|
|
|
if ([] !== $parameters) { |
33
|
|
|
$messageFormatter = $this->getMockBuilder(MessageFormatterInterface::class)->getMock(); |
34
|
|
|
$messageFormatter |
35
|
|
|
->method('format') |
36
|
|
|
->willReturn($this->formatMessage($translation, $parameters)); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var $translator Translator |
41
|
|
|
*/ |
42
|
|
|
$translator = $this->getMockBuilder(Translator::class) |
43
|
|
|
->setConstructorArgs( |
44
|
|
|
[ |
45
|
|
|
$this->createMock(EventDispatcherInterface::class), |
46
|
|
|
$messageReader, |
47
|
|
|
$messageFormatter |
48
|
|
|
] |
49
|
|
|
) |
50
|
|
|
->enableProxyingToOriginalMethods() |
51
|
|
|
->getMock(); |
52
|
|
|
|
53
|
|
|
$this->assertEquals($expected, $translator->translate($id, $parameters, $category)); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testFallbackLocale(): void |
57
|
|
|
{ |
58
|
|
|
$category = 'test'; |
59
|
|
|
$message = 'test'; |
60
|
|
|
$fallbackMessage = 'test de locale'; |
61
|
|
|
|
62
|
|
|
$messageReader = $this->createMessageReader(['test' => $fallbackMessage]); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var $translator Translator |
66
|
|
|
*/ |
67
|
|
|
$translator = $this->getMockBuilder(Translator::class) |
68
|
|
|
->setConstructorArgs( |
69
|
|
|
[ |
70
|
|
|
$this->createMock(EventDispatcherInterface::class), |
71
|
|
|
$messageReader, |
72
|
|
|
] |
73
|
|
|
) |
74
|
|
|
->enableProxyingToOriginalMethods() |
75
|
|
|
->getMock(); |
76
|
|
|
|
77
|
|
|
$translator->setDefaultLocale('de'); |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->assertEquals($fallbackMessage, $translator->translate($message, [], $category, 'en')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testMissingEventTriggered(): void |
84
|
|
|
{ |
85
|
|
|
$category = 'test'; |
86
|
|
|
$language = 'en'; |
87
|
|
|
$message = 'Message'; |
88
|
|
|
|
89
|
|
|
$eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class) |
|
|
|
|
90
|
|
|
->setMethods(['dispatch']) |
91
|
|
|
->getMock(); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var $translator Translator |
95
|
|
|
*/ |
96
|
|
|
$translator = $this->getMockBuilder(Translator::class) |
97
|
|
|
->setConstructorArgs( |
98
|
|
|
[ |
99
|
|
|
$eventDispatcher, |
100
|
|
|
$this->createMessageReader([]), |
101
|
|
|
] |
102
|
|
|
) |
103
|
|
|
->enableProxyingToOriginalMethods() |
104
|
|
|
->getMock(); |
105
|
|
|
|
106
|
|
|
$translator->setDefaultLocale('de'); |
107
|
|
|
|
108
|
|
|
$eventDispatcher |
109
|
|
|
->expects($this->at(0)) |
110
|
|
|
->method('dispatch') |
111
|
|
|
->with(new MissingTranslationEvent($category, $language, $message)); |
112
|
|
|
|
113
|
|
|
$translator->translate($message, [], $category, $language); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getTranslations(): array |
117
|
|
|
{ |
118
|
|
|
return [ |
119
|
|
|
['test', 'test', 'test', [], null], |
120
|
|
|
['test {param}', 'translated {param}', 'translated param-value', ['param' => 'param-value'], null], |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function formatMessage(string $message, array $parameters): string |
125
|
|
|
{ |
126
|
|
|
foreach ($parameters as $key => $value) { |
127
|
|
|
$message = str_replace('{' . $key . '}', $value, $message); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $message; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function createMessageReader(array $messages): MessageReaderInterface |
134
|
|
|
{ |
135
|
|
|
return new class ($messages) implements MessageReaderInterface { |
136
|
|
|
private array $messages; |
137
|
|
|
|
138
|
|
|
public function __construct(array $messages) |
139
|
|
|
{ |
140
|
|
|
$this->messages = $messages; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function all($context = null): array |
144
|
|
|
{ |
145
|
|
|
return $this->messages; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function one(string $id, $context = null): ?string |
149
|
|
|
{ |
150
|
|
|
return $this->messages[$id] ?? null; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function plural(string $id, int $count, $context = null): ?string |
154
|
|
|
{ |
155
|
|
|
return $this->messages[$id] ?? null; |
156
|
|
|
} |
157
|
|
|
}; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|