The following article is valid for unix based systems like Ubuntu Linux. It teaches you, how to setup your PhpStorm IDE to debug your unit tests via the Xdebug PHP extension in Drupal 8.
- Make sure you have read the article at https://www.drupal.org/docs/8/phpunit/running-phpunit-tests to understand how to run automated tests in Drupal 8.
- Make sure you have read the article at https://www.jetbrains.com/help/phpstorm/2017.1/configuring-xdebug.html to understand how to setup the Xdebug PHP extension on your system and configure it within PhpStorm.
phpunit.xml Configuration File
For the PhpStorm config you need to make sure to have the phpunit.xml file created by copying the ~/PhpstormProjects/drupal-8.x/core/phpunit.xml.dist into the same directory. Afterwards you must set the environment variables for
- Example value: http://d8-4x.dev
- Example value: mysql://user:password@localhost/d8-4.x
- Example value: ~/PhpstormProjects/drupal-8.4.x-dev/sites/simpletest/browser_output
- Example value: serverName=d8-4x.dev
- Be careful in this line! Here you set a key-value pair in the value field. Do not use any quotes here. The full line entry in my config is: <env name="PHP_IDE_CONFIG" value="serverName=d8-4x.dev"/>
- Example value: idekey=PHPSTORM
- Again: a key-value pair as value.
in your newly created phpunit.xml file.
PHPUnit Config in Your PhpStorm Project
Make sure you have configured:
- path to the autoloader script from composer. It is located in ~/PhpstormProjects/drupal-8.4/vendor/autoload.php
- path to your phpunit.xml file
The config window must look according to the following screenshot (be careful about the paths on your system):
Define the Server Setting and Map the Tests Directory
Set exactly the same value, which you have previously set in your phpunit.xml file inside the PHP_IDE_CONFIG environment variable for your server. You must also map the directory where your tests are. Do your settings according to the screenshot below.
Path to CLI PHP Interpreter
Set the path to the PHP interpreter which is running your command line based scripts. It is usually located at /usr/bin/php. The config must look like in the following screenshots.
Run a Test File
For being able to execute your test quickly, open the PHP file with your test and hit the debug icon.
Step Into Your PHP Code
Chances are, that PhpStorm will output a error message like
Remote file path '/home/your-user/PhpstormProjects/drupal-8.4.x-dev/-' is not mapped to any file path in project.
You can ignore this error and just click the "resum program" button. PhpStorm will reveal the PHP interpreter on your breakpoint quickly. If you know, how the reason for this message can be solved - please put me a hint in the comments. However you could create a file with the name "-" and map to this file in the path settings, but this is not a real fix.