Homestead

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:
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 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.

Happy debugging!

Tags: Homestead · PHPStorm · Debugging · artisan · cli

Want products news and updates?

Sign up for our newsletter to stay up to date.

We care about the protection of your data. Read our Privacy Policy.

Impressions from our Team

  • #Office #Garden - #

  • #workhard - #

  • #belgrade #skyline - #

  • #happybirthday Phil :) - #

  • #happybirthday Stefan 🥂 - #

  • #happybirthday Lidija 🍾 - #

  • Say hi 👋 to our newest team member ☕️ - #

  • #bithday #cake 😻 - #

  • #stayathome #homeoffice #42coders - #

  • #stayathome #homeoffice #42coders #starwars :) - #

  • #stayathome #homeoffice #42coders - #

  • We had a really nice time with #laracononline #laravel - #

  • Happy Birthday 🎂 Miloš - #

  • Happy Birthday 🎂Nikola - #

  • #42coders #christmas #dinner what a nice evening :) - #

  • Happy Birthday 🎂 Ognjen - #

  • Wish you all a merry Christmas 🎄🎁 - #

  • #happybirthday Luka! - #

  • https://communian.com Triff dich mit Menschen aus deiner Umgebung die deine Interessen teilen :) Wir sind gerade live gegangen und freuen uns über jede neue Community die zu uns kommt ❤️ - #

  • Nice #meetup at #myposter thanks! - #

© 2020 42coders All rights reserved.