Skip to content
Snippets Groups Projects
title: Testing
tags:
- documentation
- tools
- tests

Testing Drupal Code

This section describes everything we have to test our code from static code analysis to unit testing.

You also have to set up your environment with the tool l3d.

Static Code Analysis

We have several tools to perform static code analysis.

  • PHP LOC
  • Code Sniffer
  • Mess Detection

PHP LOC

PHP LOC prints several metrics about your code, like the average size of a class.

To execute PHP LOC on the console:

a test phploc <file_ordirectory>

Whether it is a directory, it runs the analysis for everything under this directory recursively.

PHP Code Sniffer

PHP Code Sniffer performs a static code analysis with a specific Drupal profile. It detects code smells like static calls.

To execute PHP Code Sniffer on the console:

a test phpcs <file_ordirectory>

Whether it is a directory, it runs the analysis for everything under this directory recursively.

PHP Mess Detection

PHP Mess Detection is a more detailed analysis comparing to code sniffer. It uses several profiles like:

  • cleancode
  • design

This analysis detects also code smells like the complexity (e.g. number of dependencies) of classes.

To execute PHP Code Sniffer on the console:

a test phpmd <file_ordirectory> json <profile>

Whether it is a directory, it runs the analysis for everything under this directory recursively.

Unit/Kernel/Functional Testing

To execute the tests, you can use a single command:

a test phpunit <file_ordirectory>

All kind of Drupal tests will be executed. Whether it is a directory, it runs the analysis for everything under this directory recursively.

You also can execute the tests within your IDE.

First got to the settings of your IDE and add a new PHP interpreter. Select Docker and enter the form:

Screenshot

Use the option Docker Compose:

Screenshot

Fill the following form like:

Screenshot

Use the server you created for debugging.

The next step is to create a settings for your test framework. Remove all configuration and add a new one for remote interpreter:

Screenshot

Screenshot

Set the autoloader and the configurations file.

Now you are ready to go. Open a directory or a single test file (or method) and click on the green arrow:

Screenshot

You can also use the execution with the debugger.

Code Coverage

First, activate the coverage option:

a debug coverage

Enter the projects to cover into your .lakedrops.yml file, like:

lakedrops-dev:
  coverage:
    - ../web/modules/contrib/bpmn_io
    - ../web/modules/contrib/camunda
    - ../web/modules/contrib/eca
    - ../web/modules/contrib/eca_entity_share
    - ../web/modules/contrib/eca_state_machine
    - ../web/modules/contrib/eca_tamper

This is just an example!

You have to execute the following command to write a new config file for testing:

composer lakedrops:scaffold

Now you can execute every test including code coverage. To check, if it is working properly, a new view for coverage should open in your IDE, like:

Screenshot