DEV Community

Daniel Werner
Daniel Werner

Posted on • Originally published at 42coders.com on

2 2

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
Enter fullscreen mode Exit fullscreen mode

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay