Testing Artisan Commands in Laravel 5.7

Tutorials

August 29th, 2018

laravel-5.7-testing-artisan-featured.png

Laravel 5.7 is going to improve how you write Artisan console commands, with a new API contributed by Laravel employee Mohamed Said:

ICYMI Last week a new PR was merged into laravel 5.7 with a neat way for testing Artisan commands, check details here: https://t.co/36f9FbAhBI pic.twitter.com/pLC3sVLrJP

— Mohamed Said???? (@themsaid) August 27, 2018

You can learn more about the new console testing API is available in the Laravel 5.7 Console Tests Documentation:

1<?php
2
3/**
4 * Test a console command.
5 *
6 * @return void
7 */
8public function test_console_command()
9{
10 $this->artisan('laracon')
11 ->expectsQuestion('What is your name?', 'Taylor Otwell')
12 ->expectsQuestion('Which language do you program in?', 'PHP')
13 ->expectsOutput('Your name is Taylor Otwell and you program in PHP.')
14 ->assertExitCode(0);
15}

Mohamed Said contributed the new testing API in laravel/framework#25270 if you are interested in the implementation details. He also introduced the new style of Testing Artisan Commands in Laravel 5.7+ on his blog, where you can find more information and an example.

Nice work Mohamed!

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.