forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleAssertionsTrait.php
More file actions
106 lines (91 loc) · 3.84 KB
/
Copy pathConsoleAssertionsTrait.php
File metadata and controls
106 lines (91 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;
use function in_array;
use function sprintf;
trait ConsoleAssertionsTrait
{
/**
* Run Symfony console command, grab response and return as string.
* Recommended to use for integration or functional testing.
*
* ```php
* <?php
* $result = $I->runSymfonyConsoleCommand('hello:world', ['arg' => 'argValue', 'opt1' => 'optValue'], ['input']);
* ```
*
* @param string $command The console command to execute.
* @param array<string, int|string> $parameters Arguments and options passed to the command
* @param list<string> $consoleInputs Inputs for interactive questions.
* @param int $expectedExitCode Expected exit code.
* @return string Console output (stdout).
*/
public function runSymfonyConsoleCommand(
string $command,
array $parameters = [],
array $consoleInputs = [],
int $expectedExitCode = 0
): string {
$kernel = $this->grabKernelService();
$application = new Application($kernel);
$consoleCommand = $application->find($command);
$commandTester = new CommandTester($consoleCommand);
$commandTester->setInputs($consoleInputs);
$input = ['command' => $command] + $parameters;
$options = $this->configureOptions($parameters);
$exitCode = $commandTester->execute($input, $options);
$output = $commandTester->getDisplay();
$this->assertSame(
$expectedExitCode,
$exitCode,
sprintf('Command exited with %d instead of expected %d. Output: %s', $exitCode, $expectedExitCode, $output)
);
return $output;
}
/**
* @param array<string, int|string|bool> $parameters
* @return array<string, mixed> Options array supported by CommandTester.
*/
private function configureOptions(array $parameters): array
{
$options = [];
if (in_array('--ansi', $parameters, true)) {
$options['decorated'] = true;
} elseif (in_array('--no-ansi', $parameters, true)) {
$options['decorated'] = false;
}
if (in_array('--no-interaction', $parameters, true) || in_array('-n', $parameters, true)) {
$options['interactive'] = false;
}
if (in_array('--quiet', $parameters, true) || in_array('-q', $parameters, true)) {
$options['verbosity'] = OutputInterface::VERBOSITY_QUIET;
$options['interactive'] = false;
}
if (in_array('-vvv', $parameters, true)
|| in_array('--verbose=3', $parameters, true)
|| (isset($parameters['--verbose']) && $parameters['--verbose'] === 3)
) {
$options['verbosity'] = OutputInterface::VERBOSITY_DEBUG;
} elseif (in_array('-vv', $parameters, true)
|| in_array('--verbose=2', $parameters, true)
|| (isset($parameters['--verbose']) && $parameters['--verbose'] === 2)
) {
$options['verbosity'] = OutputInterface::VERBOSITY_VERY_VERBOSE;
} elseif (in_array('-v', $parameters, true)
|| in_array('--verbose=1', $parameters, true)
|| in_array('--verbose', $parameters, true)
|| (isset($parameters['--verbose']) && $parameters['--verbose'] === 1)
) {
$options['verbosity'] = OutputInterface::VERBOSITY_VERBOSE;
}
return $options;
}
protected function grabKernelService(): KernelInterface
{
return $this->grabService('kernel');
}
}