Skip to content

Commit

Permalink
Merge pull request #27 from mateu-aguilo-bosch/easier-autoload-inclusion
Browse files Browse the repository at this point in the history
Easier autoload inclusion
  • Loading branch information
e0ipso committed Jul 3, 2015
2 parents 8e5a0cb + 40aed2c commit 6680ffe
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 17 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ The problem that this project aims to solve is to give you a way to provide a si
all those scenarios.

## The Solution
Meet the Drupal Unit Autoload.
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):

```php
require_once __DIR__ . '/../../vendor/mateu-aguilo-bosch/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.

Expand Down
9 changes: 9 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Drupal\Composer\ClassLoader\AutoloaderBootstrap;

// Load Composer's autoloader.
$loader = require __DIR__ . '/../../autoload.php';

$autoloader_init = new AutoloaderBootstrap($loader);
$autoloader_init->register();
7 changes: 0 additions & 7 deletions drupal_unit_autoloader.php

This file was deleted.

20 changes: 11 additions & 9 deletions src/AutoloaderBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @package Drupal\Composer\ClassLoader
*/
class AutoloaderBootstrap {
class AutoloaderBootstrap implements AutoloaderBootstrapInterface {

const AUTOLOAD_METHOD = 'autoload';
const COMPOSER_CONFIGURATION_NAME = 'composer.json';
Expand Down Expand Up @@ -67,7 +67,7 @@ public function __construct(\Composer\Autoload\ClassLoader $classLoader, $seed =
}

/**
* Register the autoloader if it is not registered.
* {@inheritdoc}
*/
public function register() {
if ($this->checkLoadedAutoloader()) {
Expand Down Expand Up @@ -132,20 +132,15 @@ protected function registerPsr(array $composer_config) {
}

/**
* Checks if the autoloader has been added.
*
* @return bool
* {@inheritdoc}
*/
public function checkLoadedAutoloader() {
$functions = spl_autoload_functions();
return in_array(array($this->loader, static::AUTOLOAD_METHOD), $functions);
}

/**
* Gets the configuration for the drupal loader from the Composer loader.
*
* @return array
* The configuration array for the drupal loader.
* {@inheritdoc}
*/
public function getConfig() {
// Initialize empty configuration.
Expand Down Expand Up @@ -196,4 +191,11 @@ public function getConfig() {
return $config;
}

/**
* {@inheritdoc}
*/
public function getClassLoader() {
return $this->classLoader;
}

}
42 changes: 42 additions & 0 deletions src/AutoloaderBootstrapInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Created by PhpStorm.
* User: e0ipso
* Date: 03/07/15
* Time: 13:00
*/

namespace Drupal\Composer\ClassLoader;


interface AutoloaderBootstrapInterface {

/**
* Register the autoloader if it is not registered.
*/
public function register();


/**
* Checks if the autoloader has been added.
*
* @return bool
*/
public function checkLoadedAutoloader();

/**
* Gets the configuration for the drupal loader from the Composer loader.
*
* @return array
* The configuration array for the drupal loader.
*/
public function getConfig();
/**
* Gets the class loader.
*
* @return \Composer\Autoload\ClassLoader
* The loader.
*/
public function getClassLoader();

}
11 changes: 11 additions & 0 deletions tests/src/AutoloaderBootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,15 @@ public function test_getConfig() {
$this->assertEquals($expected, $config);
}

/**
* Tests the ::getClassLoader method.
*
* @covers ::getClassLoader()
*/
public function test_getClassLoader() {
$loader = m::mock('\Composer\Autoload\ClassLoader');
$autoloader = new AutoloaderBootstrap($loader, 'data/docroot/sites/all/modules/testmodule/composer.json');
$this->assertEquals($loader, $autoloader->getClassLoader());
}

}

0 comments on commit 6680ffe

Please sign in to comment.