我构建了一个Artisan Command来从套接字接收数据,我想为这个命令编写一个单元测试,但我不知道如何编写这样的测试。
任何人都知道怎么写它?
答案 0 :(得分:41)
测试示例
<?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);
}
}
答案 1 :(得分:25)
现在容易得多:
<?php
class YourCommandTest extends TestCase
{
public function testExample()
{
$this->artisan('command', ['param' => 'value']);
}
}
答案 2 :(得分:1)
也许对某人有用
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