Building and testing Artisan Commands
Mohamed Said has written a very nice tutorial about buiding and testing our own Artisan Commands in Laravel 5.
Introduction
While building the Laravel Langman package I was facing some difficulties trying to figure out how to test a console command that interacts with the user via questions, most of the tutorials and blog posts I found online were just calling the command and passing arguments/options using the artisan() method that comes with laravel's default test suite, however I didn't stumble upon a tutorial that covers user interaction with the console after calling the command.
In this post I'm going to share how I tested such type of commands, but let's first see how powerful artisan commands can be.
Building a console command
First we'll need to create a new command by calling:
php artisan make:console ConfigManager --command=config:manage
This will create a new class in app/Console/Commands with the following content:
class ConfigManager extends Command { protected $signature = 'config:manage'; protected $description = 'Command description'; public function __construct() { parent::__construct(); } public function handle() { // } }
Our command will be used to manage laravel's configuration files, using this command we'll be able to read config files as well as update/add keys.
For us to be able to use the command we're going to make sure we have the right setup for the signature, laravel introduces an easy and simple way of defining a command signat
Link: