9
9
use Symfony \Component \Console \Tester \CommandTester ;
10
10
use Symfony \Component \HttpKernel \KernelInterface ;
11
11
12
+ use function in_array ;
13
+ use function sprintf ;
14
+
12
15
trait ConsoleAssertionsTrait
13
16
{
14
17
/**
@@ -20,40 +23,42 @@ trait ConsoleAssertionsTrait
20
23
* $result = $I->runSymfonyConsoleCommand('hello:world', ['arg' => 'argValue', 'opt1' => 'optValue'], ['input']);
21
24
* ```
22
25
*
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).
28
31
*/
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 );
35
42
$ commandTester ->setInputs ($ consoleInputs );
36
43
37
- $ input = ['command ' => $ command ] + $ parameters ;
38
- $ options = $ this ->configureOptions ($ parameters );
39
-
44
+ $ input = ['command ' => $ command ] + $ parameters ;
45
+ $ options = $ this ->configureOptions ($ parameters );
40
46
$ exitCode = $ commandTester ->execute ($ input , $ options );
41
- $ output = $ commandTester ->getDisplay ();
47
+ $ output = $ commandTester ->getDisplay ();
42
48
43
49
$ this ->assertSame (
44
50
$ expectedExitCode ,
45
51
$ 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 )
52
53
);
53
54
54
55
return $ output ;
55
56
}
56
57
58
+ /**
59
+ * @param array<string, int|string|bool> $parameters
60
+ * @return array<string, mixed> Options array supported by CommandTester.
61
+ */
57
62
private function configureOptions (array $ parameters ): array
58
63
{
59
64
$ options = [];
@@ -69,27 +74,24 @@ private function configureOptions(array $parameters): array
69
74
}
70
75
71
76
if (in_array ('--quiet ' , $ parameters , true ) || in_array ('-q ' , $ parameters , true )) {
72
- $ options ['verbosity ' ] = OutputInterface::VERBOSITY_QUIET ;
77
+ $ options ['verbosity ' ] = OutputInterface::VERBOSITY_QUIET ;
73
78
$ options ['interactive ' ] = false ;
74
79
}
75
80
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 )
80
84
) {
81
85
$ 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 )
86
89
) {
87
90
$ 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 )
93
95
) {
94
96
$ options ['verbosity ' ] = OutputInterface::VERBOSITY_VERBOSE ;
95
97
}
@@ -101,4 +103,4 @@ protected function grabKernelService(): KernelInterface
101
103
{
102
104
return $ this ->grabService ('kernel ' );
103
105
}
104
- }
106
+ }
0 commit comments