w3resource

Laravel (5.7) Console Tests

In the last tutorial we introduced you to HTTP testing, please go through this tutorial if you have not.

This tutorial on Console test will be broken into two: Introduction and expecting input/output.

Introduction

Laravel does not only simplify HTTP testing there is more to it, it also provides a simple API that will help the developer to test console applications that requests input from users.

Expecting Input/Output

The Laravel framework will allow you to easily "mock" user input for your console commands using the expectsQuestion method.

That is not all, Laravel makes it convenient to specify the exit code and text that you want you are expecting to be the output of a particular console command using the assertExitCode and expectsOutput methods. Consider, as an example the following console command:

Artisan::command('question', function () {
    $name = $this->ask('Please what is your name?');
    $location= $this->ask('where do you live');

    $language = $this->choice('Which language do you program in?', [
        'PHP',
        'Ruby',
        'Python',
    ]);

    $this->line('Your name is '.$name.', you live in '.$location.' and you program in '.$language.'.?);
});

You can test the command above with the following test which will utilize the expectsQuestion, expectsOutput, and assertExitCode methods:

 * Test a console command.
 *
 * @return void
 */
public function test_console_command()
{
    $this->artisan('question')
         ->expectsQuestion('What is your name?', 'Aryan Vicky')
         ->expectsQuestion('where do you live', 'New Delhi')
         ->expectsQuestion('Which language do you program in?', 'PHP')
         ->expectsOutput('Your name is Aryan Vicky, you live in New Delhi, and you program in PHP.')
         ->assertExitCode(0);
}

Now let us expand the scope of the test to implement a test for a company info:

Artisan::command('question', function () {
    $name = $this->ask('Please what is your company?s name?');
    $location= $this->ask(?where is your company located?);

    $language = $this->choice('Which language does your company program in?', [
        'PHP',
        'Ruby',
        'Python',
	 'C#',
	 'Java',
    ]);
    $employees=$this->choice(?How many employees does your company have??,[
	'1-10',
	'11-20',
	'21-50',
	'51-100',
	'>100',
	]);
     $revenue= $this->ask('what is your company's annual revenue');
    $this->line('Your company's name is '.$name.', you are located in '.$location.', you have '.$employees.' Employees, your annual revenue is '.$revenue.', and you program in '.$language.'.');
});

You can test the command above with the following test which will utilize the expectsQuestion, expectsOutput, and assertExitCode methods:

 * Test a console command.
 *
 * @return void
 */
public function test_console_command()
{
    $this->artisan('question')
         ->expectsQuestion(' Please what is your company?s name?', 'w3resource')
         ->expectsQuestion('where is your company located?', 'New Delhi')
         ->expectsQuestion('Which language does your company program in?', 'PHP')
	  ->expectsQuestion('How many employees does your company have?', '21-50')
	  ->expectsQuestion('what is your company's annual revenue?', '100000')
->expectsOutput(`Your company's name is w3resource, you are located in New Delhi, you have 21-50 Employees, your annual revenue is 100000, and you program in PHP`);
         ->assertExitCode(0);
}


Share this Tutorial / Exercise on : Facebook and Twitter