Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Xdebug PHPUnit Tests from within VM - CLI and/or PHPStorm #716

Closed
justinlevi opened this issue Jun 9, 2016 · 15 comments
Closed

Xdebug PHPUnit Tests from within VM - CLI and/or PHPStorm #716

justinlevi opened this issue Jun 9, 2016 · 15 comments
Labels

Comments

@justinlevi
Copy link
Contributor

I realize this is a bit off topic but thought I'd throw out the question here anyway. I'm writing some PHPUnit tests for a D8 custom module and doing the development work on the VM. XDebug is setup and working fine for breakpoints if I load the site through my browser. However, I'm running into an exception for one of my tests and I'd like to debug within PHPStorm and I don't want to run my tests by using the testing module. I'm not even sure if that would work in the first place...

I've tried to create quite a few different PHPStorm debug configurations both using the default PHPUnit configuration and the php script configuration with no luck. I've also tried to run my tests from the command line, which works fine, but I can't get XDebug to trip.

Here's the command line I'm trying to run

export XDEBUG_CONFIG="idekey=PHPSTORM"; sudo php docroot/core/scripts/run-tests.sh --url http://dvm.dev --verbose --color
--class "Drupal\soapwebservice\Tests\SoapWebServiceTest"

Here's a shell script I created to make my life a bit easier

#!/usr/bin/env bash 
export XDEBUG_CONFIG="idekey=PHPSTORM" 
echo $1 
php /var/www/dvm/docroot/core/scripts/run-tests.sh --url http://dvm.dev --verbose --color --execute-test $1

source: http://vlikin.blogspot.com/2013/11/debuggingtracing-drupal-8-tests.html


I've also tried setting up a debug configuration in PHPStorm with the following settings, per slide 22 of the talk below:
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
http://www.slideshare.net/FranciscoSeva/xdebug-and-drupal

debug-tests-run-tests

Here's the thing that's totally throwing me off though. This site is a Bolt derivative, so all dependencies are managed with composer, and the tests within /var/www/dvm/tests/phpunit all run as expected in PHPStorm. I was thinking this could be an Autoloader issue but I tried to run google_analytics tests from within PHPStorm and I'm getting the same errors.

What is the the workflow for debugging(xdebug) PHPUnit tests for a custom module from within PHPStorm? If I can get this all setup and working I will definitely do a blog post on this one...

@geerlingguy
Copy link
Owner

