【问题标题】:Laravel / Symfony - How can I test confirmations in console commands?Laravel / Symfony - 如何在控制台命令中测试确认?
【发布时间】:2014-09-08 17:49:54
【问题描述】:

我知道我可以通过像这样传递参数和选项来对控制台命令进行单元测试:

$command->run(new ArrayInput($data), new NullOutput);

但是,如果我想使用confirm() method in Laravel 向我的命令添加确认对话框怎么办?

【问题讨论】:

    标签: php unit-testing symfony laravel laravel-4


    【解决方案1】:

    您是否阅读过 Symfony 网站上的示例?

    http://symfony.com/doc/current/components/console/helpers/dialoghelper.html#testing-a-command-which-expects-input

    如果您有但仍然无法使用它,请告诉我们。

    【讨论】:

    • 哦,我错过了!非常感谢您的链接,找出解决方案至关重要:)
    • @AndreaMarcoSartori 您能否将此答案标记为解决方案?
    【解决方案2】:

    我忘了提到我正在使用 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 2016-11-28
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      相关资源
      最近更新 更多