From 7850c665eef417042d8d4fac2ec7b53aab0d7f3f Mon Sep 17 00:00:00 2001 From: Ernesto Rodriguez Ortiz Date: Fri, 3 Apr 2020 00:27:32 -0400 Subject: [PATCH] refactor: runSymfonyConsoleCommand --- src/Codeception/Module/Symfony.php | 48 ++++++++++++++++-------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index aa48d34..7fcaa28 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -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; @@ -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 * 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 */