DEV Community

Daniel Werner
Daniel Werner

Posted on • Originally published at 42coders.com on

How to debug php cli – artisan commands or unit tests

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:

zend_extension=xdebug.so
xdebug.remote_autostart = 1
xdebug.remote_enable = 1
xdebug.remote_connect_back = 0
xdebug.remote_port = 9000
xdebug.max_nesting_level = 512
xdebug.remote_host = 192.168.10.1
xdebug.idekey = homestead_cli

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!

The post How to debug php cli – artisan commands or unit tests appeared first on 42 Coders.

Oldest comments (0)