|
9 | 9 | use Codeception\Lib\Connector\Symfony as SymfonyConnector;
|
10 | 10 | use Codeception\Lib\Interfaces\DoctrineProvider;
|
11 | 11 | use Codeception\Lib\Interfaces\PartedModule;
|
| 12 | +use Symfony\Component\Console\Tester\CommandTester; |
12 | 13 | use Symfony\Component\Finder\Finder;
|
13 | 14 | use Symfony\Component\DependencyInjection\ContainerInterface;
|
14 | 15 | use Symfony\Component\Finder\SplFileInfo;
|
@@ -545,42 +546,45 @@ public function grabService($service)
|
545 | 546 | }
|
546 | 547 | return $container->get($service);
|
547 | 548 | }
|
548 |
| - |
| 549 | + |
549 | 550 | /**
|
550 | 551 | * Run Symfony console command, grab response and return as string.
|
551 | 552 | * Recommended to use for integration or functional testing.
|
552 | 553 | *
|
553 | 554 | * ``` php
|
554 | 555 | * <?php
|
555 |
| - * $result = $I->runSymfonyConsoleCommand('hello:world', '--verbose' => 3]); |
| 556 | + * $result = $I->runSymfonyConsoleCommand('hello:world', ['arg' => 'argValue', 'opt1' => 'optValue'], ['input']); |
556 | 557 | * ?>
|
557 | 558 | * ```
|
558 | 559 | *
|
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 |
563 | 564 | *
|
564 |
| - * @throws \Exception |
| 565 | + * @return string Returns the console output of the command |
565 | 566 | */
|
566 |
| - public function runSymfonyConsoleCommand(string $command, array $params = []) |
| 567 | + public function runSymfonyConsoleCommand($command, $parameters = [], $consoleInputs = [], $expectedExitCode = 0) |
567 | 568 | {
|
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 | + ); |
580 | 585 |
|
581 |
| - return $content; |
| 586 | + return $output; |
582 | 587 | }
|
583 |
| - |
584 | 588 | /**
|
585 | 589 | * @return \Symfony\Component\HttpKernel\Profiler\Profile
|
586 | 590 | */
|
|
0 commit comments