Ashura Ashura - 5 months ago 56
PHP Question

Symfony interactive Command Test ends with RuntimeException

I'm trying to test my console command with interactive input. So i wrote a function to change the inputstream of the question helper.

protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
fwrite($stream, $input);
rewind($stream);

return $stream;
}


Here is my code that fails

public function testRunCommandWithoutArguments()
{
self::bootKernel();
$application = new Application(self::$kernel);
$application->setAutoExit(false);
$application->add(new InstallCommand());

$command = $application->find('app:install');
$commandTester = new CommandTester($command);
$helper = $command->getHelper('question');
/** @var QuestionHelper $helper */
$helper->setInputStream($this->getInputStream('No\\nNo\\n'));

$commandTester->execute(array('command' => $command->getName()));
}



RuntimeException : Aborted
/Users/Ashura/Documents/Projects/CustomFramework/vendor/symfony/symfony/src/Symfony/Component/Console/Helper/QuestionHelper.php:135
/Users/Ashura/Documents/Projects/CustomFramework/vendor/symfony/symfony/src/Symfony/Component/Console/Helper/QuestionHelper.php:56
/Users/Ashura/Documents/Projects/CustomFramework/src/AppBundle/Command/InstallCommand.php:96
/Users/Ashura/Documents/Projects/CustomFramework/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:256
/Users/Ashura/Documents/Projects/CustomFramework/vendor/symfony/symfony/src/Symfony/Component/Console/Tester/CommandTester.php:80
/Users/Ashura/Documents/Projects/CustomFramework/tests/AppBundle/Command/InstallCommandTest.php:79

Answer

\n in single quote don't work, there are displayed in a print for instance.

Change 'No\\nNo\\n' to "No\nNo\n" and it should work.

Another I prefer: sprintf('No%1$sNo%1$s', PHP_EOL)