1414use PHPUnit \Framework \TestCase ;
1515use Symfony \Bundle \FrameworkBundle \Command \TranslationDebugCommand ;
1616use Symfony \Bundle \FrameworkBundle \Console \Application ;
17+ use Symfony \Bundle \FrameworkBundle \Tests \Functional \Bundle \ExtensionWithoutConfigTestBundle \ExtensionWithoutConfigTestBundle ;
18+ use Symfony \Component \Console \Tester \CommandCompletionTester ;
1719use Symfony \Component \Console \Tester \CommandTester ;
1820use Symfony \Component \DependencyInjection \Container ;
1921use Symfony \Component \Filesystem \Filesystem ;
@@ -139,23 +141,30 @@ protected function tearDown(): void
139141 $ this ->fs ->remove ($ this ->translationDir );
140142 }
141143
142- private function createCommandTester ($ extractedMessages = [], $ loadedMessages = [], $ kernel = null , array $ transPaths = [], array $ codePaths = []): CommandTester
144+ private function createCommandTester (array $ extractedMessages = [], array $ loadedMessages = [], KernelInterface $ 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 (array $ extractedMessages = [], array $ loadedMessages = [], KernelInterface $ kernel = null , array $ transPaths = [], array $ codePaths = [], ExtractorInterface $ extractor = null , array $ bundles = [], array $ enabledLocales = []): TranslationDebugCommand
143150 {
144151 $ translator = $ this ->createMock (Translator::class);
145152 $ translator
146153 ->expects ($ this ->any ())
147154 ->method ('getFallbackLocales ' )
148155 ->willReturn (['en ' ]);
149156
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+ }
159168
160169 $ loader = $ this ->createMock (TranslationReader::class);
161170 $ loader
@@ -182,20 +191,20 @@ function ($path, $catalogue) use ($loadedMessages) {
182191 $ kernel
183192 ->expects ($ this ->any ())
184193 ->method ('getBundles ' )
185- ->willReturn ([] );
194+ ->willReturn ($ bundles );
186195
187196 $ container = new Container ();
188197 $ kernel
189198 ->expects ($ this ->any ())
190199 ->method ('getContainer ' )
191200 ->willReturn ($ container );
192201
193- $ command = new TranslationDebugCommand ($ translator , $ loader , $ extractor , $ this ->translationDir .'/translations ' , $ this ->translationDir .'/templates ' , $ transPaths , $ codePaths );
202+ $ command = new TranslationDebugCommand ($ translator , $ loader , $ extractor , $ this ->translationDir .'/translations ' , $ this ->translationDir .'/templates ' , $ transPaths , $ codePaths, $ enabledLocales );
194203
195204 $ application = new Application ($ kernel );
196205 $ application ->add ($ command );
197206
198- return new CommandTester ( $ application ->find ('debug:translation ' ) );
207+ return $ application ->find ('debug:translation ' );
199208 }
200209
201210 private function getBundle ($ path )
@@ -209,4 +218,55 @@ private function getBundle($path)
209218
210219 return $ bundle ;
211220 }
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 , [new ExtensionWithoutConfigTestBundle ()], ['fr ' , 'nl ' ]));
251+ $ suggestions = $ tester ->complete ($ input );
252+ $ this ->assertSame ($ expectedSuggestions , $ suggestions );
253+ }
254+
255+ public function provideCompletionSuggestions ()
256+ {
257+ yield 'locale ' => [
258+ ['' ],
259+ ['fr ' , 'nl ' ],
260+ ];
261+
262+ yield 'bundle ' => [
263+ ['fr ' , '--domain ' , 'messages ' , '' ],
264+ ['ExtensionWithoutConfigTestBundle ' , 'extension_without_config_test ' ],
265+ ];
266+
267+ yield 'option --domain ' => [
268+ ['en ' , '--domain ' , '' ],
269+ ['messages ' , 'validators ' , 'custom_domain ' ],
270+ ];
271+ }
212272}
0 commit comments