Skip to content

Commit

Permalink
Merge pull request #16 from mateu-aguilo-bosch/autoloader-boostrap-tests
Browse files Browse the repository at this point in the history
Autoloader boostrap tests
  • Loading branch information
e0ipso committed Jun 17, 2015
2 parents 9fd4f17 + 31fd712 commit 6b13695
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/AutoloaderBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,43 @@ class AutoloaderBootstrap {
const AUTOLOAD_FUNCTION = '\Drupal\Composer\ClassLoader\Loader::autoload';
const COMPOSER_CONFIGURATION_NAME = 'composer.json';

/**
* Holds the composer autoloader.
*
* @var \Composer\Autoload\ClassLoader
*/
protected $loader;

/**
* Holds the seed.
*
* @var string
*/
protected $seed;

/**
* Constructs a AutoloaderBootstrap object.
*
* @param \Composer\Autoload\ClassLoader $loader
* The Composer class loader.
* @param string $seed
* The seed to find the drupal projects.
*/
public function __construct(\Composer\Autoload\ClassLoader $loader) {
public function __construct(\Composer\Autoload\ClassLoader $loader, $seed = 'composer.json') {
$this->loader = $loader;
$this->seed = $seed;
}

/**
* Register the autoloader if it is not registered.
*/
public function register() {
if ($functions = spl_autoload_functions()) {
if (array_search(static::AUTOLOAD_FUNCTION, $functions)) {
return;
}
if ($this::checkLoadedAutoloader()) {
return;
}
// Parse the composer.json.
$composer_config = json_decode(file_get_contents(static::COMPOSER_CONFIGURATION_NAME));
Loader::setSeed('composer.json');
Loader::setSeed($this->seed);
$this->registerDrupalPaths($composer_config);
$this->registerPsr($composer_config);
}
Expand Down Expand Up @@ -97,4 +112,23 @@ protected function registerPsr($composer_config) {
Loader::registerPsr($this->loader);
}

/**
* Checks if the autoloader has been added.
*
* @return bool
*/
public static function checkLoadedAutoloader() {
$autoloader_str = ltrim(static::AUTOLOAD_FUNCTION, '\\');
$autoloader = explode('::', $autoloader_str);
foreach (spl_autoload_functions() as $callable) {
if (
($callable[0] == $autoloader[0] || $callable[0] == $autoloader[0]) &&
$callable[1] == $autoloader[1]
) {
return TRUE;
}
}
return FALSE;
}

}
6 changes: 6 additions & 0 deletions tests/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@
"Drupal\\Composer\\ClassLoader\\": "../src/",
"Drupal\\Composer\\ClassLoader\\Tests\\": "src/"
}
},
"class-loader": {
"drupal-path": {
"\\Tmp": "DRUPAL_ROOT/file.inc",
"\\Tmp2": "data/acme.inc"
}
}
}
57 changes: 57 additions & 0 deletions tests/src/AutoloaderBootstrapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* @file
* Contains Drupal\Composer\ClassLoader\Tests\AutoloaderBootstrapTest.
*/

namespace Drupal\Composer\ClassLoader\Tests;

use Drupal\Composer\ClassLoader\AutoloaderBootstrap;
use Mockery as m;
/**
* Class AutoloaderBootstrapTest
*
* @coversDefaultClass Drupal\Composer\ClassLoader\AutoloaderBootstrap
*
* @package Drupal\Composer\ClassLoader\Tests
*/
class AutoloaderBootstrapTest extends \PHPUnit_Framework_TestCase {

/**
* Tests the ::__construct() method.
*/
public function test___construct() {
$loader = m::mock('\Composer\Autoload\ClassLoader');
$autoloader = new AutoloaderBootstrap($loader);
$reflection_property = new \ReflectionProperty(get_class($autoloader), 'loader');
$reflection_property->setAccessible(TRUE);
$value = $reflection_property->getValue($autoloader);
$this->assertEquals($loader, $value);
$reflection_property = new \ReflectionProperty(get_class($autoloader), 'seed');
$reflection_property->setAccessible(TRUE);
$value = $reflection_property->getValue($autoloader);
$this->assertEquals('composer.json', $value);
}

/**
* Tests the ::register() method.
*
* @covers ::register()
* @covers ::load()
* @covers ::registerDrupalPaths()
* @covers ::checkLoadedAutoloader()
*/
public function test_register() {
$loader = m::mock('\Composer\Autoload\ClassLoader');
$autoloader = new AutoloaderBootstrap($loader, 'data/docroot/sites/all/modules/testmodule/composer.json');
$autoloader->register();

$this->assertTrue($autoloader::checkLoadedAutoloader());
// Make sure that calling to register a second time does not fail.
$autoloader->register();
$this->assertTrue($autoloader::checkLoadedAutoloader());
}


}

0 comments on commit 6b13695

Please sign in to comment.