Skip to content

refactor: runSymfonyConsoleCommand #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Codeception\Lib\Connector\Symfony as SymfonyConnector;
use Codeception\Lib\Interfaces\DoctrineProvider;
use Codeception\Lib\Interfaces\PartedModule;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Finder\Finder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Finder\SplFileInfo;
Expand Down Expand Up @@ -545,42 +546,45 @@ public function grabService($service)
}
return $container->get($service);
}

/**
* 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', '--verbose' => 3]);
* $result = $I->runSymfonyConsoleCommand('hello:world', ['arg' => 'argValue', 'opt1' => 'optValue'], ['input']);
* ?>
* ```
*
* @param string $command
* @param mixed[] $params
*
* @return string
* @param string $command The console command to execute
* @param array $parameters Parameters (arguments and options) to pass to the command
* @param array $consoleInputs Console inputs (e.g. used for interactive questions)
* @param int $expectedExitCode The expected exit code of the command
*
* @throws \Exception
* @return string Returns the console output of the command
*/
public function runSymfonyConsoleCommand(string $command, array $params = [])
public function runSymfonyConsoleCommand($command, $parameters = [], $consoleInputs = [], $expectedExitCode = 0)
{
$application = new Application($this->kernel);
$application->setAutoExit(false);
$params['command'] = $command;

$input = new ArrayInput($params);
$output = new BufferedOutput();
$code = $application->run($input, $output);

// return the output, don't use if you used NullOutput()
$content = $output->fetch();

$this->assertEquals(0, $code, 'Exit code in '.$command.' is not equal 0 :'.$content);
$kernel = $this->grabService('kernel');
$application = new Application($kernel);
$consoleCommand = $application->find($command);
$commandTester = new CommandTester($consoleCommand);
$commandTester->setInputs($consoleInputs);

$parameters = ['command' => $command] + $parameters;
$exitCode = $commandTester->execute($parameters);
$output = $commandTester->getDisplay();

$this->assertEquals(
$expectedExitCode,
$exitCode,
'Command did not exit with code '.$expectedExitCode
.' but with '.$exitCode.': '.$output
);

return $content;
return $output;
}

/**
* @return \Symfony\Component\HttpKernel\Profiler\Profile
*/
Expand Down