This is a question where I can't help too terribly much, since I'm not using PHPStorm (though I've considered starting many times now...). Hopefully someone else can chime in and help?

@justinlevi
Copy link
Contributor Author

justinlevi commented Jun 9, 2016

ok, I feel a little silly - seems that xdebug was not installed for some reason. I updated my config.yml settings to the following (as I've done many times before) :

# XDebug configuration. XDebug is disabled by default for better performance.
php_xdebug_default_enable: 1
php_xdebug_coverage_enable: 1
php_xdebug_cli_enable: 1
php_xdebug_remote_enable: 1
php_xdebug_remote_connect_back: 1

Then did a vagrant reload --provision and just assumed xdebug was running. From within a vagrant ssh session, I randomly decided to run :

php -i | grep xdebug

And sure enough, I got nothing back...

Switching the following in my config.yml got xdebug back up and running

php_packages:
  - php5

to

php_packages:
  -php5-xdebug

I don't think the PHPStorm PHPUnit debug configuration is going to work though because it still doesn't seem to find any classes when I click, "Choose Test Class". But oddly, PHPStorm does find the Test Class for a test at the project root that subclasses TestBase.php

https://github.com/acquia/blt/blob/8.x/template/tests/phpunit/src/TestBase.php

In fact, all of the tests here seem to work correctly from within PHPStorm without any issue.
https://github.com/acquia/blt/tree/8.x/template/tests/phpunit/Bolt

This feels like an AutoLoad issue to me but I'm just guessing. I'll try again from the command line now that I have xdebug working again.

Update: Created a note in the documentation about this.
#717

@justinlevi
Copy link
Contributor Author

justinlevi commented Jun 9, 2016

@geerlingguy Any ideas why my config settings for xdebug wouldn't be getting applied?

# XDebug configuration. XDebug is disabled by default for better performance.
php_xdebug_default_enable: 1
php_xdebug_coverage_enable: 1
php_xdebug_cli_enable: 1
php_xdebug_remote_enable: 1
php_xdebug_remote_connect_back: 1
# Use PHPSTORM for PHPStorm, sublime.xdebug for Sublime Text.
php_xdebug_idekey: PHPSTORM
php_xdebug_max_nesting_level: 256

Here's what I see when I print out my php settings:

$ php -i | grep xdebug
/etc/php5/cli/conf.d/20-xdebug.ini,
xdebug
xdebug support => enabled
xdebug.auto_trace => Off => Off
xdebug.cli_color => 0 => 0
xdebug.collect_assignments => Off => Off
xdebug.collect_includes => On => On
xdebug.collect_params => 0 => 0
xdebug.collect_return => Off => Off
xdebug.collect_vars => Off => Off
xdebug.coverage_enable => On => On
xdebug.default_enable => On => On
xdebug.dump.COOKIE => no value => no value
xdebug.dump.ENV => no value => no value
xdebug.dump.FILES => no value => no value
xdebug.dump.GET => no value => no value
xdebug.dump.POST => no value => no value
xdebug.dump.REQUEST => no value => no value
xdebug.dump.SERVER => no value => no value
xdebug.dump.SESSION => no value => no value
xdebug.dump_globals => On => On
xdebug.dump_once => On => On
xdebug.dump_undefined => Off => Off
xdebug.extended_info => On => On
xdebug.file_link_format => no value => no value
xdebug.force_display_errors => Off => Off
xdebug.force_error_reporting => 0 => 0
xdebug.halt_level => 0 => 0
xdebug.idekey => no value => no value
xdebug.max_nesting_level => 256 => 256
xdebug.max_stack_frames => -1 => -1
xdebug.overload_var_dump => On => On
xdebug.profiler_aggregate => Off => Off
xdebug.profiler_append => Off => Off
xdebug.profiler_enable => Off => Off
xdebug.profiler_enable_trigger => Off => Off
xdebug.profiler_enable_trigger_value => no value => no value
xdebug.profiler_output_dir => /tmp => /tmp
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.remote_autostart => Off => Off
xdebug.remote_connect_back => Off => Off
xdebug.remote_cookie_expire_time => 3600 => 3600
xdebug.remote_enable => Off => Off
xdebug.remote_handler => dbgp => dbgp
xdebug.remote_host => localhost => localhost
xdebug.remote_log => no value => no value
xdebug.remote_mode => req => req
xdebug.remote_port => 9000 => 9000
xdebug.scream => Off => Off
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.show_mem_delta => Off => Off
xdebug.trace_enable_trigger => Off => Off
xdebug.trace_enable_trigger_value => no value => no value
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.trace_output_dir => /tmp => /tmp
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3

Note that the idekey is not set nor is the remote_enable, or remote_connect_back even though I have them set in my config.yml

@kevinquillen
Copy link

kevinquillen commented Jun 10, 2016

If you are trying to run unit tests from PHPStorm or the command line, I think there was a longstanding issue recently fixed in core that allows this to work now, though I could be mistaken.

I had an issue in the past where my unit tests worked from the UI page in Drupal, but running them from the command line or unit runner tool in PHPStorm did nothing.

One thing that really helps is to make sure that the PHP Interpreter settings are pointing at the package inside of the VM via the Vagrant integration in PHPStorm, and not using the host machine PHP.

https://www.jetbrains.com/help/phpstorm/2016.1/configuring-remote-php-interpreters.html

See: "Configuring a PHP Interpreter in a Vagrant Instance"

This helps me run Behat tests, among many other things.

@justinlevi
Copy link
Contributor Author

@kevinquillen Thanks for the comment.

I'm actually not having any issues running my custom module phpUnit tests from the command line or via the simpletest module. It's just that I'm having no luck getting PHPStorm execute, and more importantly, to pause using xdebug. I'm not a huge fan of printing my instance variables to the screen in order to debug something.

I've put together a much more detailed question detailing the issue here:
http://drupal.stackexchange.com/questions/203801/how-can-i-debug-phpunit-simpletests-for-a-custom-d8-module-in-phpstorm

@geerlingguy
Copy link
Owner

@justinlevi - Have you gotten anywhere with this? I'm not sure if there's anything I can do in Drupal VM in particular to help further the effort, as it looks like it may be more related to how PHPStorm is working with the debugging, and not debugging itself not working correctly in PHP installed in the VM...

@justinlevi
Copy link
Contributor Author

Negative. I ended up giving up on it as I was able to debug my phpunit tests from the drupal testing interface. This is not ideal and I would love to find a solution at some point. I'll close this out.

@geerlingguy
Copy link
Owner

Sounds good. Testing is still not as easy as it should be :(

@kentr
Copy link
Contributor

kentr commented Nov 23, 2016

@justinlevi Am I correct in understanding that your goal is to get a PHP script started via command line inside the VM (run-tests.sh) to break in your debug program on the host (PHPStorm)?

If so, one way I've done this is to set xdebug.remote_autostart = On and set the debug client (PHPStorm in your case) to listen for debug connections. Xdebug then sends a debug connection request when the PHP script is run, and the debug client catches the request and engages.

You may need to also set xdebug.remote_host to the IP of the Vagrant host machine.

@justinlevi
Copy link
Contributor Author

@kentr - I forgot about this thread. Thanks for the idea. That does seem like it would work.

I have since switched to using this plugin which has made this process much easier
https://github.com/mglaman/intellij-drupal-run-tests/

@mirsoftacquia
Copy link

To debug phpunit in phpstorm from drupalvm, prepend following settings before the phpunit command (or any other php-based command):
PHP_IDE_CONFIG="serverName=yourservername-vm.dev" XDEBUG_CONFIG="remote_host=10.0.2.2"

  • replace yourservername-vm.dev with your server name (you will find this in PhpStorm: Languages & Frameworks / PHP / Servers, value of field Name)
  • replace 10.0.2.2 with IP address of your Vagrant host machine

Example (run from docroot/core directory of D8 installation):

Run unit test without debugging:
../vendor/bin/phpunit ../modules/custom/mymodule/test/src/Unit/MyModuleTest.php

Run unit test with debugging enabled:
PHP_IDE_CONFIG="serverName=yourservername-vm.dev" XDEBUG_CONFIG="remote_host=10.0.2.2" ../vendor/bin/phpunit ../modules/custom/mymodule/test/src/Unit/MyModuleTest.php

This assumes that you already have installed and enabled xdebug globally in drupalvm config (php_xdebug_default_enable: 1 in config.yml and xdebug under installed_extras).

This should not only work for phpunit, but also for any other php-based script.

Note however that the best practice is to not have xdebug plugin for CLI loaded and enabled globally in php.ini, but to load xdebug plugin only if you want to debug your script. Otherwise you will get performance penalty on all php-based CLI scripts (including composer, drush etc.), even if you don't intend to debug. For this, xdebug.so must be present on vm, but by default neither enabled nor loaded (then you can use the similar CLI trick with prepending PHP settings that load and enable xdebug.so on demand). If I will have some more time, I will try to set this up on drupalvm somehow.

@japerry
Copy link

japerry commented Mar 1, 2017

To get Drush working, I do the following:

First, I enabled xdebug enabled on drupalvm, and the configured PHPSTORM with a 'PHP Remote Debug' server, pointing to a hostname identified below (ie: testsite.dev)

Then add the following to .bashrc:

export PHP_IDE_CONFIG="serverName=testsite.dev"
export XDEBUG_CONFIG="idekey=PHPSTORM remote_host=10.0.2.2"
export PHP_OPTIONS="-dxdebug.remote_autostart=On -didekey=PHPSTORM -dremote_host=testsite.dev -dprofiler_enable=1"

With this patch, this should bring up the debugger when running drush. YMMV depending on how your PHPSTORM project is setup, but most basic setups should work.

@FinBoWa
Copy link

FinBoWa commented Mar 1, 2017

Wouldnt it be better that ansible generated those lines to the vagrant users bashrc and based them on the config.yaml settings. That way you don't have to export them first on your host.

Im refering to this this #1171

@FinBoWa
Copy link

FinBoWa commented Mar 1, 2017

Iv also added these files https://gist.github.com/FinBoWa/9e54697233284bdf0302a67ccc5f4f81 to help on quickly toggling xdebug on our projects

@oxyc
Copy link
Collaborator

oxyc commented Jun 2, 2017

There are now docs on how to do this from your host machine as well http://docs.drupalvm.com/en/latest/extras/xdebug/#xdebug-over-sshdrush (will be available in the next release).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants