【发布时间】:2014-09-08 17:49:54
【问题描述】:
我知道我可以通过像这样传递参数和选项来对控制台命令进行单元测试:
$command->run(new ArrayInput($data), new NullOutput);
但是,如果我想使用confirm() method in Laravel 向我的命令添加确认对话框怎么办?
【问题讨论】:
标签: php unit-testing symfony laravel laravel-4
我知道我可以通过像这样传递参数和选项来对控制台命令进行单元测试:
$command->run(new ArrayInput($data), new NullOutput);
但是,如果我想使用confirm() method in Laravel 向我的命令添加确认对话框怎么办?
【问题讨论】:
标签: php unit-testing symfony laravel laravel-4
您是否阅读过 Symfony 网站上的示例?
如果您有但仍然无法使用它,请告诉我们。
【讨论】:
我忘了提到我正在使用 PHPSpec 进行单元测试。
最后我想出了如何用它来测试确认。
这些是use 语句:
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\HelperSet;
这是一个示例测试:
public function it_fires_the_command(QuestionHelper $question, HelperSet $helpers)
{
// $data is an array containing arguments and options for the console command
$input = new ArrayInput($data);
$output = new NullOutput;
// $query must be an instance of ConfirmationQuestion
$query = Argument::type('Symfony\Component\Console\Question\ConfirmationQuestion');
// with "willReturn" we can decide whether (TRUE) or not (FALSE) the user confirms
$question->ask($input, $output, $query)->willReturn(true);
// we expect the HelperSet to be invoked returning the mocked QuestionHelper
$helpers->get('question')->willReturn($question);
// finally we set the mocked HelperSet in our console command...
$this->setHelperSet($helpers);
// ...and run it
$this->run($input, $output);
}
【讨论】: