99use Symfony \Component \Console \Tester \CommandTester ;
1010use Symfony \Component \HttpKernel \KernelInterface ;
1111
12+ use function in_array ;
13+ use function sprintf ;
14+
1215trait ConsoleAssertionsTrait
1316{
1417 /**
@@ -20,40 +23,42 @@ trait ConsoleAssertionsTrait
2023 * $result = $I->runSymfonyConsoleCommand('hello:world', ['arg' => 'argValue', 'opt1' => 'optValue'], ['input']);
2124 * ```
2225 *
23- * @param string $command The console command to execute
24- * @param array $parameters Parameters (arguments and options) to pass to the command
25- * @param array $consoleInputs Console inputs (e.g. used for interactive questions)
26- * @param int $expectedExitCode The expected exit code of the command
27- * @return string Returns the console output of the command
26+ * @param string $command The console command to execute.
27+ * @param array<string, int|string> $parameters Arguments and options passed to the command
28+ * @param list<string> $consoleInputs Inputs for interactive questions.
29+ * @param int $expectedExitCode Expected exit code.
30+ * @return string Console output (stdout).
2831 */
29- public function runSymfonyConsoleCommand (string $ command , array $ parameters = [], array $ consoleInputs = [], int $ expectedExitCode = 0 ): string
30- {
31- $ kernel = $ this ->grabKernelService ();
32- $ application = new Application ($ kernel );
33- $ consoleCommand = $ application ->find ($ command );
34- $ commandTester = new CommandTester ($ consoleCommand );
32+ public function runSymfonyConsoleCommand (
33+ string $ command ,
34+ array $ parameters = [],
35+ array $ consoleInputs = [],
36+ int $ expectedExitCode = 0
37+ ): string {
38+ $ kernel = $ this ->grabKernelService ();
39+ $ application = new Application ($ kernel );
40+ $ consoleCommand = $ application ->find ($ command );
41+ $ commandTester = new CommandTester ($ consoleCommand );
3542 $ commandTester ->setInputs ($ consoleInputs );
3643
37- $ input = ['command ' => $ command ] + $ parameters ;
38- $ options = $ this ->configureOptions ($ parameters );
39-
44+ $ input = ['command ' => $ command ] + $ parameters ;
45+ $ options = $ this ->configureOptions ($ parameters );
4046 $ exitCode = $ commandTester ->execute ($ input , $ options );
41- $ output = $ commandTester ->getDisplay ();
47+ $ output = $ commandTester ->getDisplay ();
4248
4349 $ this ->assertSame (
4450 $ expectedExitCode ,
4551 $ exitCode ,
46- sprintf (
47- 'Command did not exit with code %d but with %d: %s ' ,
48- $ expectedExitCode ,
49- $ exitCode ,
50- $ output
51- )
52+ sprintf ('Command exited with %d instead of expected %d. Output: %s ' , $ exitCode , $ expectedExitCode , $ output )
5253 );
5354
5455 return $ output ;
5556 }
5657
58+ /**
59+ * @param array<string, int|string|bool> $parameters
60+ * @return array<string, mixed> Options array supported by CommandTester.
61+ */
5762 private function configureOptions (array $ parameters ): array
5863 {
5964 $ options = [];
@@ -69,27 +74,24 @@ private function configureOptions(array $parameters): array
6974 }
7075
7176 if (in_array ('--quiet ' , $ parameters , true ) || in_array ('-q ' , $ parameters , true )) {
72- $ options ['verbosity ' ] = OutputInterface::VERBOSITY_QUIET ;
77+ $ options ['verbosity ' ] = OutputInterface::VERBOSITY_QUIET ;
7378 $ options ['interactive ' ] = false ;
7479 }
7580
76- if (
77- in_array ('-vvv ' , $ parameters , true ) ||
78- in_array ('--verbose=3 ' , $ parameters , true ) ||
79- (isset ($ parameters ["--verbose " ]) && $ parameters ["--verbose " ] === 3 )
81+ if (in_array ('-vvv ' , $ parameters , true )
82+ || in_array ('--verbose=3 ' , $ parameters , true )
83+ || (isset ($ parameters ['--verbose ' ]) && $ parameters ['--verbose ' ] === 3 )
8084 ) {
8185 $ options ['verbosity ' ] = OutputInterface::VERBOSITY_DEBUG ;
82- } elseif (
83- in_array ('-vv ' , $ parameters , true ) ||
84- in_array ('--verbose=2 ' , $ parameters , true ) ||
85- (isset ($ parameters ["--verbose " ]) && $ parameters ["--verbose " ] === 2 )
86+ } elseif (in_array ('-vv ' , $ parameters , true )
87+ || in_array ('--verbose=2 ' , $ parameters , true )
88+ || (isset ($ parameters ['--verbose ' ]) && $ parameters ['--verbose ' ] === 2 )
8689 ) {
8790 $ options ['verbosity ' ] = OutputInterface::VERBOSITY_VERY_VERBOSE ;
88- } elseif (
89- in_array ('-v ' , $ parameters , true ) ||
90- in_array ('--verbose=1 ' , $ parameters , true ) ||
91- in_array ('--verbose ' , $ parameters , true ) ||
92- (isset ($ parameters ["--verbose " ]) && $ parameters ["--verbose " ] === 1 )
91+ } elseif (in_array ('-v ' , $ parameters , true )
92+ || in_array ('--verbose=1 ' , $ parameters , true )
93+ || in_array ('--verbose ' , $ parameters , true )
94+ || (isset ($ parameters ['--verbose ' ]) && $ parameters ['--verbose ' ] === 1 )
9395 ) {
9496 $ options ['verbosity ' ] = OutputInterface::VERBOSITY_VERBOSE ;
9597 }
@@ -101,4 +103,4 @@ protected function grabKernelService(): KernelInterface
101103 {
102104 return $ this ->grabService ('kernel ' );
103105 }
104- }
106+ }
0 commit comments