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 \Fixtures \BaseBundle \BaseBundle ;
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 ;
20
22
use Symfony \Component \HttpKernel \Bundle \BundleInterface ;
21
23
use Symfony \Component \HttpKernel \KernelInterface ;
24
+ use Symfony \Component \Intl \Locales ;
22
25
use Symfony \Component \Translation \Extractor \ExtractorInterface ;
23
26
use Symfony \Component \Translation \Reader \TranslationReader ;
24
27
use Symfony \Component \Translation \Translator ;
@@ -139,23 +142,30 @@ protected function tearDown(): void
139
142
$ this ->fs ->remove ($ this ->translationDir );
140
143
}
141
144
142
- private function createCommandTester ($ extractedMessages = [], $ loadedMessages = [], $ kernel = null , array $ transPaths = [], array $ codePaths = []): CommandTester
145
+ private function createCommandTester (array $ extractedMessages = [], array $ loadedMessages = [], KernelInterface $ kernel = null , array $ transPaths = [], array $ codePaths = []): CommandTester
146
+ {
147
+ return new CommandTester ($ this ->createCommand ($ extractedMessages , $ loadedMessages , $ kernel , $ transPaths , $ codePaths ));
148
+ }
149
+
150
+ private function createCommand (array $ extractedMessages = [], array $ loadedMessages = [], KernelInterface $ kernel = null , array $ transPaths = [], array $ codePaths = [], ExtractorInterface $ extractor = null , array $ bundles = []): TranslationDebugCommand
143
151
{
144
152
$ translator = $ this ->createMock (Translator::class);
145
153
$ translator
146
154
->expects ($ this ->any ())
147
155
->method ('getFallbackLocales ' )
148
156
->willReturn (['en ' ]);
149
157
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
- );
158
+ if (!$ extractor ) {
159
+ $ extractor = $ this ->createMock (ExtractorInterface::class);
160
+ $ extractor
161
+ ->expects ($ this ->any ())
162
+ ->method ('extract ' )
163
+ ->willReturnCallback (
164
+ function ($ path , $ catalogue ) use ($ extractedMessages ) {
165
+ $ catalogue ->add ($ extractedMessages );
166
+ }
167
+ );
168
+ }
159
169
160
170
$ loader = $ this ->createMock (TranslationReader::class);
161
171
$ loader
@@ -182,7 +192,7 @@ function ($path, $catalogue) use ($loadedMessages) {
182
192
$ kernel
183
193
->expects ($ this ->any ())
184
194
->method ('getBundles ' )
185
- ->willReturn ([] );
195
+ ->willReturn ($ bundles );
186
196
187
197
$ container = new Container ();
188
198
$ kernel
@@ -195,7 +205,7 @@ function ($path, $catalogue) use ($loadedMessages) {
195
205
$ application = new Application ($ kernel );
196
206
$ application ->add ($ command );
197
207
198
- return new CommandTester ( $ application ->find ('debug:translation ' ) );
208
+ return $ application ->find ('debug:translation ' );
199
209
}
200
210
201
211
private function getBundle ($ path )
@@ -209,4 +219,55 @@ private function getBundle($path)
209
219
210
220
return $ bundle ;
211
221
}
222
+
223
+ /**
224
+ * @dataProvider provideCompletionSuggestions
225
+ */
226
+ public function testComplete (array $ input , array $ expectedSuggestions )
227
+ {
228
+ $ extractedMessagesWithDomains = [
229
+ 'messages ' => [
230
+ 'foo ' => 'foo ' ,
231
+ ],
232
+ 'validators ' => [
233
+ 'foo ' => 'foo ' ,
234
+ ],
235
+ 'custom_domain ' => [
236
+ 'foo ' => 'foo ' ,
237
+ ],
238
+ ];
239
+ $ extractor = $ this ->createMock (ExtractorInterface::class);
240
+ $ extractor
241
+ ->expects ($ this ->any ())
242
+ ->method ('extract ' )
243
+ ->willReturnCallback (
244
+ function ($ path , $ catalogue ) use ($ extractedMessagesWithDomains ) {
245
+ foreach ($ extractedMessagesWithDomains as $ domain => $ message ) {
246
+ $ catalogue ->add ($ message , $ domain );
247
+ }
248
+ }
249
+ );
250
+
251
+ $ tester = new CommandCompletionTester ($ this ->createCommand ([], [], null , [], [], $ extractor , [new BaseBundle ()]));
252
+ $ suggestions = $ tester ->complete ($ input );
253
+ $ this ->assertSame ($ expectedSuggestions , $ suggestions );
254
+ }
255
+
256
+ public function provideCompletionSuggestions ()
257
+ {
258
+ yield 'locale ' => [
259
+ ['' ],
260
+ Locales::getLocales (),
261
+ ];
262
+
263
+ yield 'bundle ' => [
264
+ ['fr ' , '--domain ' , 'messages ' , '' ],
265
+ ['BaseBundle ' ],
266
+ ];
267
+
268
+ yield 'option --domain ' => [
269
+ ['en ' , '--domain ' , '' ],
270
+ ['messages ' , 'validators ' , 'custom_domain ' ],
271
+ ];
272
+ }
212
273
}
0 commit comments