Skip to content

Commit 986268c

Browse files
authored
Merge pull request #7 from eortiz-tracktik/master
refactor: runSymfonyConsoleCommand
2 parents 7533877 + 7850c66 commit 986268c

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/Codeception/Module/Symfony.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Codeception\Lib\Connector\Symfony as SymfonyConnector;
1010
use Codeception\Lib\Interfaces\DoctrineProvider;
1111
use Codeception\Lib\Interfaces\PartedModule;
12+
use Symfony\Component\Console\Tester\CommandTester;
1213
use Symfony\Component\Finder\Finder;
1314
use Symfony\Component\DependencyInjection\ContainerInterface;
1415
use Symfony\Component\Finder\SplFileInfo;
@@ -545,42 +546,45 @@ public function grabService($service)
545546
}
546547
return $container->get($service);
547548
}
548-
549+
549550
/**
550551
* Run Symfony console command, grab response and return as string.
551552
* Recommended to use for integration or functional testing.
552553
*
553554
* ``` php
554555
* <?php
555-
* $result = $I->runSymfonyConsoleCommand('hello:world', '--verbose' => 3]);
556+
* $result = $I->runSymfonyConsoleCommand('hello:world', ['arg' => 'argValue', 'opt1' => 'optValue'], ['input']);
556557
* ?>
557558
* ```
558559
*
559-
* @param string $command
560-
* @param mixed[] $params
561-
*
562-
* @return string
560+
* @param string $command The console command to execute
561+
* @param array $parameters Parameters (arguments and options) to pass to the command
562+
* @param array $consoleInputs Console inputs (e.g. used for interactive questions)
563+
* @param int $expectedExitCode The expected exit code of the command
563564
*
564-
* @throws \Exception
565+
* @return string Returns the console output of the command
565566
*/
566-
public function runSymfonyConsoleCommand(string $command, array $params = [])
567+
public function runSymfonyConsoleCommand($command, $parameters = [], $consoleInputs = [], $expectedExitCode = 0)
567568
{
568-
$application = new Application($this->kernel);
569-
$application->setAutoExit(false);
570-
$params['command'] = $command;
571-
572-
$input = new ArrayInput($params);
573-
$output = new BufferedOutput();
574-
$code = $application->run($input, $output);
575-
576-
// return the output, don't use if you used NullOutput()
577-
$content = $output->fetch();
578-
579-
$this->assertEquals(0, $code, 'Exit code in '.$command.' is not equal 0 :'.$content);
569+
$kernel = $this->grabService('kernel');
570+
$application = new Application($kernel);
571+
$consoleCommand = $application->find($command);
572+
$commandTester = new CommandTester($consoleCommand);
573+
$commandTester->setInputs($consoleInputs);
574+
575+
$parameters = ['command' => $command] + $parameters;
576+
$exitCode = $commandTester->execute($parameters);
577+
$output = $commandTester->getDisplay();
578+
579+
$this->assertEquals(
580+
$expectedExitCode,
581+
$exitCode,
582+
'Command did not exit with code '.$expectedExitCode
583+
.' but with '.$exitCode.': '.$output
584+
);
580585

581-
return $content;
586+
return $output;
582587
}
583-
584588
/**
585589
* @return \Symfony\Component\HttpKernel\Profiler\Profile
586590
*/

0 commit comments

Comments
 (0)