How To Test Artisan Commands in Laravel 5

2019-04-17 19:15发布

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.

Anyone an idea how to write it?

3条回答
forever°为你锁心
2楼-- · 2019-04-17 20:04

It is much easier now:

<?php

class YourCommandTest extends TestCase
{

    public function testExample()
    {
        $this->artisan('command', ['param' => 'value']);
    }

}
查看更多
【Aperson】
3楼-- · 2019-04-17 20:09

Maybe useful for someone

Artisan command Test Cases in Laravel 5.7

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

查看更多
Root(大扎)
4楼-- · 2019-04-17 20:14

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);
    }

}
查看更多
登录 后发表回答