Skip to content

Commit

Permalink
refactor: runSymfonyConsoleCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
eortiz-tracktik committed Apr 3, 2020
1 parent 7533877 commit 7850c66
Showing 1 changed file with 26 additions and 22 deletions.
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

0 comments on commit 7850c66

Please sign in to comment.