Source: www.csrhymes.com

Testing a Laravel Console Command

Category: Laravel

In this example we are going to make a console command to import some products into our database from a csv file and then test they are all added as expected.

In this newly created class, we are going to use the Spatie Simple Excel package to read the csv file and then import the products. We are going to use the product_code as the unique identifier and we are going to update or create the product name and description.

To run the console command manually we can run the following: Next we can create a test using artisan.

Now we have tested without a file and cancelling the command, we can test running the command from start to finish and ensure that it outputs all the messages we expect to see and returns the correct exit code.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners