How to debug php cli – artisan commands or unit tests
Daniel Verner -
If we develop an application with TDD methodology (which we should do!), we write a lot of code that runs in the command line. Also when we create tasks or artisan commands, debugging the code in the 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 an 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 a 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.