13
13
use FactorioItemBrowser \Export \Service \ModFileService ;
14
14
use FactorioItemBrowser \Export \Parser \TranslationParser ;
15
15
use FactorioItemBrowser \ExportData \Collection \DictionaryInterface ;
16
+ use FactorioItemBrowser \ExportData \Collection \TranslationDictionary ;
16
17
use FactorioItemBrowser \ExportData \ExportData ;
17
18
use PHPUnit \Framework \MockObject \MockObject ;
18
19
use PHPUnit \Framework \TestCase ;
20
+ use ReflectionException ;
19
21
20
22
/**
21
23
* The PHPUnit test of the TranslationParser class.
@@ -42,13 +44,21 @@ protected function setUp(): void
42
44
$ this ->translator = $ this ->createMock (Translator::class);
43
45
}
44
46
45
- private function createInstance (): TranslationParser
47
+ /**
48
+ * @param array<string> $mockedMethods
49
+ * @return TranslationParser&MockObject
50
+ */
51
+ private function createInstance (array $ mockedMethods = []): TranslationParser
46
52
{
47
- return new TranslationParser (
48
- $ this ->console ,
49
- $ this ->modFileManager ,
50
- $ this ->translator ,
51
- );
53
+ return $ this ->getMockBuilder (TranslationParser::class)
54
+ ->disableProxyingToOriginalMethods ()
55
+ ->onlyMethods ($ mockedMethods )
56
+ ->setConstructorArgs ([
57
+ $ this ->console ,
58
+ $ this ->modFileManager ,
59
+ $ this ->translator ,
60
+ ])
61
+ ->getMock ();
52
62
}
53
63
54
64
/**
@@ -139,7 +149,66 @@ public function testTranslate(): void
139
149
'' ,
140
150
);
141
151
142
- $ instance = $ this ->createInstance ();
152
+ $ instance = $ this ->createInstance (['filterDuplicates ' ]);
153
+ $ instance ->expects ($ this ->once ())
154
+ ->method ('filterDuplicates ' )
155
+ ->with ($ this ->identicalTo ($ translations ));
156
+
143
157
$ instance ->translate ($ translations , $ localisedString , $ fallbackLocalisedString );
144
158
}
159
+
160
+ /**
161
+ * @return array<mixed>
162
+ */
163
+ public function provideFilterDuplicates (): array
164
+ {
165
+ // Translations with duplication
166
+ $ translations1 = new TranslationDictionary ();
167
+ $ translations1 ->set ('en ' , 'abc ' );
168
+ $ translations1 ->set ('de ' , 'abc ' );
169
+ $ translations1 ->set ('fr ' , 'def ' );
170
+ $ translations1 ->set ('ja ' , 'def ' );
171
+ $ expectedTranslations1 = new TranslationDictionary ();
172
+ $ expectedTranslations1 ->set ('en ' , 'abc ' );
173
+ $ expectedTranslations1 ->set ('fr ' , 'def ' );
174
+ $ expectedTranslations1 ->set ('ja ' , 'def ' );
175
+
176
+ // Translations without duplication
177
+ $ translations2 = new TranslationDictionary ();
178
+ $ translations2 ->set ('en ' , 'abc ' );
179
+ $ translations2 ->set ('de ' , 'def ' );
180
+ $ translations2 ->set ('fr ' , 'ghi ' );
181
+ $ expectedTranslations2 = new TranslationDictionary ();
182
+ $ expectedTranslations2 ->set ('en ' , 'abc ' );
183
+ $ expectedTranslations2 ->set ('de ' , 'def ' );
184
+ $ expectedTranslations2 ->set ('fr ' , 'ghi ' );
185
+
186
+ // Translations without English will never filter
187
+ $ translations3 = new TranslationDictionary ();
188
+ $ translations3 ->set ('de ' , 'abc ' );
189
+ $ translations3 ->set ('fr ' , 'abc ' );
190
+ $ translations3 ->set ('ja ' , 'ghi ' );
191
+ $ expectedTranslations3 = new TranslationDictionary ();
192
+ $ expectedTranslations3 ->set ('de ' , 'abc ' );
193
+ $ expectedTranslations3 ->set ('fr ' , 'abc ' );
194
+ $ expectedTranslations3 ->set ('ja ' , 'ghi ' );
195
+
196
+ return [
197
+ [$ translations1 , $ expectedTranslations1 ],
198
+ ];
199
+ }
200
+
201
+ /**
202
+ * @throws ReflectionException
203
+ * @dataProvider provideFilterDuplicates
204
+ */
205
+ public function testFilterDuplicates (
206
+ DictionaryInterface $ translations ,
207
+ DictionaryInterface $ expectedTranslations
208
+ ): void {
209
+ $ instance = $ this ->createInstance ();
210
+ $ this ->invokeMethod ($ instance , 'filterDuplicates ' , $ translations );
211
+
212
+ $ this ->assertEquals ($ expectedTranslations , $ translations );
213
+ }
145
214
}
0 commit comments