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 \Bundle \FrameworkBundle \Tests \Functional \Bundle \ExtensionWithoutConfigTestBundle \ExtensionWithoutConfigTestBundle ;
18
+ use Symfony \Component \Console \Tester \CommandCompletionTester ;
17
19
use Symfony \Component \Console \Tester \CommandTester ;
18
20
use Symfony \Component \DependencyInjection \Container ;
19
21
use Symfony \Component \Filesystem \Filesystem ;
@@ -139,23 +141,30 @@ protected function tearDown(): void
139
141
$ this ->fs ->remove ($ this ->translationDir );
140
142
}
141
143
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
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
@@ -182,20 +191,20 @@ function ($path, $catalogue) use ($loadedMessages) {
182
191
$ kernel
183
192
->expects ($ this ->any ())
184
193
->method ('getBundles ' )
185
- ->willReturn ([] );
194
+ ->willReturn ($ bundles );
186
195
187
196
$ container = new Container ();
188
197
$ kernel
189
198
->expects ($ this ->any ())
190
199
->method ('getContainer ' )
191
200
->willReturn ($ container );
192
201
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 );
194
203
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,55 @@ 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 , [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
+ }
212
272
}
0 commit comments