3

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

Ashura
  • 86
  • 10

1 Answers1

4

\n in single quote don't work, they 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)

chalasr
  • 12,971
  • 4
  • 40
  • 82
  • `\n` in single quote doesn't work, there as displayed in a `print` for instance. – chalasr Jun 08 '16 at 20:44
  • 1
    Take the exact string I give. You are escaping \\n in your code, you must write "No\nNo\n" and, if your command doesn't need more than the two inputs you give it, it will work. How many questions does your command have? Confirmation included. – chalasr Jun 09 '16 at 06:46
  • Thank you very much. I didn't expect that to actually work. Is there any explaination i can read on? – Ashura Jun 09 '16 at 07:04
  • Sure http://stackoverflow.com/questions/2531969/print-newline-in-php-in-single-quotes – chalasr Jun 09 '16 at 07:10
  • So the reason why it didn't work is, the single quotes entered the string \n but the newline character was needed. Isn't it? – Ashura Jun 09 '16 at 07:22
  • 1
    Yes, as your question already contains the \n I assumed that you already know that the command looks for breakline for separate each input. You can read this here http://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input – chalasr Jun 09 '16 at 11:50
  • I already read the documentation. But there are singlequotes used aswell. – Ashura Jun 09 '16 at 11:52
  • You're right, I proposed [a fix](https://github.com/symfony/symfony-docs/pull/6644). – chalasr Jun 09 '16 at 12:29