PHPackages                             e0ipso/drupal-unit-autoload - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. e0ipso/drupal-unit-autoload

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

e0ipso/drupal-unit-autoload
===========================

Allows you to load classes in non standard unknown file locations.

1.0.0(11y ago)1568.3k↑34.7%2GPL-2.0PHPPHP &gt;=5.4.0

Since Jun 14Pushed 9y ago2 watchersCompare

[ Source](https://github.com/e0ipso/drupal-unit-autoload)[ Packagist](https://packagist.org/packages/e0ipso/drupal-unit-autoload)[ RSS](/packages/e0ipso-drupal-unit-autoload/feed)WikiDiscussions master Synced yesterday

READMEChangelog (4)DependenciesVersions (5)Used By (0)

[![Coverage Status](https://camo.githubusercontent.com/9b7f363e01a6ea69969635ec8a7763205d5d0292ec7d8b2fcc69333072575b25/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d617465752d616775696c6f2d626f7363682f64727570616c2d756e69742d6175746f6c6f61642f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/mateu-aguilo-bosch/drupal-unit-autoload?branch=master) [![Build Status](https://camo.githubusercontent.com/de56b4d8b94d85501ba870bf3bb4ef254e9be22f7a49674caa3ae47ee4a44dfa/68747470733a2f2f7472617669732d63692e6f72672f65306970736f2f64727570616c2d756e69742d6175746f6c6f61642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/e0ipso/drupal-unit-autoload) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/0c583392ac34b37213c1f892ba9e68d1c6de47dbd5a5816869cb11af2cc3bc39/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f65306970736f2f64727570616c2d756e69742d6175746f6c6f61642f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/e0ipso/drupal-unit-autoload/?branch=master)

[![Gitter](https://camo.githubusercontent.com/abe08b740a4156153736f791393ec4da6619c4be73212e75769f52edacc0e2b5/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e253230436861742e737667)](https://gitter.im/e0ipso/drupal-unit-autoload?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

Drupal Unit Autoload
====================

[](#drupal-unit-autoload)

Have you ever wanted to add **PHPUnit** tests to your Drupal 7 module? Well, you should. This tool aims to help you to deal with autoloading the classes that you have in your Drupal installation.

The Problem
-----------

[](#the-problem)

The main problem arises when the class -or code- that you are testing depends on classes declared in other modules, or Drupal core itself.

Imagine that you are testing your `Car` class. That class depends on `\DrupalCacheInterface` (you are using a mock cache provider that **has** to implement that interface), and also depends on several classes from the service container module. After all you are injecting services in your `Car` class to be able to mock them afterwards, and that may require to have the `Drupal\service_container\` available to you during tests.

Since you are doing unit testing, you may not want to bootstrap Drupal to have the database available in order to be able to check in the registry to find all those classes.

At this point you can think *I will just use Composer's autoloader and define where to find those classes and namespaces*. This is when you realize that Drupal allows you to install contrib modules in **many** locations. That makes it impossible to ship your module with the relative paths that you need.

Imagine this possibility:

- Our custom module (the one that will have unit testing) is installed in `sites/example.org/modules/contrib/racing_modules/car`.
- The modules that the *car* module depends on are installed in:
    - `sites/all/modules/essentials/service_container`.
    - `sites/default/modules/contrib/dependency`.

It seems that if you wanted to provide the path to `includes/cache.inc` to make `\DrupalCacheInterface` available, then you would need to add a path like: `../../../../../../includes/cache.inc`. But what if someone decides to install your `car` module in `sites/all/modules/car`? That path you provided in the module will not work in that situation. The correct one would be `../../../includes/cache.inc`. Basically every site installation may need a different path.

The problem that this project aims to solve is to give you a way to provide a single path in your code that will work in all those scenarios.

The Solution
------------

[](#the-solution)

Meet the Drupal Unit Autoload. To include it, just add the following to your PHPUnit test class (change the path depending on the location of your test classes):

```
require_once __DIR__ . '/../../vendor/e0ipso/drupal-unit-autoload/autoload.php';
```

That will load Composer's autoloader + the Drupal capabilities.

The only thing that you need to do is add a new `composer.json` key with tokens in the path.

Inside the folder where you have your unit tests you will need to have a `composer.json` file that has:

```
{
  "require-dev": {
    "phpunit/phpunit": "4.7.*",
    "mockery/mockery": "0.9.*",
    "e0ipso/drupal-unit-autoload": "1.0.*"
  },
  "autoload": {
    "psr-0": {
      … This is usual Composer business …
    },
    "psr-4": {
      … This is usual Composer business …
      "Drupal\\service_container\\": "DRUPAL_CONTRIB/src",
      "Drupal\\Core\\": [
        "DRUPAL_CONTRIB/lib/Core",
        "DRUPAL_CONTRIB/src/DrupalCore"
      ]
    },
    "class-location": {
      "\\DrupalCacheInterface": "DRUPAL_ROOT/includes/cache.inc",
      "\\ServiceContainer": "DRUPAL_CONTRIB/lib/ServiceContainer.php",
      "\\Drupal": "DRUPAL_CONTRIB/lib/Drupal.php"
    }
  }
}
```

Running `composer install` on that folder will download PHPUnit, Mockery -and all of the tools that you use for your tests-. Additionally it will download this project, that is what `"e0ipso/drupal-unit-autoload": "0.1.*"` is for.

At this point you only need is add the paths with `DRUPAL_ROOT` or `DRUPAL_CONTRIB` in your composer file.

You have two options:

- Provide class names and the files where they are found using the new key inside of `autoload` called `class-location`.
- Provide psr-4 and psr-0 namespace prefixes and the path where they are mapped. This is very simmilar to what composer does, but with the magic that finds where the Drupal root is and where the contrib modules are installed.

In the paths that you provide, you will be able to include two tokens: `DRUPAL_ROOT` and `DRUPAL_CONTRIB`. Those tokens will be expanded to the real paths that they represent. This way, providing `DRUPAL_CONTRIB` can end up expanding in:

- `/var/www/docroot/sites/all/modules/ctools` in one Drupal installation.
- `/User/Sites/drupal-site/sites/default/modules/contrib/ctools` in another installation.

The important thing to note is that your code ships with the same *tokenized* path for everyone, without caring about where the dependencies are installed.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 58.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~6 days

Total

4

Last Release

4018d ago

Major Versions

0.1.2 → 1.0.02015-07-03

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1140906?v=4)[Mateu Aguiló Bosch](/maintainers/e0ipso)[@e0ipso](https://github.com/e0ipso)

---

Top Contributors

[![e0ipso](https://avatars.githubusercontent.com/u/1140906?v=4)](https://github.com/e0ipso "e0ipso (24 commits)")[![penyaskito](https://avatars.githubusercontent.com/u/516163?v=4)](https://github.com/penyaskito "penyaskito (16 commits)")[![gitter-badger](https://avatars.githubusercontent.com/u/8518239?v=4)](https://github.com/gitter-badger "gitter-badger (1 commits)")

### Embed Badge

![Health badge](/badges/e0ipso-drupal-unit-autoload/health.svg)

```
[![Health](https://phpackages.com/badges/e0ipso-drupal-unit-autoload/health.svg)](https://phpackages.com/packages/e0ipso-drupal-unit-autoload)
```

###  Alternatives

[prologue/support

Prologue Support is an extension for Illuminate Support

1516.8k](/packages/prologue-support)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
