Testing that an artisan command is registered in Laravel

Last updated 24th June, 2022

I recently came across a piece of functionality that I couldn't find in the docs - testing if a command I created was **actually **registered in the kernel. Here's how you can write that test:

it('registers a command', function () {
    expect(collect(Artisan::all()))
        ->has('namespace:command')
        ->toBeTrue();
});

It seems a little bit unnecessary that we have to pull out a list of every registered command to check if ours is there, but there's currently no method in the Laravel framework that allows you to directly check if a command is registered on the Artisan facade.

So instead, to keep things nice and fluent we use a Laravel collection to build a collection of the commands registered, and use the has method to check if the one we're looking for exists.

Maybe someday soon someone will add an Artisan::hasCommand() method to the kernel!

No comments yet…

DevInTheWild.

Login To Add Comments.

Want More Like This?

Subscribe to occasional updates.