How to debug php cli – artisan commands or unit tests

Share on facebook
Share on twitter
Share on linkedin

Introduction

If we develop an application with TDD methodology (which we should do!), we write a lot of code which runs in the command line. Also when we create tasks or artisan commands, debugging the code in command line would make our life easier. In this post, I’ll show how to set up cli debugging with PhpStorm and Homestead.

Prerequisites

You’d need to have Xdebug extension installed and enabled for php cli. Please note that I’ll use php 7.2 in paths, but you may need to replace it with your php version.

Xdebug configuration

Open the xdebug configuration file: sudo nano /etc/php/7.2/cli/conf.d/20-xdebug.ini
Put the following configuration in the file:

There are two important configuration keys above, which might change depending on your configuration:
xdebug.remote_host – this is the host machine’s IP address (on the virtualbox network)
xdebug.idekey – This is a custom key which should be used in the PhpStorm as well

PhpStorm configuration

Open the debug configurations and add new PHP Remote Debug configuration, set up the server (if you need guidance on how to create new server, please check out my previous post on debugging, here: https://42coders.com/how-to-set-up-debugging-with-phpstorm-and-homestead/ ), and the IDE key, which should be the same as what you’ve set up in the xdebug configuration:

Start the debugging session as usual by clicking on the green bug button in the top right corner, set breakpoints in the code. When you start the command line script, PhpStorm connects to the xdebug automatically, and you can debug your code.

Happy debugging!

Subscribe to our newsletter

Be first to read new helpful post

Share this post with your friends

Share on linkedin
Share on xing
Share on twitter
Share on facebook

Leave a Reply

avatar
  Subscribe  
Notify of