Drupal Test

Autoloading, Includes and Dependencies

Autoloading

Without a class autoload map for dependency classes, your tests will fail. At first glance you may want to update the autoload section of tests/composer.json, but that should not be done, as that file gets overwritten on update. Instead you will edit drupal_test_config.yml.

Here is an example for when a unit test uses \Drupal\node\NodeInterface. You can see that we are mapping the PSR-4 namespace Drupal\node to ../web/core/modules/node/src. This should look familiar if you are used to adding PSR-4 autoloading to composer.json files.

Note: the configuration seen below will apply to all tests, so you don't need to do anything else for another test or test subject that needs to have access to \Drupal\node\NodeInterface.

autoload_psr4:
  Drupal\node\: ../web/core/modules/node/src

By manually configuring only what needs to be autoloaded for our particular tests, we are able to keep unit testing very fast, without the need to scan for files and dynamically create an autoload map.

Changes to the autoload_* configuration does not require composer update --lock to be called, but you may need to run composer dump.

You may skip autoload_psr4 if you are testing a module that handles autoloading via it's own composer.json file, in which case you want to use merge_composer.json, instead.

Autoloading Keys

The following keys correspond to Composer autoloading and should be used in the same way as described for Composer.

Merging composer.json Files

When testing modules with their own composer.json files, you probably need to register those module composer.json files in drupal_test_config.yml. This will inform the test runner to pull in those dependencies so they are available during testing, if your tests require that. Here is an example of what that could look like.

merge_composer.json:
  - ../web/modules/custom/alter_partials/composer.json
  - ../web/modules/custom/render_patterns/composer.json
  - ../web/modules/custom/loft_core/composer.json

Whenever you alter this section of drupal_test_config.yml, you must call composer update --lock from the Drupal test root directory. This is what pulls in the dependencies. Under the hood, this feature uses the Composer Merge Plugin.

Module Setup

Drupal Test uses Composer for autoloading when Unit testing modules.

  1. In your module's directory, create composer.json and add it's path to merge_composer.json, in drupal_test_config.yml.
  2. Also in the module's composer.json, use autoload-dev to create a namespace map for your module so it's classes can be autoloaded.

    {
        "autoload-dev": {
            "psr-4": {
                "Drupal\\my_module\\": "src/"
            }
        }
    }
    
  3. If any tests or test subjects rely on Drupal Core classes then map those namespaces directly in drupal_test_config.yml. Do not add anything to autoload-dev, that is outside of your module's directory.

  4. If you are writing tests that cover functions, then add the file defining those functions in your module's composer.json, e.g.,

    {
        "autoload-dev": {
            ...
            "files": [
                "my_module.module"
            ]
        }
    }        
    
  5. In the Drupal Test phpunit.xml, make sure your unit tests are discoverable, e.g.,

    <phpunit ...>
        <testsuites>
            <testsuite name="Unit">
                <directory>../web/modules/custom/*/tests/src/Unit</directory>
            </testsuite>
        </testsuites>
    </phpunit>