---
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](../docker/l3d/index.md).

## 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:

```l3d
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:

```l3d
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:

```l3d
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:

```l3d
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](/files/screenshots/settings_php_interpreter.png)

Use the option Docker Compose:

![Screenshot](/files/screenshots/settings_php_interpreter_docker.png)

Fill the following form like:

![Screenshot](/files/screenshots/settings_php_interpreter_new.png)

Use the server you created for [debugging](./debug.md).

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

![Screenshot](/files/screenshots/settings_php_test_framework_new_config.png)



![Screenshot](/files/screenshots/settings_php_test_framework_new.png)

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](/files/screenshots/settings_php_test_execution.png)

You can also use the execution with the debugger.

### Code Coverage

First, activate the coverage option:

```l3d
a debug coverage
```

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

```yml
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:

```l3d
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](/files/screenshots/settings_php_test_coverage_view.png)