1414use Symfony \Bundle \MakerBundle \Maker \MakeCommand ;
1515use Symfony \Bundle \MakerBundle \Test \MakerTestCase ;
1616use Symfony \Bundle \MakerBundle \Test \MakerTestRunner ;
17+ use Symfony \Component \HttpKernel \Kernel ;
1718use Symfony \Component \Yaml \Yaml ;
1819
1920class MakeCommandTest extends MakerTestCase
@@ -25,23 +26,25 @@ protected function getMakerClass(): string
2526
2627 public function getTestDetails (): \Generator
2728 {
29+ $ supportsInvokable = Kernel::VERSION_ID >= 70300 ;
30+
2831 yield 'it_makes_a_command_no_attributes ' => [$ this ->createMakerTest ()
29- ->run (function (MakerTestRunner $ runner ) {
32+ ->run (function (MakerTestRunner $ runner ) use ( $ supportsInvokable ) {
3033 $ runner ->runMaker ([
3134 // command name
3235 'app:foo ' ,
33- ]);
36+ ], $ supportsInvokable ? ' --no-invokable ' : '' );
3437
3538 $ this ->runCommandTest ($ runner , 'it_makes_a_command.php ' );
3639 }),
3740 ];
3841
3942 yield 'it_makes_a_command_with_attributes ' => [$ this ->createMakerTest ()
40- ->run (function (MakerTestRunner $ runner ) {
43+ ->run (function (MakerTestRunner $ runner ) use ( $ supportsInvokable ) {
4144 $ runner ->runMaker ([
4245 // command name
4346 'app:foo ' ,
44- ]);
47+ ], $ supportsInvokable ? ' --no-invokable ' : '' );
4548
4649 $ this ->runCommandTest ($ runner , 'it_makes_a_command.php ' );
4750
@@ -54,7 +57,7 @@ public function getTestDetails(): \Generator
5457
5558 yield 'it_makes_a_command_in_custom_namespace ' => [$ this ->createMakerTest ()
5659 ->changeRootNamespace ('Custom ' )
57- ->run (function (MakerTestRunner $ runner ) {
60+ ->run (function (MakerTestRunner $ runner ) use ( $ supportsInvokable ) {
5861 $ runner ->writeFile (
5962 'config/packages/dev/maker.yaml ' ,
6063 Yaml::dump (['maker ' => ['root_namespace ' => 'Custom ' ]])
@@ -63,11 +66,68 @@ public function getTestDetails(): \Generator
6366 $ runner ->runMaker ([
6467 // command name
6568 'app:foo ' ,
66- ]);
69+ ], $ supportsInvokable ? ' --no-invokable ' : '' );
6770
6871 $ this ->runCommandTest ($ runner , 'it_makes_a_command_in_custom_namespace.php ' );
6972 }),
7073 ];
74+
75+ if ($ supportsInvokable ) {
76+ yield 'it_makes_an_invokable_command_by_default ' => [$ this ->createMakerTest ()
77+ ->run (function (MakerTestRunner $ runner ) {
78+ $ runner ->runMaker ([
79+ // command name
80+ 'app:foo ' ,
81+ 'foo ' ,
82+ '' ,
83+ '' ,
84+ ]);
85+
86+ $ this ->runCommandTest ($ runner , 'it_makes_a_command.php ' );
87+
88+ $ commandFileContents = file_get_contents ($ runner ->getPath ('src/Command/FooCommand.php ' ));
89+
90+ self ::assertStringContainsString ('use Symfony\Component\Console\Attribute\AsCommand; ' , $ commandFileContents );
91+ self ::assertStringContainsString ('#[AsCommand( ' , $ commandFileContents );
92+ self ::assertStringNotContainsString ('extends Command ' , $ commandFileContents );
93+ self ::assertStringContainsString ('__invoke( ' , $ commandFileContents );
94+ }),
95+ ];
96+
97+ yield 'it_makes_an_invokable_and_configures_parameters ' => [$ this ->createMakerTest ()
98+ ->run (function (MakerTestRunner $ runner ) {
99+ $ runner ->runMaker ([
100+ // command name
101+ 'app:foo ' ,
102+ 'foo ' ,
103+ 'bar ' , // Argument name
104+ 0 , // Argument type (string)
105+ 'Adds a bar argument to your command ' , // Argument description
106+ 'no ' , // Has no default value
107+ 'yes ' , // Is nullable
108+ 'baz ' , // Second argument, will be required
109+ 1 , // Second type (int)
110+ 'How many bazzes do you need? ' , // Second argument description
111+ 'no ' , // Second argument no default
112+ 'no ' , // Second argument not nullable
113+ '' , // Stop Arguments
114+ 'dry-run ' , // Option name
115+ 'd ' , // Option shortcut
116+ 0 , // Option type (boolean)
117+ 'Perform a dry run? ' ,
118+ 'no ' , // Default value (false)
119+ '' , // Stop option insertion
120+ ]);
121+
122+ $ commandFileContents = file_get_contents ($ runner ->getPath ('src/Command/FooCommand.php ' ));
123+
124+ self ::assertStringContainsString ('__invoke( ' , $ commandFileContents );
125+ self ::assertStringContainsString ('#[Argument(description: \'How many bazzes do you need? \')] int $baz, ' , $ commandFileContents );
126+ self ::assertStringContainsString ('#[Argument(description: \'Adds a bar argument to your command \')] ?string $bar = null ' , $ commandFileContents );
127+ self ::assertStringContainsString ('#[Option(description: \'Perform a dry run? \', shortcut: \'d \')] bool $dryRun = false ' , $ commandFileContents );
128+ }),
129+ ];
130+ }
71131 }
72132
73133 private function runCommandTest (MakerTestRunner $ runner , string $ filename ): void
0 commit comments