Drupal

Drupal 8: Debug Kernel, Functional and Unit Tests in PhpStorm by Xdebug

Submitted by Peter Majmesku on Sun, 04/16/2017 - 20:21

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.

  1. 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.
  2. 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 

  • SIMPLETEST_BASE_URL
  • SIMPLETEST_DB
  • BROWSERTEST_OUTPUT_DIRECTORY
    • Example value: ~/PhpstormProjects/drupal-8.4.x-dev/sites/simpletest/browser_output
  • PHP_IDE_CONFIG
    • 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"/>
  • XDEBUG_CONFIG
    • 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):

 

PHPUnit config

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.

Server setting directory mapping.

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.

php interpreter
php cli

Run a Test File

For being able to execute your test quickly, open the PHP file with your test and hit the debug icon.

Run the test

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.

Path settings
Happy debugging

Drupal 8: Execute automated tests and debug them in PhpStorm

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:51

To test Drupal 8 tests by the command line, you need to do the following. You need to export two environment variables first:

export SIMPLETEST_DB=mysql://root:password@localhost/database;
export SIMPLETEST_BASE_URL=http://host;

Then you can execute the test by the following command, being in the Drupal root folder:

vendor/bin/phpunit -c core modules/my_module/tests/src/Kernel/MyTest.php

Make sure to set the environment variable for PhpStorm:

export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"

phpunit.xml 

There's a phpunit.xml.dist file in PHP projects. That's a template for your PHPUnit configuration. You can specifiy there some PHP ini settings. By this method you do not need to export the variables like above. Copy it to phpunit.xml and set there your settings like follows:

 

<phpunit bootstrap="tests/bootstrap.php" colors="true"

beStrictAboutTestsThatDoNotTestAnything="true"

beStrictAboutOutputDuringTests="true"

beStrictAboutChangesToGlobalState="true"

checkForUnintentionallyCoveredCode="false"

printerClass="\Drupal\Tests\Listeners\HtmlOutputPrinter">

<php>

<!-- Set error reporting to E_ALL. -->

<ini name="error_reporting" value="32767"/>

<!-- Do not limit the amount of memory tests take to run. -->

<ini name="memory_limit" value="-1"/>

<!-- Example SIMPLETEST_BASE_URL value: http://localhost -->

<env name="SIMPLETEST_BASE_URL" value="http://pbt-d8"/>

<!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->

<env name="SIMPLETEST_DB" value="mysql://root:password@localhost/pbt-d8"/>

<!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->

<env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/home/peter/d8-tests"/>

</php>

</phpunit>

Afterwards you are able to run your test without the manual environment variables setting, just by specifying your configuration directory by the "-c" parameter. Example:

vendor/bin/phpunit -c modules/permissions_by_term modules/permissions_by_term/tests/src/Kernel/SelectTermTest.php

This command cannot execute SimpleTest tests, which inherit from the WebTestBase class. Only unit tests and tests which inherits from the KernelTestBase class can be run.

Runs a SimpleTest

// --- SimpleTests
php core/scripts/run-tests.sh --url http://d8-paragraphs:8888 --browser --verbose --color --concurrency 4 --class 'Drupal\paragraphs\Tests\ParagraphsPreviewTest'

Troubleshooting

If the debug mode is switched on in PhpStorm and the debugger does not stop at the breakpoint and you are getting the following output in the debugger window:

Remote file path '/home/user/Websites/my-project/-' is not mapped to any file path in project

 If you have already setup path mapping, then there's probably any error in your code. Chances are, that it is in the first line of the setUp() method of your test class, which inherits from KernelTestBase. Then you probably do not have Drupal 8.2.x but Drupal 8.1.x. If it's in the first line, you can insert any code line or insert

xdebug_break(); 

With this function the debugger will be forced to stop on a specific line.


PhpStorm IDE settings (version 2016.1.2)

If you are executing the tests from the command line (and probably also via the IDE itself) it's important that you setup the path mappings, so that Xdebug can recognize your test's location. You can to this in PhpStorm's settings at "Languages & Frameworks > PHP > Servers". Create a host for your Drupal website host and set the path mapping to your tests directory. An example you can find in the screenshot below: 

PhpStorm Path Mapping

Drush: Rebuild the cache of a single Drupal (8) site in a multi-site setup with the magic "-l" parameter

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:02

After lots of googling, testing, reading and so on, I've finally found out the Drush command to flush the cache of a single Drupal (8) site in a multi-site setup.

drush -l 8888.notes cache-rebuild

If your site is located at /sites/8888.notes you need the command above. The "-l" parameter is necessary. I could not find out what "-l" means, but this parameter seemed necessary. 

Tags
Subscribe to Drupal