我试图用交互式输入测试我的控制台命令。所以我写了一个函数来改变问题助手的输入流。
protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
fwrite($stream, $input);
rewind($stream);
return $stream;
}
这是我的代码失败
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:已中止 /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
答案 0 :(得分:2)
\n
不起作用,例如它们显示在print
中。
将'No\\nNo\\n'
更改为"No\nNo\n"
,它应该有效。
我更喜欢的另一个:sprintf('No%1$sNo%1$s', PHP_EOL)