如何在 Laravel 5 中测试 Artisan 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33611788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Test Artisan Commands in Laravel 5
提问by mnv
I build an Artisan Command to receive data from a socket, and I want to write a unit-testing for this command but I'm not sure how to write such a test.
我构建了一个 Artisan 命令来从套接字接收数据,我想为此命令编写单元测试,但我不确定如何编写这样的测试。
Anyone an idea how to write it?
有人知道怎么写吗?
采纳答案by Roma Rush
It is much easier now:
现在容易多了:
<?php
class YourCommandTest extends TestCase
{
public function testExample()
{
$this->artisan('command', ['param' => 'value']);
}
}
回答by mnv
Example of test
测试示例
<?php
class YourCommandTest extends TestCase
{
public function testExample()
{
Artisan::call('your_command', [
'command_parameter_1' => 'value1',
'command_parameter_2' => 'value2',
]);
// If you need result of console output
$resultAsText = Artisan::output();
$this->assertTrue(true);
}
}
回答by Ajay
Maybe useful for someone
也许对某人有用
Artisan command Test Cases in Laravel 5.7
Laravel 5.7 中的 Artisan 命令测试用例
public function test_console_command()
{
$this->artisan('your_command')
->expectsQuestion('What is your name?', 'Ajay Makwana')
->expectsQuestion('Which language do you program in?', 'PHP')
->expectsOutput('Your name is Ajay Makwana and you program in PHP.')
->assertExitCode(0);
}
https://laravel-news.com/testing-artisan-commands-in-laravel-5-7
https://laravel-news.com/testing-artisan-commands-in-laravel-5-7