Using netbeans, xdebug, symfony and phpunit together - php

I installed xdebug on my Apache and if I define a breakpoint in Netbeans, the execution breaks fine. But if I execute the tests with symfony phpunit:test-all, the execution will not break on the given break point.
Any ideas?
cowabunga!

To debug a command-line script, export the XDEBUG_CONFIG variable first, like so:
export XDEBUG_CONFIG="idekey=netbeans-xdebug" (Unix/Linux/OS X)
set XDEBUG_CONFIG=idekey=netbeans-xdebug (Windows)
(Note: I did not test the Windows command. You may have to escape the = character, or the command may look a bit different. If that's the case, I hope someone comes along and corrects me in a comment.)
Explanation: When you open a debugging session for a script that runs through Apache, NetBeans will open your browser to a URL that ends with "XDEBUG_SESSION_START=netbeans-xdebug". When this happens, Xdebug knows to intercept this argument and give your browser a cookie with this debug session information.
The command above is the equivalent for setting the cookie in the command line environment.

When you say you define a breakpoint in Netbeans and it breaks fine, are you talking about running the unit tests from within Netbeans, or running a web application that triggers the breakpoints in Netbeans?
Either way, my first guess would be that the two scenarios are using different PHP.INI files. Get one of your unit tests to dump configuration information and you'll see pretty quickly I daresay. Find the PHP.INI that's being used on the command line and make sure that XDebug is set up for that scenario.
I hope that makes sense. I'm entirely coffee-free at the moment.

I don't use netbeans but I don't think netbeans modifies the file you put break points in and I don't think that symfony can read your netbean configuration to find out where to break.
Also if you are running the test all task inside netbeans and expect the test files to break I don't think that will work either as the task forks php processes I believe and these processes won't be readable by netbeans.
All hypothesis of course.

Related

Debug CLI PHP script in PhpStorm, cannot establish connection to Xdebug 2.6.0

I'm trying to run a CLI script in PhpStorm and use the debugger to step through it.
It took some doing, but I now have Xdebug installed and the PhpStorm CLI Interpreters dialog confirms that:
I created a Run/Debug config and when I try to execute/debug it, the script runs all the way through without breaking at my breakpoints and I get this error at the bottom of PhpStorm:
The PhpStorm docs say that xdebug for CLI scripts can configured with an environment variable like this:
export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"
I've confirmed that PhpStorm sees that environment variable:
Here is my Run/Debug config:
I'm at a complete loss here. How the heck can I get this thing to debug a CLI php script? Any help is appreciated.
Gah, I hate it when I spend a couple of hours trying to fix something and discover it's totally unrelated.
The problem was that I'm using short tags and my cli php.ini file had short_open_tag=Off. The code I'm running is normally run in a web context and the php.ini for that web context had short_open_tag=On so the code was working.
This code is an api endpoint and I needed to be able to step through and see what was going on and there's nothing that inherently requires it to run in the web context (except for the input params, but I'm faking those for testing purposes anyway).
So, my debug configuration was fine.
I discovered it by trying to reduce the problem further and further and I just tried running the code directly from a command line (php sync.php) to see that it was at least working and producing the correct output.
It wasn't producing the normal api json output, it was just echoing all of the code in the file. That's what led me down the path to realizing my stupid mistake.

Why can't I step through PHPunit tests with XDebug when I run them from the command line?

PHP, XDebug, and my IDE (PhpStorm) are all configured properly. PhpStorm is listening, and it's picking up the debug session, but it never hits the breakpoint in my unit test class and I'm unable to step through it. The entire test runs.
Anyone have any ideas?
I can step into PHPUnit tests without a problem using XDebug. However, I did notice that it sometimes seems to skip certain statements (such as simple assignment to variables in a function without anything else), so spread some breakpoints around.
Make sure you've set up path mapping properly, with the correct version of the library mapped over.
If you're using XDebug from the command-line, you have to set certain environment variables - see here and here.
In my case, the following environment variables needed to be set in the remote box and run from the remote box. 10.0.2.2 works OK for Virtualbox VMs - basically you need to point to your local machine.
export XDEBUG_CONFIG="idekey=PHPSTORM remote_host=10.0.2.2 remote_port=9000"
export PHP_IDE_CONFIG="serverName=[yourservernamehere]"

How to debug apis in PHP (without using var_dump();die();)

