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:
1 |
|
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:
1 |
|
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:
1 |
|
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:
1 |
|
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:
Use the option Docker Compose:
Fill the following form like:
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:
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:
You can also use the execution with the debugger.
Code Coverage¶
First, activate the coverage option:
1 |
|
Enter the projects to cover into your .lakedrops.yml
file, like:
1 2 3 4 5 6 7 8 |
|
This is just an example!
You have to execute the following command to write a new config file for testing:
1 |
|
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: