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
- You can also use a SQLite database (faster). Mind the two slashes between the host and the filesystem location: sqlite://localhost//tmp/test.sqlite
- 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.
Uncheck the option "Force break at first line when no path mapping specified" in PHPStorm
You might experience an error like "Cannot find file 'home/user/project/root/-' locally". Therefor you must uncheck the option "Force break at first line when no path mapping specified" at Settings/Languages & Frameworks/PHP/Debug in PHPStorm.
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.