So I'm writing an api in php, and I'd like to use a full-service debugging tool. i.e., set breakpoints, view stack traces, inspect variables, etc. This kind of thing is very common in compiled languages.
I've checked out xdebug, and after configuring it to work with phpstorm (my IDE), I was disappointed to find that it only works when I run from within phpstorm, not when I actually service real api requests.
To add additional complexity to this, the api dbs are actually hosted on a vagrant instance, so although I write and edit code on my local machine, the code being run is in a virtual machine vagrant environment.
Any other way of doing this? Or should I just get used to something along the lines of print_r();exit; and rerun the request?
Use XDebug, and configure it properly. You can configure it to work without running the code from PhpStorm quite easily.
xdebug.remote_enable = on
xdebug.remote_connect_back = on
xdebug.idekey = "vagrant"
Make sure those are set. Then, in PhpStorm, there is something that looks like a phone icon in the top right (along with the rest of the debugging and running stuff in the toolbar). Make sure that is all green (i.e. listening for connections).
You can use Xdebug to set breakpoints. It'll even work with CLI apps if you've set it up properly. You can even debug code from inside Vagrant VMs, or on remote servers.
You just need to do some research into how to set up your IDE. ;)
More details: http://www.sitepoint.com/install-xdebug-phpstorm-vagrant/
This may come in handy for you: https://www.jetbrains.com/phpstorm/marklets/
And this: https://www.jetbrains.com/phpstorm/help/configuring-xdebug.html
And this: https://confluence.jetbrains.com/display/PhpStorm/Zero-configuration+Web+Application+Debugging+with+Xdebug+and+PhpStorm
Php xdebug
Take a look this example: http://www.sitepoint.com/install-xdebug-phpstorm-vagrant/
best tool available for php debugging is Xdebug. http://xdebug.org/ here is homepage.

Debug the PHP interpreter with GDB

I would like to use GDB to step though the C++ code that makes up the php.so Apache extension. I want to see what PHP is doing while it's running a PHP application. Preferably I would use an IDE like Netbeans or Eclipse on a LAMP system.
You want to get your hands on a debug build of mod_php (with symbols) or build your own (configure --enable-debug)
You should configure your Apache to always keep exactly one worker process instance up (which will be the instance you debug), that is, set MinSpareServers, MaxSpareServers and StartServers all to 1. Also make sure any timeout parameters are generously set
Use gdb or any graphical interface to gdb (such as ddd or Eclipse CDT) to attach to the one and only Apache worker process. Stick a breakpoint in one of the PHP sources etc. and continue.
Point your browser to your webserver and access a PHP page. Your breakpoint will trigger. If you want to wake the debugger at a particular point in your PHP script execution, generate a SIGTRAP from PHP and gdb will normally oblige you.
Have fun!
Maybe you could do that on windows.
However, your best bet is to do this on a Unix box. You will have to compile everything with debugging enabled. GDB will need access to those directories for source.
Then you will have to run apache and then run the process.
In order to give yourself time to attache while you are hitting the PHP/Apache with a browser, add a sleep call in the PHP script. If you ps, you will see the process in the sleep state. Or you could just have it write its process id to a file in tmp before it does the sleep.

PHP - Print all statements that are executed in a PHP command line script?

In python, one can trace all the statements that are executed by a command line script using the trace module. In bash you can do the same with set -x. We have a PHP script that we're running from the command line, like a normal bash / python / perl / etc script. Nothing web-y is going on.
Is there anyway to get a trace of all the lines of code that are being executes?
There is a PECL extension, apd, that will generate a trace file.
Not in pure-PHP, no -- as far as i know.
But you can use a debugger ; a nice way to do that is with
The extension Xdebug, which can be used as a debugger
and some graphical IDE that integrates some debugging tools, like Eclipse PDT
Both of those are free, btw.
With those, you can do step by step, set up breakpoints, watch the content of variables, view stack traces, ... And it works both for Web and CLI scripts ;-)
Of course, it means having Eclipse running on the machine you are executing your script... But if you are executing it on your development machine, you probably have a GUI and all that, so it should be fine...
(I know that, for web applications, you can have Eclipse running on a different machine than the one with the PHP webserver -- don't know if it's possible in CLI, though)
As a sidenote : maybe you can integrate Xdebug with a CLI-based debugger ; see the page I linked to earlier for a list of supported tools.
I'm kinda blind here but I guess one way you could do it is to write all the relevant code inside custom functions and call debug_backtrace(). debug_print_backtrace may also be useful.
I hope it helps.

Categories