14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Bundle \FrameworkBundle \Command \TranslationDebugCommand ;
16
16
use Symfony \Bundle \FrameworkBundle \Console \Application ;
17
+ use Symfony \Component \Console \Tester \CommandCompletionTester ;
17
18
use Symfony \Component \Console \Tester \CommandTester ;
18
19
use Symfony \Component \DependencyInjection \Container ;
19
20
use Symfony \Component \Filesystem \Filesystem ;
20
21
use Symfony \Component \HttpKernel \Bundle \BundleInterface ;
21
22
use Symfony \Component \HttpKernel \KernelInterface ;
23
+ use Symfony \Component \Intl \Locales ;
22
24
use Symfony \Component \Translation \Extractor \ExtractorInterface ;
23
25
use Symfony \Component \Translation \Reader \TranslationReader ;
24
26
use Symfony \Component \Translation \Translator ;
@@ -140,22 +142,29 @@ protected function tearDown(): void
140
142
}
141
143
142
144
private function createCommandTester ($ extractedMessages = [], $ loadedMessages = [], $ kernel = null , array $ transPaths = [], array $ codePaths = []): CommandTester
145
+ {
146
+ return new CommandTester ($ this ->createCommand ($ extractedMessages , $ loadedMessages , $ kernel , $ transPaths , $ codePaths ));
147
+ }
148
+
149
+ private function createCommand ($ extractedMessages = [], $ loadedMessages = [], $ kernel = null , array $ transPaths = [], array $ codePaths = [], ExtractorInterface $ extractor = null ): TranslationDebugCommand
143
150
{
144
151
$ translator = $ this ->createMock (Translator::class);
145
152
$ translator
146
153
->expects ($ this ->any ())
147
154
->method ('getFallbackLocales ' )
148
155
->willReturn (['en ' ]);
149
156
150
- $ extractor = $ this ->createMock (ExtractorInterface::class);
151
- $ extractor
152
- ->expects ($ this ->any ())
153
- ->method ('extract ' )
154
- ->willReturnCallback (
155
- function ($ path , $ catalogue ) use ($ extractedMessages ) {
156
- $ catalogue ->add ($ extractedMessages );
157
- }
158
- );
157
+ if (!$ extractor ) {
158
+ $ extractor = $ this ->createMock (ExtractorInterface::class);
159
+ $ extractor
160
+ ->expects ($ this ->any ())
161
+ ->method ('extract ' )
162
+ ->willReturnCallback (
163
+ function ($ path , $ catalogue ) use ($ extractedMessages ) {
164
+ $ catalogue ->add ($ extractedMessages );
165
+ }
166
+ );
167
+ }
159
168
160
169
$ loader = $ this ->createMock (TranslationReader::class);
161
170
$ loader
@@ -195,7 +204,7 @@ function ($path, $catalogue) use ($loadedMessages) {
195
204
$ application = new Application ($ kernel );
196
205
$ application ->add ($ command );
197
206
198
- return new CommandTester ( $ application ->find ('debug:translation ' ) );
207
+ return $ application ->find ('debug:translation ' );
199
208
}
200
209
201
210
private function getBundle ($ path )
@@ -209,4 +218,50 @@ private function getBundle($path)
209
218
210
219
return $ bundle ;
211
220
}
221
+
222
+ /**
223
+ * @dataProvider provideCompletionSuggestions
224
+ */
225
+ public function testComplete (array $ input , array $ expectedSuggestions )
226
+ {
227
+ $ extractedMessagesWithDomains = [
228
+ 'messages ' => [
229
+ 'foo ' => 'foo ' ,
230
+ ],
231
+ 'validators ' => [
232
+ 'foo ' => 'foo ' ,
233
+ ],
234
+ 'custom_domain ' => [
235
+ 'foo ' => 'foo ' ,
236
+ ],
237
+ ];
238
+ $ extractor = $ this ->createMock (ExtractorInterface::class);
239
+ $ extractor
240
+ ->expects ($ this ->any ())
241
+ ->method ('extract ' )
242
+ ->willReturnCallback (
243
+ function ($ path , $ catalogue ) use ($ extractedMessagesWithDomains ) {
244
+ foreach ($ extractedMessagesWithDomains as $ domain => $ message ) {
245
+ $ catalogue ->add ($ message , $ domain );
246
+ }
247
+ }
248
+ );
249
+
250
+ $ tester = new CommandCompletionTester ($ this ->createCommand ([], [], null , [], [], $ extractor ));
251
+ $ suggestions = $ tester ->complete ($ input );
252
+ $ this ->assertSame ($ expectedSuggestions , $ suggestions );
253
+ }
254
+
255
+ public function provideCompletionSuggestions ()
256
+ {
257
+ yield 'locale ' => [
258
+ ['' ],
259
+ Locales::getLocales (),
260
+ ];
261
+
262
+ yield 'option --domain ' => [
263
+ ['en ' , '--domain ' , '' ],
264
+ ['messages ' , 'validators ' , 'custom_domain ' ],
265
+ ];
266
+ }
212
267
}
0 commit comments