Debugging CLI-Applications with Xdebug

Debugging CLI-Applications with Xdebug

I’m using Xdebug to debug Web-applications for a while now, but I never got Xdebug running to debug CLI-application (e.g. Unittests). I found some howtos on the internet, but I always screwed up getting it to work. This was very annoying and this week I made another attempt. And finally I was sucessful with that. Read more about getting a Xdebug session running for CLI-applications with Netbeans 7.

If you know the solution, it’s seems to be a very easy solution: It’s all about a bash-script that gets the script that you want to test as an argument. This script encapsulates all the needed Xdebug settings and it looks like this:

Nothing more, nothing less.

An example: Let’s say the script above is called “cli_debug.sh”, and let’s also say you want to debug a script called “myscript.php”. Then you simply can debug by starting the debug session in Netbeans first (ensure that your idekey is equal to the configured), and then call:

If you have set a breakpoint the program execution will stop there and everything is like expected and known from debugging webapplications.

You can also debug scripts with parameters because the bash script just passes all parameters to the php-cli interpreter:

So, I’m finally very glad, that this works, so I can use debugging for unittests now. It works on a development-system (Debian Squeeze, PHP 5.3 Xdebug 2.1.2).