如何在Laravel 5.4中使用ask()测试工匠命令

时间:2018-05-09 13:24:37

标签: laravel phpunit laravel-5.4 mockery

尝试使用ask()函数为laravel php artisan命令编写测试。我之前从未使用过嘲弄,但是当我尝试运行测试时,它会冻结,所以我想,我做错了。

MyCommand.php:

public function handle()
    {
        $input['answer1'] = $this->ask('Ask question 1');
        $input['answer2'] = $this->ask('Ask question 2');
        $input['answer3'] = $this->ask('Ask question 3');


        //--- processing validation        
        $validator = Validator::make($input, [
            'answer1' => 'required',
            'answer2' => 'required',
            'answer3' => 'required',

        ]);

        if ($validator->fails()) {
            // processing error
            }
        } else {
            // saving to DB
        }
    }

我的单元测试:

    $command = m::mock('\App\Console\Commands\Questions');


            $command->shouldReceive('ask')              
                  ->andReturn('Answer 1')
                  ->shouldReceive('ask')
                  ->andReturn('Answer 2')
                  ->shouldReceive('ask')
                  ->andReturn('Answer 3')


            $this->artisan('myCommand:toRun');

$this->assertDatabaseHas('myTable', [
            'question1' => 'answer1'
        ]);

//

1 个答案:

答案 0 :(得分:0)

Laravel 5.4-5.6

这里的实际问题是,运行console命令正在等待用户输入,但是我们正在通过PHPUnit运行它,因此我们无法输入任何内容。

在单元测试中碰到限制可能会令人沮丧,但是您发现的限制最终可能是变相的祝福。

当前,您的实现与视图紧密结合(控制台命令,因此是管理员视图,但仍然是视图)。此处可以做的是将任何逻辑放在单独的类中, MyCommand可以利用,PHPUnit可以实际对其进行测试。我们知道,如Laravel单元测试中所示,运行自定义命令的基本原理起作用,因此我们可以在一个单独的可测试类中卸载逻辑。

您的新班级可能看起来像这样:

class CommandLogic
{

    public function getQuestion1Text()
    {
        return 'Ask question 1';
    }

    public function getQuestion2Text()
    {
        return 'Ask question 2';
    }

    public function getQuestion3Text()
    {
        return 'Ask question 3';
    }

    public function submit(array $input)
    {
        $validator = \Illuminate\Support\Facades\Validator::make($input, [
            'answer1' => 'required',
            'answer2' => 'required',
            'answer3' => 'required',
        ]);

        if ($validator->fails()) {
            // processing error
        } else {
            // saving to DB
        }
    }

}

...您的实际单元测试,如下所示:

$commandLogic = new CommandLogic();
$sampleInput = [
    'answer1' => 'test1',
    'answer2' => 'test2',
    'answer3' => 'test3',
];

$commandLogic->submit($sampleInput);
$this->assertDatabaseHas('myTable', [
    'question1' => 'test1'
]);

...以及您的控制台命令,如下所示:

public function handle()
{
    $commandLogic = new CommandLogic();
    $input['answer1'] = $this->ask($commandLogic->getQuestion1Text());
    $input['answer2'] = $this->ask($commandLogic->getQuestion2Text());
    $input['answer3'] = $this->ask($commandLogic->getQuestion3Text());

    $commandLogic->submit($input);
}

这将强制执行single responsibility principle并将代码库中的移动部分分开。我知道这听起来有点像个警察,但是在Laravel 5.4中测试这些东西很困难。如果您愿意升级到5.7或更高版本,请阅读以下内容...


Laravel 5.7 +

Laravel 5.7引入了能够运行控制台测试的功能,它满足了这个问题的确切要求-https://laravel.com/docs/5.7/console-tests。这更多的是完全集成测试,而不是单元测试。