Skip to content

Commit fa74e60

Browse files
wirwolfDavertMik
authored andcommitted
Add runSymfonyConsoleCommand (#5588)
* Add runSymfonyConsoleCommand * Fix compatebility * Update Symfony.php
1 parent 391a16b commit fa74e60

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/Codeception/Module/Symfony.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,41 @@ public function grabService($service)
532532
}
533533
return $container->get($service);
534534
}
535+
536+
/**
537+
* Run Symfony console command, grab response and return as string.
538+
* Recommended to use for integration or functional testing.
539+
*
540+
* ``` php
541+
* <?php
542+
* $result = $I->runSymfonyConsoleCommand('hello:world', '--verbose' => 3]);
543+
* ?>
544+
* ```
545+
*
546+
* @param string $command
547+
* @param mixed[] $params
548+
*
549+
* @return string
550+
*
551+
* @throws \Exception
552+
*/
553+
public function runSymfonyConsoleCommand(string $command, array $params = [])
554+
{
555+
$application = new Application($this->kernel);
556+
$application->setAutoExit(false);
557+
$params['command'] = $command;
558+
559+
$input = new ArrayInput($params);
560+
$output = new BufferedOutput();
561+
$code = $application->run($input, $output);
562+
563+
// return the output, don't use if you used NullOutput()
564+
$content = $output->fetch();
565+
566+
$this->assertEquals(0, $code, 'Exit code in '.$command.' is not equal 0 :'.$content);
567+
568+
return $content;
569+
}
535570

536571
/**
537572
* @return \Symfony\Component\HttpKernel\Profiler\Profile

0 commit comments

Comments
 (0)