Closed IwfY closed 6 years ago
We may add a variable in order to store the value:
$this->lastExitCode = $application->run($input, $output);
And a method in order to check the exit code:
public function assertSameLastExitCode($expectedExitCode) {
// the following code has to be rewritten
PHPUnit::assertSame($expectedExitCode, $this->lastExitCode);
}
If we can consider BCs (due to #332), we could do a cleaner solution, changing the return value to an object containing both the output and the exit code as gettable properties.
changing the return value to an object containing both the output and the exit code as gettable properties.
Also, as the current result is a string, we may implement __toString
method to the object returning the same thing with backward compatibility keeping.
Using the CommandTester
class may solve the issue (see also #219):
This is what I do as a replacement of runCommand
for the moment:
protected function executeCommand(Command $command, array $arguments = [], $reuseKernel = false)
{
if (!$reuseKernel) {
if (null !== static::$kernel) {
static::$kernel->shutdown();
}
$kernel = static::$kernel = static::createKernel(['environment' => $this->environment]);
$kernel->boot();
} else {
$kernel = $this->getContainer()->get('kernel');
}
$application = new Application($kernel);
$application->add($command);
$command = $application->find($command->getName());
$commandTester = new CommandTester($command);
$commandTester->execute(
array_merge(['command' => $command->getName()], $arguments),
[
'interative' => false,
'decorated' => $this->getDecorated(),
'verbosity' => $this->getVerbosityLevel(),
]
);
return $commandTester;
}
And to use it:
$commandTester = $this->executeCommand(new MyCommand());
static::assertSame(0, $commandTester->getStatusCode());
static::assertSame($display, $commandTester->getDisplay());
Maybe something like this could be implemented for v2 as replacement of the current implementation of runCommand
?
@alexislefebvre what do you think about flagging it for 2.0 as we have seen that it requires some BC break.
This has been fixed in #460.
For some command testing I want to evaluate the exit code returned by it. With the current implementation of runCommand() in WebTestCase this is not possible.
So I basically copied the function and used a reference parameter to pass the command's exit code.
I'd be willing to create a pull request for such functionality if it is wanted in this bundle. I just need some advice how to integrate this feature. Expanding the interface of the current runCommand in the way seen above might not be wanted as it would break backwards compatibility. But having this much duplicate code feels bad too.
Any interest in the feature or preferences how you'd like to have it implemented?